【问题标题】:Calling custom WSDL in asp.net web service(ASMX)在 asp.net Web 服务(ASMX)中调用自定义 WSDL
【发布时间】:2014-08-11 11:44:14
【问题描述】:

我有一个webservice。我需要打电话给custom 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>

所以,进入主要部分。

我的 Web 服务编译成功,没有任何错误。现在,我将 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

我做了他在博客中描述的完全一样的事情。

【问题讨论】:

  • 取消注释该块并更正它
    localhost:62783/Service.asmx" />
  • 作者要求评论该行,因为 WSDL:PORT 是自己创建的
  • 该错误明确指出了绑定,那么如果您将其中一个绑定的名称更改为 CustomWSDL 会发生什么?喜欢将“ServiceSoap12”切换到“CustomWSDL”吗?只是一个想法。

标签: c# asp.net web-services wsdl


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您确定需要将其作为 ASMX 服务吗?该技术堆栈已被Windows Communication Foundation 取代。新的开发最好在该堆栈中完成。

    我将使用 ASMX 工具堆栈回答您的问题。

    您可以按照自己尝试的方式手工制作服务实现,但让工具为您完成繁重的工作要容易得多。

    对于 asmx,该工具称为 Web Services Description Language Tool (wsdl.exe),如果您启动 Visual Studio 命令提示符,该工具就在您的路径中。

    wsdl.exe
    为 xml Web 服务客户端和 xml Web 服务生成代码的实用程序 从 WSDL 合同文件、XSD 模式和 .discomap 中使用 ASP.NET 发现文件。该工具可与disco.exe配合使用。

    我复制了您的 wsdl 并将其保存在一个文件中。之后我运行 wsdl 工具来指导它为我生成一个服务器实现:

    wsdl /serverInterface /n:Any.AweSome.NameSpace /l:csharp custom.wsdl
    

    用法:wsdl.exe &lt;options&gt; &lt;url or path&gt; &lt;url or path&gt; ...

    使用的选项:

    • /serverInterface
      为 ASP.Net 的服务器端实现生成接口 网络服务。为 wsdl 中的每个绑定生成一个接口 文件。 wsdl 单独实现 wsdl 契约(类 实现该接口的不应包括以下任何一项 关于类方法:Web Service 属性或序列化 更改 wsdl 合同的属性)。缩写形式是 '/si'。

    • /language:&lt;language&gt;
      用于生成的代理类的语言。从“CS”中选择, 'VB'、'JS'、'VJS'、'CPP' 或为类提供完全限定名称 实现 System.CodeDom.Compiler.CodeDomProvider。默认 语言是“CS”(CSharp)。缩写形式是 '/l:'。

    • /namespace:&lt;namespace&gt; 生成的代理或模板的命名空间。默认命名空间 是全局命名空间。缩写形式是 '/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
    • SoapDocumentMethod 需要一组完全不同的属性。

    我建议您通过生成一个接口来更改当前的实现,并按如下方式实现该接口,将注释留在接口类中:

    // 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::命名空间中生成,你需要在你的接口前面添加。

    【讨论】:

    • 我想要一个 ASMX 服务本身。我的应用程序绑定到大型机,它来自过去 3 年。我的客户不希望 WCF 中的服务。这就是为什么我有这么多麻烦。
    • 我正在尝试你的步骤。当我运行 wsdl /serverInterface /l:csharp custom.wsdl 时,我得到相同的错误 Element binding named CustomWSDL from namespace tempuri.org is missing
    • 只需重新检查一些事情:您得到了一个 interface.cs 作为 WSDL 命令的输出?您确实在您的 web 项目中添加了现有文件以使用界面添加该文件?您确实用我提供的代码替换了 Service 类的代码(因此您摆脱了该类上的所有属性)?因为当我尝试这个时它确实有效......
    • 我添加了接口。但是当我尝试继承接口时,它并没有出现在列表中。
    • 在接口前面添加任何global:: 或在wsdl 文件中添加/n 选项以在您想要的命名空间中生成接口,如下所示:/n:Any.Awesome.Namespace.Here 并添加using Any.Awesome.Namespace.Here; at文件中的使用与您的实现
    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2011-09-03
    • 2013-03-27
    • 2010-10-07
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    相关资源
    最近更新 更多