您可以使用SoapExtensionReflector类修改WSDL。从Kirk Evans Blog:
当您的类型被反映出来为您的服务提供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()
{
//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);
}
}
}
}
此添加到配置/ System.Web程序部在web.config中:
<webServices>
<soapExtensionReflectorTypes>
<add type="TestWebservice.MyReflector, TestWebservice" />
</soapExtensionReflectorTypes>
</webServices>
这应该给你一个动态从WSDL文档中删除方法的出发点。如果禁用,您还需要从Web方法中抛出NotImplementedException。
最后,您需要禁用不带?WSDL参数调用.asmx端点生成的Web服务文档。将wsdlHelpGenerator元素的href属性设置为某个URL。您可以使用DefaultWsdlHelpGenerator.aspx作为您自己的文档处理程序的起点。请参阅XML Files, August 2002中有关Web服务文档的问题。
感谢您的快速响应 - 不幸的是它必须是SOAP。我直到现在都在使用XMLRPC,但与不愿使用XMLRPC的第三方接口意味着我必须用SOAP来玷污它。 :( – 2009-05-30 22:24:37
嗯......“祝你好运...” – 2009-05-30 22:25:59