C# - How do I hide a web method from a derived class
I have a .NET webservice, and derived versions of it.
My question is, in the derived versions, I have a method I want to hide(from WSDL and front page). I have tried overriding, marking it as obsolete, setting it as private and overriding, but still, the webservice attribute is still "havin开发者_StackOverflowg its way".
Is there any way to remove a method attribute on a derived method. Or any way to hide an method in the WSDL?
//Robin
You are having trouble to achieve that because the inheritance system of .NET (and of any other object oriented framework, for that matter) is not designed for that: take a look at the Liskov substitution principle.
Maybe you should take an alternative course to achieve what you want. For example, instead of using inheritance, create a completely new service, and make its methods to simply invoke the equivalent methods on the original service class; this way you can include only the methods you want in the new service.
I don't think there is an option you can enable to get this behavior. If you really need to hide it and you can't simply remove it, I would create a new webservice which exposes only the method you want exposed.
This method works with .Net 4.0 :
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public virtual string HelloWorld()
{
return "Hello World";
}
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service2 : Service1
{
public override string HelloWorld()
{
throw new NotImplementedException();
}
}
Service2 derive from Service1 and override the HelloWorld WebMethod without specify the WebMethodAttribute
精彩评论