2015-10-20 45 views
0

,我有以下DataContract在我的WCF服務排除DataContract名稱從返回JSON

namespace MyNamespace 
{ 
    [DataContract(Namespace = Constants.ContractNamespace)] 
    public class MyResponse 
    { 
     [DataMember] 
     public string ResponseCode { get; set; } 

     [DataMember] 
     public List<MyObject> MyObjects { get; set; } 

    } 
} 

而當我再使用它作爲返回類型從我OperationContracts的一個這樣的

[OperationContract] 
MyResponse ValidateById(string id); 

我然後得到以下回應

{"MyResponse":{"MyObjects":null,"ResponseCode":"ERR001"}} 

是否有任何方法可以使這只是返回屬性IES所以它會像

{"MyObjects":null,"ResponseCode":"ERR001"} 

更新: 我也使用transportClientEndpointBehavior

<endpointBehaviors> 
    <behavior name="EndpointBehavior_IMyService"> 
     <transportClientEndpointBehavior> 
     <tokenProvider> 
      <sharedAccessSignature keyName="RootManageSharedAccessKey" key="keyhere" /> 
     </tokenProvider> 
     </transportClientEndpointBehavior> 
    </behavior> 
    </endpointBehaviors> 

和netTcpRelayBinding

<netTcpRelayBinding> 
    <binding name="NetTcpRelayBinding_IMyService"/> 
    </netTcpRelayBinding> 
+0

如果使用netTCP綁定,爲什麼不使用SOAP而不是JSON? – Menahem

回答

1

你可以使用體類型屬性設置包裝的響應或不從你的配置文件。 例如

<endpointBehaviors> 
    <behavior name="WebWithDefaults"> 
     <webHttp defaultOutgoingResponseFormat="Json" 
       defaultBodyStyle="Bare" /> 
    </behavior> 
</endpointBehaviors> 

上面的代碼假設webHttp協議。您可以將其更改爲您的

+0

感謝Patel,在使用transportClientEndpointBehavior時如何工作?我會更新這個問題 – QazPhilby

0

發現問題出在我的數據合同上。

我發現錯了一個上面實際上是

[DataContract(Name = "MyResponse", Namespace = Common.Constants.ContractNamespace)] 
public class MyValidateModel 
{ 
    [DataMember] 
    public MyResponse MyResponse { get; set; } 
} 

,但它需要

[DataContract(Name = "MyResponse", Namespace = Common.Constants.ContractNamespace)] 
public class MyValidateModel : Common.MyResponse 
{ 
}