您可以使用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