我有一个描述WCF服务中使用的方法的ServiceContract。该方法有一个定义UriTemplate和ResponseFormat的WebGet属性。
我想重用单个方法,并拥有具有不同UriTemplates和不同ResponseFormats的多个WebGet属性。基本上,我希望避免使用多个方法来区分返回类型是XML还是JSON。不过,在我到目前为止看到的所有示例中,都需要为每个WebGet属性创建不同的方法。这是一个示例OperationContract
[ServiceContract]
public interface ICatalogService
{
[OperationContract]
[WebGet(UriTemplate = "product/{id}/details?format=xml", ResponseFormat = WebMessageFormat.Xml)]
Product GetProduct(string id);
[OperationContract]
[WebGet(UriTemplate = "product/{id}/details?format=json", ResponseFormat = WebMessageFormat.Json)]
Product GetJsonProduct(string id);
}
使用上面的例子,我想对xml和json返回类型使用GetProduct方法,如下所示:
[ServiceContract]
public interface ICatalogService
{
[OperationContract]
[WebGet(UriTemplate = "product/{id}/details?format=xml", ResponseFormat = WebMessageFormat.Xml)]
[WebGet(UriTemplate = "product/{id}/details?format=json", ResponseFormat = WebMessageFormat.Json)]
Product GetProduct(string id);
}
有没有办法做到这一点,这样我就不用编写不同的方法来返回不同的ResponseFormats了?
谢谢!
https://stackoverflow.com/questions/1746857
复制相似问题