1

I have two interfaces in a WCF web service like below;

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "GetTypes",
    BodyStyle = WebMessageBodyStyle.Bare,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    string GetTypes();

    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "GetTypes",
    BodyStyle = WebMessageBodyStyle.Bare,
    ResponseFormat = WebMessageFormat.Xml,
    RequestFormat = WebMessageFormat.Xml)]
    XmlDocument GetTypes();
}

Basically I'm wanting to allow the incoming requests to support either Xml or Json formation. But I'm getting the compilation error of

Type 'Service.Service' already defines a member called 'GetTypes' with the same parameter types C:\Projects\WCF\Service.svc.cs

To over come this error I can code as follows;

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "GetTypes",
    BodyStyle = WebMessageBodyStyle.Bare,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    string GetTypes(string sJson);

    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "GetTypes",
    BodyStyle = WebMessageBodyStyle.Bare,
    ResponseFormat = WebMessageFormat.Xml,
    RequestFormat = WebMessageFormat.Xml)]
    XmlDocument GetTypes(XmlDocument oXml);
}

The GetTypes method would be something like;

public string GetTypes(string sJson)
{
    var sr = new StreamReader(sJson);
    string text = sr.ReadToEnd();
    //do something .... and return some Json
}

and

public XmlDocument GetTypes(XmlDocument oXml)
{
    var sr = new StreamReader(oXml);
    string text = sr.ReadToEnd();
    //do something .... and return a XmlDocument
}

Is this the best way of achieving this, or is their a better alternative. Or am I better have two methods like

GetTypesXml(XmlDocument oXml)

and

GetTypesJson(string sJson)

Seymour
  • 7,043
  • 12
  • 44
  • 51
neildt
  • 5,101
  • 10
  • 56
  • 107
  • This is basic c#, two methods with same name only differ if parameters differ, not the return type. – Abhinav Feb 10 '14 at 17:23

1 Answers1

1

The following MSDN article seems to address the method overloading issue you have.

Changing the return type of a method does not make the method unique as stated in the common language runtime specification. You cannot define overloads that vary only by return type.

http://msdn.microsoft.com/en-us/library/vstudio/ms229029(v=vs.100).aspx

In the event that you need two similar methods that only differ on the return type, you may want to consider different method names rather than trying to force an overload. (e.g. GetTypes and GetTypesXML)

Seymour
  • 7,043
  • 12
  • 44
  • 51
  • Is having similar methods that only differ on the return type, the only option I have ? – neildt Feb 11 '14 at 11:45
  • I think you either need to have two methods or two endpoints. The following link describes the later: http://stackoverflow.com/questions/186631/rest-soap-endpoints-for-a-wcf-service – Seymour Feb 11 '14 at 11:58