C# 在asp.net web服务(ASMX)中调用自定义WSDL
我有一个C# 在asp.net web服务(ASMX)中调用自定义WSDL,c#,asp.net,web-services,wsdl,C#,Asp.net,Web Services,Wsdl,我有一个webservice。我需要调用一个自定义WSDL,并对字段等进行一些验证。 我已经阅读了一些文章并完成了一些步骤,我将在下面展示这些步骤 1) C#代码隐藏 该位置设置为service1.wsdl。然后我改成只检查一下。 现在,我的WSDL如下所示 <?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:soapenc="http://schemas.xmlsoap.org/soap/encodin
webservice
。我需要调用一个自定义WSDL
,并对字段等进行一些验证。
我已经阅读了一些文章并完成了一些步骤,我将在下面展示这些步骤
1) C#代码隐藏
该位置设置为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>
所以,进入主体部分
我的Web服务编译成功,没有任何错误。现在,我正在将webservice导入到应用程序中,我将获得新的WSDL以及自动生成的WSDL
。但是,当我试图建立一个说法时,它给了我一个错误
Element binding named CustomWSDL from namespace http://tempuri.org/ is missing.
我阅读了MSDN和其他一些文章来解决这个错误,但是没有一篇文章能够解决我的问题
创建此WSDL时我犯了什么错误
注: 我的参考:
我做了与他在博客中描述的完全相同的事情。你确定你需要这个作为ASMX服务吗?该技术堆栈已被替代。新的开发在这一堆中做得更好 我将使用ASMX工具堆栈回答您的问题 您可以像正在尝试的那样手工制作服务实现,但是让工具为您完成繁重的工作要容易得多 对于asmx,如果启动VisualStudio命令提示符,该工具将被调用并位于您的路径中 wsdl.exe
用于为xml web服务客户端和xml web服务生成代码的实用工具 从WSDL契约文件、XSD模式和.discmap中使用ASP.NET 发现文件。此工具可与disco.exe结合使用 我复制了您的wsdl并将其保存在一个文件中。之后,我运行wsdl工具来指示它为我生成一个服务器实现:
wsdl /serverInterface /n:Any.AweSome.NameSpace /l:csharp custom.wsdl
用法:wsdl.exe…
使用的选项:
/serverInterface
为ASP.Net的服务器端实现生成接口 网络服务。为wsdl中的每个绑定生成一个接口 文件。wsdl单独实现wsdl契约(类) 该接口的实现不应包括以下任何一项 关于类方法:Web服务属性还是序列化 更改wsdl契约的属性)。缩写为“/si”/语言:
用于生成的代理类的语言。从“CS”中选择, “VB”、“JS”、“VJS”、“CPP”或为类提供完全限定的名称 正在实现System.CodeDom.Compiler.CodeDomProvider。默认值 语言是“CS”(CSharp)。缩写为“/l:”
生成的代理或模板的命名空间。默认名称空间 是全局命名空间。缩写为“/n:”。1/namespace:
[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);
}
如果将来由于某种原因WSDL和生成的接口发生了更改,那么您可以轻松地将现有实现调整为新接口
一,。名称空间不是必需的,如果不添加该选项,则会在全局::
名称空间中生成接口,您需要在接口前面添加该名称空间。更改
[WebServiceBinding(Name=“CustomWSDL”,Location=“Service1.wsdl”)]
到
[WebServiceBinding(Name=“ServiceSoap”,Location=“Service1.wsdl”)]
请阅读提供的链接中的说明
“Name属性提供自定义WSDL中的的名称,ASP.NET需要它来允许它正确地向您的实现分派调用。”取消对该块的注释并更正该块作者要求注释该行,因为WSDL:端口是由它自己创建的错误显式声明绑定,那么,如果将其中一个绑定的名称更改为CustomWSDL,会发生什么情况呢?比如将“ServiceSoap12”切换到“CustomWSDL”?只是一个想法。我想要一个ASMX服务本身。我的应用程序被绑定到一个大型机上,这是过去3年的事情。我的客户不想要WCF中的服务。这就是我为什么这么麻烦的原因。我在试你的方法。当我运行wsdl/serverInterface/l:csharp custom.wsdl时,我从名称空间中得到的名为CustomWSDL的相同错误元素绑定丢失了,请重新检查一系列内容:您得到了一个interface.cs作为wsdl命令的输出?您确实在webproject上添加了现有文件,以便使用界面添加该文件吗?您确实用我提供的代码替换了服务类的代码(所以您去掉了该类上的所有属性)?因为当我尝试这个时,它确实起作用了…我添加了接口。但是当我尝试继承接口时,它不会出现在列表中。在接口前面添加any
global::
,或者在wsdl文件中添加/n选项以在您想要的名称空间中生成接口,如下所示:/n:any.Awesome.namespace.Here
并使用any.Awesome.Here添加一个代码>在您的实现文件中的usings处
wsdl /serverInterface /n:Any.AweSome.NameSpace /l:csharp custom.wsdl
[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);
}
// 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";
}
}