2009-03-03 66 views
9

給定的合同,如:RESTful WCF服務可以在JSON(P)和XML中都響應,仍然可以用作SOAP Web服務?

[ServiceContract] public interface IService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "GetData/{id}.{format}")] 
    ResponseData GetData(string id, string format); 
} 

是否存在時所要求的方式去使用JSON響應服務:當作爲/GetData/1234.xml並請 /GetData/1234.json,XML仍然可以作爲一個適當的SOAP服務在其他網址,與強類型WSDL合同?

使用Stream作爲GetData的返回值是行不通的,就像它滿足前兩個要求一樣,wcf不能創建完整的wsdl規範,因爲它不知道生成的Stream的內容是什麼。

+0

請參閱[codemeit](http://www.codemeit.com/wcf/wcf-restful-pox-json-and-soap-coexist.html)中的[WCF RESTful POX,JSON和SOAP共存] //stackoverflow.com/users/11413/codemeit)。 – 2009-03-04 07:20:34

回答

12

應該有哪些需要ID和格式兩種不同的方法(他們會打電話共享的實現,它返回ResponseData),其中有不同WebGet attributes

[ServiceContract] 
public interface IService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "GetData/{id}.{format}.xml", 
     ResponseFormat=WebMessageFormat.Xml)] 
    ResponseData GetDataXml(string id, string format); 

    [OperationContract] 
    [WebGet(UriTemplate = "GetData/{id}.{format}.json", 
     ResponseFormat=WebMessageFormat.Json)] 
    ResponseData GetDataJson(string id, string format); 
} 

對於SOAP端點,你應該能夠調用其中任何一種方法,但您將必須有一個單獨的ServiceHost實例來執行合同。