Tag: .net

远程方法调用的.NET标准是什么?

场景: 通用程序集定义域对象类型(客户端和服务器程序集引用的程序集) 服务器程序集定义返回基本类型或公共程序集中定义的类型的类和方法 客户端程序集需要调用服务器定义的方法,当然,还要以适当的类型获取结果。 什么是.NET 3.5标准方法来解决这个问题? 你能否在网上指出任何资源来帮助我度过难关?

使用.Net中的Dictionary 进行线程安全

我有这个function: static Dictionary KeyValueDictionary = new Dictionary(); static void IncreaseValue(int keyId, int adjustment) { if (!KeyValueDictionary.ContainsKey(keyId)) { KeyValueDictionary.Add(keyId, 0); } KeyValueDictionary[keyId] += adjustment; } 我原本认为这不是线程安全的。 但是,到目前为止,在测试它时,我没有看到同时从多个线程调用它时的任何exception。 我的问题:它是线程安全还是我到目前为止幸运? 如果它是线程安全的那么为什么?

将INotifyPropertyChanged添加到模型?

我在我的wpf MVVM(基于Prism的)应用程序中遇到了一些设计问题,很乐意得到你的建议。 我的模型非常简单: public class Customer { public string FirstName {get;set;} public string LastName {get;set;} } 如您所见,我的Model类没有任何INotifyPropertyChnaged支持。 我还在CustomerDetails屏幕上有ViewModel,它支持INotifyPropertyChanged。 public class CustomerDetailsViewModel:INotifyPropertyChanged /*Or NotificationObject*/ { /*INotifyPropertyChanged event */ private Customer item; public Customer Item { get{return item;} set { item=value; //Raise PropertyChanged //Set IsDirty to true } } } 在我看来,我正在使用绑定到Item.FirstName和我正在更新的ViewModel。 我的问题是 – 因为只有FirstName属性通过View更新,而Model本身不支持INotifyPropertyChanged,因此Item setter没有被调用,并且IsDirty仍然等于false(因此不会更新IsDirty通知)用户界面)。 我知道我可以在模型中支持INotifyPropertyChanged,然后在视图模型中注册到Item.PropertyChanged事件,并实际将IsDirty设置为true,但是 – […]

在.NET 4.5中使用HttpClient进行编码

我正在使用fogbugz XML API消耗一些数据。 此API始终以UTF-8提供数据。 当使用WebClient类发出请求时,我可以设置编码。 例如: var result = new WebClient(); result.Encoding = Encoding.UTF8; 但是HttpClient类怎么样? HttpClient client = new HttpClient(); 我应该使用: client.GetByteArrayAsync(url); …然后将编码中的字节(UTF-8)转换为字符串? 或者有没有办法直接将内容作为UTF-8字符串? using (var client = Connector.GetHttpClient()) { var byteData = await client.GetByteArrayAsync(url); data = Encoding.UTF8.GetString(byteData); } 最后,这里是XML响应的摘录:

尝试调用有效方法重载时出现奇怪的“程序集未引用”错误

我在Assembly A使用方法重载: public static int GetPersonId(EntityDataContext context, string name) { var id = from … in context… where … select …; return id.First(); } public static int GetPersonId(SqlConnection connection, string name) { using (var context = new EntityDataContext(connection, false)) { return GetPersonId(context, name); } } 当我尝试从Assembly B调用第二个重载时,VS会产生以下编译时错误: “System.Data.Entity.DbContext”类型在未引用的程序集中定义。 您必须添加对程序集’EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = …’的引用。 […]

如果打开自定义错误,是否会触发global.asax Application_Error事件?

如果在Web配置中将自定义错误设置为RemoteOnly – 这是否意味着global.asax中的MVC应用程序级别错误事件 – Application_Error不会因错误而触发? 我刚刚注意到,当我的应用程序中发生某个错误,并且我正在远程查看该站点时,不会记录任何错误。 但是,当我访问服务器上的应用程序并发生相同的错误时,将记录错误。 这是自定义错误配置设置: 编辑 只是出于对人们的兴趣 – 我最终完全关闭了自定义错误并在Application_Error处理重定向,如下所示: protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // … log error here var httpEx = exception as HttpException; if (httpEx != null && httpEx.GetHttpCode() == 403) { Response.Redirect(“/youraccount/error/forbidden”, true); } else if (httpEx != null && httpEx.GetHttpCode() == 404) […]

无法更新数据库,因为它是只读的

我已经设置了我的网站以使用ASP.NET成员资格。 尝试在我的开发机器上使用它时一切正常但是当我把它放在Web服务器上并尝试登录时,我收到此错误: “Failed to update database “C:\INETPUB\WWWROOT\APP_DATA\ASPNETDB.MDF” because the database is read-only. “

MS C#编译器和非优化代码

注意:我发现我发布的示例中有一些错误 – 编辑修复它 如果你不启用优化,官方的C#编译器会做一些有趣的事情。 例如,一个简单的if语句: int x; // … // if (x == 10) // do something 如果优化,会变成以下内容: ldloc.0 ldc.i4.s 10 ceq bne.un.s do_not_do_something // do something do_not_do_something: 但如果我们禁用优化,它会变成这样: ldloc.0 ldc.i4.s 10 ceq ldc.i4.0 ceq stloc.1 ldloc.1 brtrue.s do_not_do_something // do something do_not_do_something: 我无法理解这一点。 为什么所有额外的代码,似乎在源中不存在? 在C#中,这相当于: int x, y; // … // y = x […]

如何在linq中包含()嵌套的子实体

我如何包括孩子的孩子? 即,乔布斯有引用具有QuoteItems var job = db.Jobs .Where(x => x.JobID == id) .Include(x => x.Quotes) .Include(x => x.Quotes.QuoteItems) // This doesn’t work .SingleOrDefault(); 只是为了更清楚 – 我正在尝试检索单个作业项目,并且它是关联的报价(一对多)和每个报价相关的QuoteItems(一个报价可以有许多QuoteItems) 我问的原因是因为在我的报价索引视图中,我试图通过SUMming小计显示每个报价的所有报价项目的总和,但是它出现为0.我正在调用这样的小计: @item.QuoteItem.Sum(p => p.Subtotal) 我相信我遇到这个问题的原因是我上面的Linq查询没有检索每个Quote的相关QuoteItems。

Generic方法可以处理Reference和Nullable Value类型吗?

我有一系列的Extension方法来帮助对IDataRecord对象进行空值检查,我目前正在实现这样: public static int? GetNullableInt32(this IDataRecord dr, int ordinal) { int? nullInt = null; return dr.IsDBNull(ordinal) ? nullInt : dr.GetInt32(ordinal); } public static int? GetNullableInt32(this IDataRecord dr, string fieldname) { int ordinal = dr.GetOrdinal(fieldname); return dr.GetNullableInt32(ordinal); } 等等,对于我需要处理的每种类型。 我想重新实现这些作为通用方法,部分是为了减少冗余,部分是为了学习如何编写通用方法。 我写过: public static Nullable GetNullable(this IDataRecord dr, int ordinal) { Nullable nullValue = null; return dr.IsDBNull(ordinal) […]