2014-12-26 4 views
0

클래스가 MethodResponse로 있습니다. 여기에 있습니다 :wcf에서 중첩 클래스를 포함하는 클래스를 반환하는 방법은 무엇입니까?

[Serializable] 
    [DataContract] 
    public class MethodResponse 
    { 
     public enum ResponseType 
     { 
      Succeed, 
      Error, 
      NotValid, 
      DataIsAlreadySaved 
     }; 

     [DataMember] 
     public ResponseType Type; 

     [DataMember] 
     public string ResultText; 

     [DataMember] 
     public object Object; 


    } 

WCF에서 MethodResponse를 반환하려고합니다. Object 필드를 설정하지 않으면 반환 할 수 있지만 Objet의 값을 설정하면 다음과 같은 오류가 반환됩니다.

An error occurred while receiving the HTTP response to http://localhost:49601/CustomerService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. 

여기

  public MethodResponse GetCustomerById(int _id) 
      { 
       CustomerDb db = new CustomerDb(); 

       return db.GetCustomerById(_id); 
      } 

      public MethodResponse GetCustomerById(int _id) 
      { 
       try 
       { 
        MethodResponse mrMethodResponse = new MethodResponse(); 

        using (var contex = new TestEntities()) 
        { 

         contex.Configuration.LazyLoadingEnabled = false; 
         Customer customer = contex.Customers.FirstOrDefault(cstm => cstm.id == _id); 
           mrMethodResponse.Type = MethodResponse.ResponseType.Succeed; 
         mrMethodResponse.Type = MethodResponse.ResponseType.Succeed; 
         mrMethodResponse.Object = customer; 


         return mrMethodResponse; 
        } 


       } 
       catch (Exception) 
       { 

        throw; 
       } 
      } 

답변

1

귀하의 문제는 당신이 object를 반환하려는 것입니다 내 WCF 및 데시벨 클래스 기능입니다. 그것은 허용되지 않습니다. 구체적 클래스 또는 제네릭 유형을 지정해야합니다.

[DataMember] 
    public object Object; 

에서

[DataMember] 
    public Customer Object; 

으로

변경 속성 유형은 또한 Customer 클래스가 직렬화 있는지 확인해야합니다.

+0

예,하지만 모든 유형의 객체를 반환하려면 MethodResponse를 사용합니다. 또한 MethodResponse는 도우미 lib로 다른 ClassLibrary에 있습니다. 그래서 그 lib 디렉토리 내 개체를 몰라요. – cagin

+0

'any type'을 반환 할 수 없습니다. 서비스 클라이언트는 반환 할 수있는 객체가 무엇인지 유형을 파악해야합니다. 객체를 JSON과 같은 문자열로 serialize하고'string' 속성을 반환 할 수 있습니다. – opewix