您可以使用SoapExtensionReflector类修改 WSDL。从柯克埃文斯博客:
SoapExtensionReflector 在反射您的类型时调用,以便为您的服务提供 WSDL 定义。您可以利用此类型拦截反射调用并修改 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()
{
//no-op
}
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 方法被禁用,您还需要从 Web 方法中抛出 NotImplementedException。
最后,您需要禁用通过调用没有 ?WSDL 参数的 .asmx 端点生成的 Web 服务文档。将 wsdlHelpGenerator 元素的 href 属性设置为某个 URL。您可以使用 DefaultWsdlHelpGenerator.aspx 作为您自己的文档处理程序的起点。请参阅XML 文件中有关 Web 服务文档的问题,2002 年 8 月。