2016-07-01 2 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

DataContractIsReference=true을 추가 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가 작동 할 수 있습니다. 제가 말할 수있는 한, 그는 단지 2 개의 간단한 구체를 가지고 있습니다. 그래서 나는 그것이 문제라고 생각하지 않는다. 그러나 당신의 "주기적인"것은 껍질을 벗기 좋은 나무일지도 모른다. – granadaCoder