引用服务有修改,点右键更新
Web.config
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="microsoft.web.services3" type="Microsoft.Web.Services3.Configuration.WebServicesConfiguration, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </configSections> <system.web> <webServices> <soapExtensionImporterTypes> <add type="Microsoft.Web.Services3.Description.WseExtensionImporter, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </soapExtensionImporterTypes> <soapServerProtocolFactory type="Microsoft.Web.Services3.WseProtocolFactory, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </webServices> <compilation> <assemblies> <add assembly="Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies> </compilation> <customErrors mode="Off"></customErrors> </system.web> </configuration>
using System; using System.Data; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.ComponentModel; namespace SOAPService { /// <summary> /// Summary description for SOAPHeaderService /// https://learn.microsoft.com/zh-cn/dotnet/api/system.web.services.protocols.soaphttpclientprotocol.invoke?view=netframework-2.0 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(Name = "TestService", ConformsTo = WsiProfiles.BasicProfile1_1)] public class SOAPHeaderService : System.Web.Services.WebService { // Visual studio will append a "UserCredentialsValue" property to the proxy class public UserCredentials consumer; /// <summary> /// / /// </summary> public SOAPHeaderService() { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] [SoapDocumentMethod(Binding = "TestService")] [SoapHeader("consumer", Required = true)] public string GetBalance() { if (checkConsumer()) return consumer.userName + " had 10000000 credit,验证通过,涂聚文!"; else return "Error in authentication"; } /// <summary> /// /// </summary> /// <param name="branch"></param> /// <param name="projectid"></param> /// <returns></returns> [WebMethod] [SoapDocumentMethod(Binding = "TestService")] [SoapHeader("consumer", Required = true)] public string GetBalanceJson(string branch,string projectid) { if (checkConsumer()) return consumer.userName + " had 10000000 credit,验证通过,涂聚文!Json,店铺编号:"+branch; else return "Error in authentication"; } /// <summary> /// /// </summary> /// <returns></returns> private bool checkConsumer() { // In this method you can check the username and password // with your database or something // You could also encrypt the password for more security if (consumer != null) { if (consumer.userName == "geovindu" && consumer.password == "1234") return true; else return false; } else return false; } } # region "SOAP Headers" /// <summary> /// /// </summary> public class UserCredentials : System.Web.Services.Protocols.SoapHeader { public string userName; public string password; } # endregion }
SOAPHeaderService.wsdl
<?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" 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:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"> <s:element name="GetBalance"> <s:complexType /> </s:element> <s:element name="GetBalanceResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="GetBalanceResult" type="s:string" /> </s:sequence> </s:complexType> </s:element> <s:element name="UserCredentials" type="tns:UserCredentials" /> <s:complexType name="UserCredentials"> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="userName" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="password" type="s:string" /> </s:sequence> <s:anyAttribute /> </s:complexType> <s:element name="GetBalanceJson"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="branch" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="projectid" type="s:string" /> </s:sequence> </s:complexType> </s:element> <s:element name="GetBalanceJsonResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="GetBalanceJsonResult" type="s:string" /> </s:sequence> </s:complexType> </s:element> </s:schema> </wsdl:types> <wsdl:message name="GetBalanceSoapIn"> <wsdl:part name="parameters" element="tns:GetBalance" /> </wsdl:message> <wsdl:message name="GetBalanceSoapOut"> <wsdl:part name="parameters" element="tns:GetBalanceResponse" /> </wsdl:message> <wsdl:message name="GetBalanceUserCredentials"> <wsdl:part name="UserCredentials" element="tns:UserCredentials" /> </wsdl:message> <wsdl:message name="GetBalanceJsonSoapIn"> <wsdl:part name="parameters" element="tns:GetBalanceJson" /> </wsdl:message> <wsdl:message name="GetBalanceJsonSoapOut"> <wsdl:part name="parameters" element="tns:GetBalanceJsonResponse" /> </wsdl:message> <wsdl:message name="GetBalanceJsonUserCredentials"> <wsdl:part name="UserCredentials" element="tns:UserCredentials" /> </wsdl:message> <wsdl:portType name="TestService"> <wsdl:operation name="GetBalance"> <wsdl:input message="tns:GetBalanceSoapIn" /> <wsdl:output message="tns:GetBalanceSoapOut" /> </wsdl:operation> <wsdl:operation name="GetBalanceJson"> <wsdl:input message="tns:GetBalanceJsonSoapIn" /> <wsdl:output message="tns:GetBalanceJsonSoapOut" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="TestService" type="tns:TestService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="GetBalance"> <soap:operation soapAction="http://tempuri.org/GetBalance" style="document" /> <wsdl:input> <soap:body use="literal" /> <soap:header message="tns:GetBalanceUserCredentials" part="UserCredentials" use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> <wsdl:operation name="GetBalanceJson"> <soap:operation soapAction="http://tempuri.org/GetBalanceJson" style="document" /> <wsdl:input> <soap:body use="literal" /> <soap:header message="tns:GetBalanceJsonUserCredentials" part="UserCredentials" use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="TestService1" type="tns:TestService"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="GetBalance"> <soap12:operation soapAction="http://tempuri.org/GetBalance" style="document" /> <wsdl:input> <soap12:body use="literal" /> <soap12:header message="tns:GetBalanceUserCredentials" part="UserCredentials" use="literal" /> </wsdl:input> <wsdl:output> <soap12:body use="literal" /> </wsdl:output> </wsdl:operation> <wsdl:operation name="GetBalanceJson"> <soap12:operation soapAction="http://tempuri.org/GetBalanceJson" style="document" /> <wsdl:input> <soap12:body use="literal" /> <soap12:header message="tns:GetBalanceJsonUserCredentials" part="UserCredentials" use="literal" /> </wsdl:input> <wsdl:output> <soap12:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="SOAPHeaderService"> <wsdl:port name="TestService" binding="tns:TestService"> <soap:address location="http://localhost:2564/SOAPHeaderService.asmx" /> </wsdl:port> <wsdl:port name="TestService1" binding="tns:TestService1"> <soap12:address location="http://localhost:2564/SOAPHeaderService.asmx" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
//------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:2.0.50727.8964 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ // // 此源代码是由 Microsoft.VSDesigner 2.0.50727.8964 版自动生成。 // #pragma warning disable 1591 namespace Consumer.SOAPHeaderService { using System.Diagnostics; using System.Web.Services; using System.ComponentModel; using System.Web.Services.Protocols; using System; using System.Xml.Serialization; /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.7905")] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Web.Services.WebServiceBindingAttribute(Name="TestService", Namespace="http://tempuri.org/")] public partial class SOAPHeaderService : System.Web.Services.Protocols.SoapHttpClientProtocol { private UserCredentials userCredentialsValueField; private System.Threading.SendOrPostCallback GetBalanceOperationCompleted; private System.Threading.SendOrPostCallback GetBalanceJsonOperationCompleted; private bool useDefaultCredentialsSetExplicitly; /// <remarks/> public SOAPHeaderService() { this.Url = global::Consumer.Properties.Settings.Default.Consumer_SOAPHeaderService_SOAPHeaderService; if ((this.IsLocalFileSystemWebService(this.Url) == true)) { this.UseDefaultCredentials = true; this.useDefaultCredentialsSetExplicitly = false; } else { this.useDefaultCredentialsSetExplicitly = true; } } public UserCredentials UserCredentialsValue { get { return this.userCredentialsValueField; } set { this.userCredentialsValueField = value; } } public new string Url { get { return base.Url; } set { if ((((this.IsLocalFileSystemWebService(base.Url) == true) && (this.useDefaultCredentialsSetExplicitly == false)) && (this.IsLocalFileSystemWebService(value) == false))) { base.UseDefaultCredentials = false; } base.Url = value; } } public new bool UseDefaultCredentials { get { return base.UseDefaultCredentials; } set { base.UseDefaultCredentials = value; this.useDefaultCredentialsSetExplicitly = true; } } /// <remarks/> public event GetBalanceCompletedEventHandler GetBalanceCompleted; /// <remarks/> public event GetBalanceJsonCompletedEventHandler GetBalanceJsonCompleted; /// <remarks/> [System.Web.Services.Protocols.SoapHeaderAttribute("UserCredentialsValue")] [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetBalance", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] public string GetBalance() { object[] results = this.Invoke("GetBalance", new object[0]); return ((string)(results[0])); } /// <remarks/> public void GetBalanceAsync() { this.GetBalanceAsync(null); } /// <remarks/> public void GetBalanceAsync(object userState) { if ((this.GetBalanceOperationCompleted == null)) { this.GetBalanceOperationCompleted = new System.Threading.SendOrPostCallback(this.OnGetBalanceOperationCompleted); } this.InvokeAsync("GetBalance", new object[0], this.GetBalanceOperationCompleted, userState); } private void OnGetBalanceOperationCompleted(object arg) { if ((this.GetBalanceCompleted != null)) { System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg)); this.GetBalanceCompleted(this, new GetBalanceCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState)); } } /// <remarks/> [System.Web.Services.Protocols.SoapHeaderAttribute("UserCredentialsValue")] [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetBalanceJson", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] public string GetBalanceJson(string branch, string projectid) { object[] results = this.Invoke("GetBalanceJson", new object[] { branch, projectid}); return ((string)(results[0])); } /// <remarks/> public void GetBalanceJsonAsync(string branch, string projectid) { this.GetBalanceJsonAsync(branch, projectid, null); } /// <remarks/> public void GetBalanceJsonAsync(string branch, string projectid, object userState) { if ((this.GetBalanceJsonOperationCompleted == null)) { this.GetBalanceJsonOperationCompleted = new System.Threading.SendOrPostCallback(this.OnGetBalanceJsonOperationCompleted); } this.InvokeAsync("GetBalanceJson", new object[] { branch, projectid}, this.GetBalanceJsonOperationCompleted, userState); } private void OnGetBalanceJsonOperationCompleted(object arg) { if ((this.GetBalanceJsonCompleted != null)) { System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg)); this.GetBalanceJsonCompleted(this, new GetBalanceJsonCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState)); } } /// <remarks/> public new void CancelAsync(object userState) { base.CancelAsync(userState); } private bool IsLocalFileSystemWebService(string url) { if (((url == null) || (url == string.Empty))) { return false; } System.Uri wsUri = new System.Uri(url); if (((wsUri.Port >= 1024) && (string.Compare(wsUri.Host, "localHost", System.StringComparison.OrdinalIgnoreCase) == 0))) { return true; } return false; } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.8773")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")] [System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/", IsNullable=false)] public partial class UserCredentials : System.Web.Services.Protocols.SoapHeader { private string userNameField; private string passwordField; private System.Xml.XmlAttribute[] anyAttrField; /// <remarks/> public string userName { get { return this.userNameField; } set { this.userNameField = value; } } /// <remarks/> public string password { get { return this.passwordField; } set { this.passwordField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAnyAttributeAttribute()] public System.Xml.XmlAttribute[] AnyAttr { get { return this.anyAttrField; } set { this.anyAttrField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.7905")] public delegate void GetBalanceCompletedEventHandler(object sender, GetBalanceCompletedEventArgs e); /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.7905")] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] public partial class GetBalanceCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs { private object[] results; internal GetBalanceCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : base(exception, cancelled, userState) { this.results = results; } /// <remarks/> public string Result { get { this.RaiseExceptionIfNecessary(); return ((string)(this.results[0])); } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.7905")] public delegate void GetBalanceJsonCompletedEventHandler(object sender, GetBalanceJsonCompletedEventArgs e); /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.7905")] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] public partial class GetBalanceJsonCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs { private object[] results; internal GetBalanceJsonCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : base(exception, cancelled, userState) { this.results = results; } /// <remarks/> public string Result { get { this.RaiseExceptionIfNecessary(); return ((string)(this.results[0])); } } } } #pragma warning restore 1591
调用:
SOAPHeaderService.SOAPHeaderService service = new SOAPHeaderService.SOAPHeaderService(); SOAPHeaderService.UserCredentials user = new SOAPHeaderService.UserCredentials(); user.userName = "geovindu"; user.password = "1234"; service.UserCredentialsValue = user; Console.WriteLine(service.GetBalance()); Console.WriteLine(); user.userName = "geovindu"; user.password = "1234"; service.UserCredentialsValue = user; //object[] ob=new object[2]; //参数问题 string s = service.GetBalanceJson("ts5", "1"); Console.WriteLine(service.GetBalanceJson("ts5","1")); Console.WriteLine();
标签:Web,string,object,System,SOAP,CSharp,Services,WSDL,public From: https://www.cnblogs.com/geovindu/p/17066338.html