tags:

views:

671

answers:

2

I have a ServiceContract describing a method used in a WCF service. The method has a WebGet attribute which defines a UriTemplate and ResponseFormat.

I want to reuse a single method and have multiple WebGet attributes with different UriTemplates and different ResponseFormats. Basically I'm hoping to avoid having multiple methods just to differentiate things like return type being XML vs. JSON. In all of the examples I've seen so far I am required to create a different method for each WebGet attribute though. Here's a sample 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);
}

Using the example above I'd like to use the GetProduct method for both the xml and json return types like this:

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

Is there a way to achieve this so I'm not stuck writing different methods just to return different ResponseFormats?

Thanks!

+1  A: 

No, I don't think you can do this - if you want to support both XML and Json (and possibly more formats in the future), you need separate method calls as you have in the first sample.

Marc

marc_s