Is it possible to define two operation contracts with the same paramiters? I want to have the same endpoint do diffrent things on post and get. My code is below
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "MAC/{input}")]
string MAC(string input);
[OperationContract]
[We开发者_Go百科bInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "MAC/{input}")]
Datalayer.Datacontracts.WebserviceMessage MAC(string input);
With the same name? NO.
Using same parameters as you have mentioned is possible. But not with the same name.
Services follow document-centric paradigm; so when designing services, we should get rid of object oriented thinking. Don't think about polymorphism, overloading or overriding.
The metadata of the service has to be shared as a document to even non-object oriented systems/platforms (to support interoperability).
In addition to what SaravananArumugam said - the code as you have doesn't even compile (you cannot have two methods in the same interface with the same name whose only difference is the return type). However, you can change the method names and continue with the same UriTemplate - you'll have a "virtual" method with the same name (i.e., the address the client uses will be the same - like in the example below.
One more thing: you shouldn't use [WebInvoke(Method = "GET")], use [WebGet] instead.
public class StackOverflow_6548562
{
public class WebserviceMessage
{
public string Data;
}
[ServiceContract]
public interface ITest
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "MAC/{input}")]
string MAC_Get(string input);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "MAC/{input}")]
WebserviceMessage MAC_Post(string input);
}
public class Service : ITest
{
public string MAC_Get(string input)
{
return input;
}
public WebserviceMessage MAC_Post(string input)
{
return new WebserviceMessage { Data = input };
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service/";
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
host.Open();
Console.WriteLine("Host opened");
WebClient c = new WebClient();
Console.WriteLine(c.DownloadString(baseAddress + "/MAC/fromGET"));
Console.WriteLine(c.UploadString(baseAddress + "/MAC/frompost", "POST", ""));
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
精彩评论