标签:
using System.Collections.Generic; using System.Web.Services; namespace 提供WebService服务 { /// <summary> /// ComplexCalculate 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class ComplexCalculate : System.Web.Services.WebService { [WebMethod]//默认的WebServices服务 public string HelloWorld() { return "Hello World"; } //假设这是京东提供外部应用程序 访问自己数据的服务 //根据用户搜索关键字 返回检索结果 private readonly Dictionary<string, string> dictionary = new Dictionary<string, string>(); [WebMethod]//特性表明该方法可供外部访问 public string SearchProduct(string searchKey) { dictionary.Add("mx2", "联通MX2 京东"); dictionary.Add("小米2", "小米四核 京东");//模拟数据 dictionary.Add("MeiZu", "MeiZu标准本 京东"); return dictionary[searchKey]; } } }
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="ComplexCalculateSoap" /> <binding name="NeedValidateWsSoap" /> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:1808/ComplexCalculate.asmx" binding="basicHttpBinding" bindingConfiguration="ComplexCalculateSoap" contract="SimpleUse.ComplexCalculateSoap" name="ComplexCalculateSoap" /> <endpoint address="http://localhost:1808/NeedValidateWS.asmx" binding="basicHttpBinding" bindingConfiguration="NeedValidateWsSoap" contract="AuthenticationPage.NeedValidateWsSoap" name="NeedValidateWsSoap" /> </client> </system.serviceModel> </configuration>
using System; using 调用WebServices服务.SimpleUse; namespace 调用WebServices服务 { public partial class SimpleUsePage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //同步调用 using(SimpleUse.ComplexCalculateSoapClient client = new ComplexCalculateSoapClient()) { Response.Write(client.SearchProduct("mx2")); } } } }
using System; using 调用WebServices服务.SimpleUse; namespace 调用WebServices服务 { public partial class UseAsync : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { using(SimpleUse.ComplexCalculateSoapClient client = new ComplexCalculateSoapClient()) { client.SearchProductCompleted += client_SearchProductCompleted; //委托 接收异步响应结果 } } void client_SearchProductCompleted(object sender, SearchProductCompletedEventArgs e) { Response.Write(e.Result);//输出异步返回结果 } } }
需要注意的是UseAsync.aspx页面需要添加Async=true;允许接收异步的配置
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UseAsync.aspx.cs" Async="true" Inherits="调用WebServices服务.UseAsync" %>
using System; using System.ComponentModel; using 调用WebServices服务.SimpleUse; namespace 调用WebServices服务 { public partial class AsyncByWorker : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { BackgroundWorker background = new BackgroundWorker(); //设置需要异步执行的操作 background.DoWork += background_DoWork; //接收异步操作结果的"回调函数" background.RunWorkerCompleted += background_RunWorkerCompleted; //开始真正的执行异步操作 不要遗漏 background.RunWorkerAsync(); } void background_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { Response.Write(e.Result);//从Result中获取结果 } void background_DoWork(object sender, DoWorkEventArgs e) { using(SimpleUse.ComplexCalculateSoapClient client = new ComplexCalculateSoapClient()) { e.Result = client.SearchProduct("mx2");//执行结果写入Result中 } } } }
using System.Web.Services.Protocols; namespace 提供WebService服务 { public class MySelfSoapHeader : SoapHeader { private string _name; private string _pwd; public string Name {get {return _name;}set {_name = value;}} public string Pwd {get {return _pwd;}set {_pwd = value;} } private bool ValidaResult(string name, string pwd) { if(name == "pizi" && pwd == "yimao") { return true; } return false; } public bool GetResult() { return ValidaResult(_name, _pwd); } } }
using System.Web.Services; using System.Web.Services.Protocols; namespace 提供WebService服务 { /// <summary> /// NeedValidateWS 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class NeedValidateWs : System.Web.Services.WebService { public MySelfSoapHeader MySoapHeader = null;//这里是通过客户端实例化的 不要new [SoapHeader("MySoapHeader")] [WebMethod(Description = "需要验证的方法")] public string IsValid() { if(MySoapHeader.GetResult()) { return "验证通过"; } return "未通过验证"; } } }
using System; using 调用WebServices服务.AuthenticationPage; namespace 调用WebServices服务 { public partial class ValidPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { using(AuthenticationPage.NeedValidateWsSoapClient client = new NeedValidateWsSoapClient()) { MySelfSoapHeader header = new MySelfSoapHeader(); header.Name = "pizi"; header.Pwd = "yimao";//验证通过 //header.Pwd = "sanmao";//未通过验证 Response.Write(client.IsValid(header)); } } } }
解决了三个问题:1.接口的自我描述;2.采用Http协议等常规协议,不用写原始的Socket;3.基于Web服务器,不占用80端口之外的端口.
标签:
原文地址:http://www.cnblogs.com/shouce/p/5376931.html