最近工作需要向第三方提供一個WebService服務,坑爹的是第三方背景牛X,我方提供的服務必須完全遵照其客戶端方預先定義好了的接口,一個符號都不允許修改。
.net平台編寫的WebService由於是根據微軟的標准,所以在wsdl方法名上面加上了特定的后綴,如<wsdl:port name="Service1Soap" binding="tns:Service1Soap"> 其中Soap就是默認加入的。為了完全適應調用方,這種默認方式顯然是不行的,網上、網下研究了半天,最終采用了擴展SoapExtensionReflector類的方法來截獲WSDL的最終生成。其方法如下:
自定義一個繼承自SoapExtensionReflector的類,重載ReflectDescription方法,在這個方法中對WSDL進行特定干預,基本上是想干啥就干啥,呵呵。
using System; using System.Collections.Generic; using System.Web; using System.Web.Services.Description; using System.Xml.Serialization; using System.Xml.Schema; using System.Xml; public class WSDLReflector : SoapExtensionReflector { /// <summary> /// ws方法不做修改 /// </summary> public override void ReflectMethod() { } /// <summary> /// 繼承修改描述方法 /// </summary> public override void ReflectDescription() { ServiceDescription description = ReflectionContext.ServiceDescription; foreach (Service service in description.Services) { foreach (Port port in service.Ports) { port.Name = port.Name.Replace("uaService", "uaWebService"); } } } }
此類根據自己的實際需要編寫完畢后,需要在項目Web.confing設置節點,才能應用,節點設置如下:
<system.web> <webServices> <!--處理WSDL標簽修改--> <soapExtensionReflectorTypes> <!--<add type ="DUS.Pub.Ws.WSDLReflector,DUS.Pub"/>--> <add type ="AppManager.Code.WSDLReflector,AppManager"/> </soapExtensionReflectorTypes> </webServices> </system.web>
至此編譯生成,本項目生成的WebService服務WSDL即可實現自定義了。