您可以使用
SoapExtensionReflector类来修改WSDL。根据
Kirk Evans博客:
当反射您的类型以提供服务的WSDL定义时,将调用SoapExtensionReflector。 您可以利用此类型拦截反射调用并修改WSDL输出。
以下示例删除了2个Web服务方法中的第一个方法:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public int Multiply(int a, int b)
{
return a * b;
}
}
创建一个继承自SoapExtensionReflector的类:
namespace TestWebservice
{
public class MyReflector : SoapExtensionReflector
{
public override void ReflectMethod()
{
}
public override void ReflectDescription()
{
ServiceDescription description = ReflectionContext.ServiceDescription;
if (description.PortTypes[0].Operations.Count == 2)
description.PortTypes[0].Operations.RemoveAt(0);
if (description.Messages.Count == 4)
{
description.Messages.RemoveAt(0);
description.Messages.RemoveAt(0);
}
foreach (Binding binding in description.Bindings)
{
if (binding.Operations.Count == 2)
binding.Operations.RemoveAt(0);
}
if (description.Types.Schemas[0].Items.Count == 4)
{
description.Types.Schemas[0].Items.RemoveAt(0);
description.Types.Schemas[0].Items.RemoveAt(0);
}
}
}
}
将以下内容添加到web.config文件的configuration/system.web部分:
<webServices>
<soapExtensionReflectorTypes>
<add type="TestWebservice.MyReflector, TestWebservice" />
</soapExtensionReflectorTypes>
</webServices>
这应该为您提供一个起点来动态删除WSDL文档中的方法。如果禁用了Web方法,您还需要从中抛出NotImplementedException。
最后,您需要禁用通过调用.asmx端点生成的Web服务文档,而不使用?WSDL参数。将wsdlHelpGenerator元素的href属性设置为某个URL。您可以使用DefaultWsdlHelpGenerator.aspx作为自己文档处理程序的起点。有关Web服务文档的问题,请参见
XML Files,2002年8月中的问题。