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)