可以同时以JSON(P)和XML响应并且仍用作SOAP Web服务的RESTful WCF服务吗? [英] RESTful WCF service that can respond in both JSON(P) and XML and still be used as SOAP web service?
问题描述
给出合同,例如:
[ServiceContract] public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "GetData/{id}.{format}")]
ResponseData GetData(string id, string format);
}
是否有一种方法可以让服务在被请求时以json响应: /GetData/1234.json,以/GetData/1234.xml的身份被请求时,仍然可以通过强类型的wsdl合同在其他网址上作为适当的肥皂服务使用xml?
Is there a way to get the service to respond with json when requested as: /GetData/1234.json, xml when requested as /GetData/1234.xml and still be available as a proper soap service at some other url, with a strongly typed wsdl contract?
使用Stream作为GetData的返回值是不可行的,因为它不能满足前两个要求,wcf无法创建完整的wsdl规范,因为它不知道生成的Stream的内容是什么. /p>
Using a Stream as the return value for GetData is not workable, as though it fufills the first two requirements, wcf can't create a full wsdl specification as it has no idea what the contents of the resultant Stream will be.
推荐答案
您应该有两个采用id和format的单独方法(它们将调用返回ResponseData
的共享实现)具有不同的 WebGet
属性:
You should have two separate methods which take id and format (and they would call a shared implementation that returns ResponseData
) which have different 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端点,您应该能够调用这两种方法,但是您将不得不具有单独的
For the SOAP endpoint, you should be able to call either method, but you are going to have to have a separate ServiceHost
instance hosting the implementation of the contract.
这篇关于可以同时以JSON(P)和XML响应并且仍用作SOAP Web服务的RESTful WCF服务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!