c# - 在 asp.net web 服务中调用自定义 WSDL (ASMX)

标签 c# asp.net web-services wsdl

我有一个网络服务。我需要调用一个自定义 WSDL 对字段等进行一些验证。 我已经阅读了一些文章并完成了一些步骤,我将在下面展示。

1) 背后的 C# 代码

[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";
    }
}

位置设置为 service1.wsdl。然后我改为只检查它。

现在,我的 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 编译成功,没有任何错误。现在,我将 Web 服务导入到应用程序中,并且获得新的 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

我做了与他在博客中描述的完全相同的事情。

最佳答案

改变 [WebServiceBinding(Name = "CustomWSDL", Location = "Service1.wsdl")][WebServiceBinding(Name = "ServiceSoap", Location = "Service1.wsdl")]

阅读所提供链接中的说明 “Name 属性给出了自定义 WSDL 的名称,ASP.NET 需要它来正确地将调用分派(dispatch)到您的实现上。”

关于c# - 在 asp.net web 服务中调用自定义 WSDL (ASMX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25242494/

相关文章:

c# - 如何检查 .NET DLL 是否已注册?

c# - Session中存储的数据有多安全

c# - PdfPCell NoWrap=true 溢出 PdfPTable 的宽度

.net - MSBuild 失败,但在 Visual Studio 中构建工作正常

java - usernametoken-auth rampart/axis2 1.6.2

java - 在等式的 RHS 中使用值后更改 boolean 值?

asp.net - bin 文件夹内任何文件的任何更改是否会导致 ASP.NET Web 应用程序中的应用程序回收?

asp.net - 箭头键无法在 IE7 上滚动我们的网站

java - Restful web服务调用中的Spring错误

performance - Web 服务与常规 Http 请求