2016-07-01 30 views
0

我有以下课程但只有SayHello可以工作。你有没有人知道原因?WCF中的数据合同和循环参考

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)* 
+0

首先,你调试? –

+0

尝试启用WCF跟踪,可能会给您一些问题的指示:http://weblogs.asp.net/seaniannuzzi/wcf-diagnostics-implementation-in-5-easy-steps –

+1

这对您也有帮助发布您的WCF绑定... –

回答

1

添加IsReference=trueDataContract如下解释:Data Contracts and Circular References

[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()方法的成功执行:

enter image description here

0

打开WCF跟踪可能会告诉你,你需要知道的一切。 我希望你可能会看到一些细节级别的东西,当你在跟踪查看器工具中打开WCF轨迹时,它会给你更多的异常信息。我最初的怀疑是你可能需要为其中一个类声明KnownTypes。包含员工和员工的部门的周期性也可能导致序列化问题。我不记得我以前是否做过这件事。

看看这里的KnownTypes和WCF的详细信息:

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

+0

如果他正在返回一个接口或抽象类,KnownTypes可能会发挥作用。据我所知,他只有两个简单的混凝土。所以我不认为这是一个问题。但是你的“循环”的东西可能是一棵好树。 – granadaCoder