WCF的复杂对象?


                    

从JQuery AJAX调用WCF服务时遇到问题。 除非我尝试将以下代码添加到User类,否则GetUserDetails可以正常工作:</ p>

  [DataMember]
公共虚拟角色Role {get; 组; }
</ code> </ pre>

添加代码后,我得到错误状态12152。如果我注释掉Role方法的[DataMember]属性,则一切正常。 </ p>

此方法的OperationContract:</ p>

  [OperationContract]
     [WebInvoke(Method =“ POST”,
     ResponseFormat = WebMessageFormat.Json,
     RequestFormat = WebMessageFormat.Json,
     BodyStyle = WebMessageBodyStyle.Wrapped
     )]
用户GetUserDetails(string userName);
</ code> </ pre>

和用户类别:</ p>

  [DataContract]
[KnownType(typeof(Role))]
公共类用户
{
     [DataMember]
     公共虚拟int UserId { 组; }
     [DataMember]
     公共虚拟字符串UserName {get; 组; }
     [DataMember]
     公共虚拟字符串FirstName {get; 组; }
     [DataMember]
     公共虚拟字符串LastName {get; 组; }
     [DataMember]
     公共虚拟字符串Email {get; 组; }
     [DataMember]
     公共虚拟字符串公司{ 组; }
     [DataMember]
     公共虚拟字符串评论{get; 组; }
     [DataMember]
     公共虚拟布尔IsApproved {get; 组; }
     [DataMember]
     公共虚拟布尔IsLocked {get; 组; }
     [DataMember]
     公共虚拟布尔LoggedIn {get; 组; }

}
</ code> </ pre>

角色类</ p>

  [DataContract]
公共类角色
{
     [DataMember]
     公共虚拟字符串名称{get; 组; }
     [DataMember]
     公共虚拟字符串说明{get; 组; }
}
</ code> </ pre>

感谢您的帮助! </ p>
     </ div>

展开原文

原文

I'm having a problem calling the WCF service from JQuery AJAX. GetUserDetails works fine unless I try add this code to the User class:

[DataMember]
public virtual Role Role { get; set; }

After code is added I get error status 12152. If I comment out [DataMember] attribute of the Role method everything works.

Here's OperationContract for the method:

    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped
    )]
User GetUserDetails(string userName);

And User class:

    [DataContract]
[KnownType(typeof(Role))]
public class User 
{
    [DataMember]
    public virtual int UserId { get; set; }
    [DataMember]
    public virtual string UserName { get; set; }
    [DataMember]
    public virtual string FirstName { get; set; }
    [DataMember]
    public virtual string LastName { get; set; }
    [DataMember]
    public virtual string Email { get; set; }
    [DataMember]
    public virtual string Company { get; set; }
    [DataMember]
    public virtual string Comments { get; set; }
    [DataMember]
    public virtual bool IsApproved { get; set; }
    [DataMember]
    public virtual bool IsLocked { get; set; }
    [DataMember]
    public virtual bool LoggedIn { get; set; }

}

Role class

[DataContract]
public class Role
{
    [DataMember]
    public virtual string Name { get; set; }
    [DataMember]
    public virtual string Description { get; set; }
}

I would appreciate your help!

1个回答


感谢您的评论。 启用跟踪有助于我发现问题。 问题是因为我使用了NHibernate,Role对象被延迟加载,并且使用了该对象的代理,并且该代理对象无法序列化。 </ p>

 类型'DecoratorAopProxy_70ab9a81c5b84b6dbf624b0a94f7028e'不能被ISerializable并具有DataContractAttribute属性。
</ code> </ pre>

我关闭了延迟加载,这解决了问题。 非常感谢你! </ p>
     </ div>

展开原文

原文

thank you for your comments. Enabling tracing helped to me find the problem. The problem was because I used NHibernate, Role object were lazy loaded and proxy for that object were used and that proxy object couldn't be serialized.

Type 'DecoratorAopProxy_70ab9a81c5b84b6dbf624b0a94f7028e' cannot be ISerializable and have DataContractAttribute attribute.

I have turned off lazy loading and that solved the issue. Thank you very much!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问