RESTful WCF service that can respond in both JSON(P) and XML and still be used as SOAP web service?

后端 未结 1 1029
时光说笑
时光说笑 2021-02-08 20:02

Given a contract such as:

[ServiceContract] public interface IService
{
    [OperationContract]
    [WebGet(UriTemplate = \"GetData/{id}.{format}\")]
    Respons         


        
1条回答
  •  醉梦人生
    2021-02-08 20:17

    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);
    }
    

    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.

    0 讨论(0)
提交回复
热议问题