[英]Calling custom WSDL in asp.net web service(ASMX)
我有一個webservice
。 我需要調用一個custom WSDL
,並對字段等進行一些驗證。我已經閱讀了一些文章並完成了一些我將在下面展示的步驟。
[WebService(Namespace = "http://tempuri.org/")]
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[WebServiceBinding(Name = "CustomWSDL", Location = "http://localhost:62783/Service1.wsdl")]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[SoapDocumentMethod(Action = "urn:foo-com:service/HelloWorld", Binding = "CustomWSDL")]
public string HelloWorld(string i) {
return "Hello World";
}
}
現在,我的WSDL
看起來像這樣。
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="HelloWorld">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="i" >
<s:simpleType>
<s:restriction base="s:string">
<s:minLength value="1"/>
</s:restriction>
</s:simpleType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="HelloWorldResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="HelloWorldResult">
<s:simpleType>
<s:restriction base="s:string">
<s:minLength value="1"/>
</s:restriction>
</s:simpleType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="HelloWorldSoapIn">
<wsdl:part name="parameters" element="tns:HelloWorld" />
</wsdl:message>
<wsdl:message name="HelloWorldSoapOut">
<wsdl:part name="parameters" element="tns:HelloWorldResponse" />
</wsdl:message>
<wsdl:portType name="ServiceSoap">
<wsdl:operation name="HelloWorld">
<wsdl:input message="tns:HelloWorldSoapIn" />
<wsdl:output message="tns:HelloWorldSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="HelloWorld">
<soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="HelloWorld">
<soap12:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<!--<wsdl:service name="Service">
<wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
<soap:address location="http://localhost:62783/Service.asmx" />
</wsdl:port>
<wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">
<soap12:address location="http://localhost:62783/Service.asmx" />
</wsdl:port>
</wsdl:service>-->
</wsdl:definitions>
wsdl:service
進行評論。 所以,它的評論。 由於我需要使用自定義 WSDL,因此我在使用WsiProfiles.BasicProfile1_1
遇到了一個問題。 所以,我不得不使用 web.config 刪除它。
<system.web>
<webServices>
<conformanceWarnings>
<remove name='BasicProfile1_1'/>
</conformanceWarnings>
</webServices>
所以,進入主要部分。
我的 Webservice 編譯成功,沒有任何錯誤。 現在,我將 webservice 導入應用程序,並且我正在獲取我的新 WSDL 以及自動生成的WSDL
。 但是,當我嘗試構建語句時,它給了我一個錯誤。
Element binding named CustomWSDL from namespace http://tempuri.org/ is missing.
我瀏覽了 MSDN 和其他一些關於此錯誤的文章,但沒有一篇可以解決我的問題。
我在創建這個 WSDL 時犯了什么錯誤?
注意:我的參考: http ://craigandera.blogspot.com/2005/12/using-custom-wsdl-file-in-aspnet-web_15.html
我做了他在博客中描述的完全相同的事情。
您確定需要將其作為 ASMX 服務嗎? 該技術堆棧已被Windows Communication Foundation取代。 在該堆棧中更好地完成新的開發。
我將使用 ASMX 工具堆棧回答您的問題。
您可以按照您嘗試的方式手工制作服務實現,但讓工具為您完成繁重的工作要容易得多。
對於 asmx,該工具稱為Web 服務描述語言工具 (wsdl.exe) ,如果您啟動 Visual Studio 命令提示符,則該工具位於您的路徑中。
wsdl.exe
使用 ASP.NET 從 WSDL 合同文件、XSD 架構和 .discomap 發現文檔為 xml web 服務客戶端和 xml web 服務生成代碼的實用程序。 此工具可與disco.exe 結合使用。
我復制了您的 wsdl 並將其保存在一個文件中。 之后,我運行 wsdl 工具來指導它為我生成服務器實現:
wsdl /serverInterface /n:Any.AweSome.NameSpace /l:csharp custom.wsdl
用法: wsdl.exe <options> <url or path> <url or path> ...
使用的選項:
/serverInterface
為 ASP.Net Web 服務的服務器端實現生成接口。 為 wsdl 文檔中的每個綁定生成一個接口。 wsdl 單獨實現了 wsdl 契約(實現接口的類不應在類方法中包含以下任何一項:Web 服務屬性或更改 wsdl 契約的序列化屬性)。 簡寫為“/si”。
/language:<language>
用於生成的代理類的語言。 從“CS”、“VB”、“JS”、“VJS”、“CPP”中進行選擇,或者為實現 System.CodeDom.Compiler.CodeDomProvider 的類提供完全限定的名稱。 默認語言是“CS”(CSharp)。 縮寫形式是“/l:”。
/namespace:<namespace>
生成的代理或模板的命名空間。 默認命名空間是全局命名空間。 縮寫形式是“/n:”。 1
結果是您的服務實現應該實現的接口:
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.Web.Services.WebServiceBindingAttribute(Name="ServiceSoap", Namespace="http://tempuri.org/")]
public interface IServiceSoap {
/// <remarks/>
[System.Web.Services.WebMethodAttribute()]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/HelloWorld", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
string HelloWorld(string i);
}
根據此輸出,您可以從當前的嘗試中推斷出錯誤/缺失的內容:
[WebServiceBinding(Name = "CustomWSDL"..]
不應該是 CustomWSDL 而是ServiceSoap我建議您通過生成一個接口來更改當前的實現並按如下方式實現該接口,將注釋留在接口類中:
// at the top of the file add
using Any.AweSome.NameSpace;
// NO MORE ATTRIBUTES HERE!
// NOTICE the IServiceSoap at the end
public class Service : System.Web.Services.WebService, IServiceSoap
{
// AND ALSO NO MORE ATTRIBUTES HERE
public string HelloWorld(string i)
{
return "foo";
}
}
如果將來由於某種原因 WSDL 以及生成的接口發生變化,您可以輕松地為新接口調整現有的實現。
1.命名空間不是強制性的,如果您不添加該選項,則接口將在global::
命名空間中生成,您需要將其添加到您的界面前面。
將[WebServiceBinding(Name = "CustomWSDL", Location = "Service1.wsdl")]
更改為[WebServiceBinding(Name = "ServiceSoap", Location = "Service1.wsdl")]
提供的鏈接中的說明為“Name 屬性給出了自定義 WSDL 的名稱,ASP.NET 需要它以允許它正確地將調用分派到您的實現上。”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.