繁体   English   中英

WCF中的数据合同和循环引用

[英]Data Contracts and Circular References in WCF

我的课程在下面,但只有SayHello可以工作。 有人知道原因吗?

public class Service1 : IService1
{
    public Department GetDepartment()
    {
        Department d1 = new Department() { DepartmentName = "dep1" };

        d1.employees = new List<Employee>() {
                new Employee() {
                    username="user1",
                    department=d1
                },
                new Employee() {
                     username="user2",
                    department=d1
                }
            };

        return d1;
    }

    public string SayHello(string username)
    {
        return "Hello " + username + "!";
    }
}

[DataContract]
public class Department
{
    [DataMember]
    public string DepartmentName { get; set; }
    [DataMember]
    public List<Employee> employees { get; set; }
}

[DataContract]
public class Employee
{
    [DataMember]
    public string username { set; get; }
    [DataMember]
    public Department department { get; set; }
}

SayHello可以正常工作,但GetDepartment失败,并出现以下错误:

*在收到对http:// localhost:8080 /的HTTP响应时发生错误。 这可能是由于服务端点绑定未使用HTTP协议。 这也可能是由于服务器终止了HTTP请求上下文(可能是由于服务关闭了)。 有关更多详细信息,请参见服务器日志。 服务器堆栈跟踪:位于

System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at IService1.GetDepartment()
   at Service1Client.GetDepartment()
Inner Exception:
The underlying connection was closed: An unexpected error occurred on a receive.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
Inner Exception:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
Inner Exception:
An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)*

按照以下说明将IsReference=true添加到您的DataContract数据合同和循环引用

[DataContract(IsReference=true)]
public class Department
{
    [DataMember]
    public string DepartmentName { get; set; }
    [DataMember]
    public List<Employee> employees { get; set; }
}

[DataContract(IsReference=true)]
public class Employee
{
    [DataMember]
    public string username { set; get; }

    [DataMember]
    public Department department { get; set; }

}

显示成功执行GetDepartment()方法的测试客户端:

在此处输入图片说明

启用WCF跟踪可能会告诉您所有您需要了解的内容。 我希望您可能会在详细信息级别看到一些信息,一旦在跟踪查看器工具中打开WCF跟踪,它就会为您提供更多的异常信息。 我最初的怀疑是您可能需要为一个或其他类声明KnownTypes。 包含雇员的部门和包含雇员的部门的周期性也可能导致序列化问题。 我不记得以前是否做过。

在这里查看有关KnownTypes和WCF的更多信息:

https://msdn.microsoft.com/zh-CN/library/ms730167(v=vs.110).aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM