我正在编写Azure WCF应用程序 .
数据 Contract 定义如下:[DataContract] public class UserInfo { [DataMember] public string UserName { get; set; } [DataMember] public int UserID { get; set; } [DataMember] public bool IsOnline { get; set; } }
然后我在我的WCF服务中定义了一个datacontract:
[DataContract(Name="UserInfo")] public class ServiceUserInfo : UserInfo { [IgnoreDataMember] public ICallback Callback { get; set; } }
然后在服务 Contract 中,它将回调给客户端,方法如下
private void NoticeUsers(UserInfo currentuser) { var users = UserManager.GetAllActiveUsers(); foreach (var user in users) { if (user.UserName == currentuser.UserName) continue; user.Callback.UpdateUserList(currentuser); } }
实际上我将ServiceUserInfo对象作为参数传递给NoticeUsers方法 . 然后会出现如下错误:
尝试序列化参数http://tempuri.org/:user时出错 . InnerException消息是'Type ' WCFServiceWebRole.ServiceUserInfo ' with data contract name ' UserInfo:http://schemas.datacontract.org/2004/07/WCFServiceWebRole ' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.' . 有关更多详细信息,请参阅InnerException .
我无法找到这个问题的解决方案 . 请帮忙 .
1 回答
我想我找到了问题的根本原因,但我不知道为什么 .
该问题是由服务 Contract 的Namespace属性引起的 .
我没有设置我的服务 Contract 接口和数据协定接口的命名空间 . 我认为它会默认设置,不会造成任何问题 . 但实际上这个线程的问题是由Namespace属性引起的 . 我为服务 Contract 和数据 Contract 设置了Namespace属性,当然是相同的命名空间,然后就可以了 . 问题再也不会发生了 .
但是,我不知道为什么会引起这个问题 . 如果我没有设置Namespace,它将是默认值“http://tempuri.org”,不是吗?