本文主要是介绍ASP. NET中的webservice,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在分布式计算中经常要用到WebService,为了更好的学习WebService,写此文供自己记忆。
编译环境:VS2008
在项目中添加一个名称为MyWebService.asmx的Web服务,App_Code文件夹下会新增一个MyWebService.cs文件,编写代码如下:
- using System;
- using System.Collections;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Xml.Linq;
- /// <summary>
- ///MyWebService 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
- // [System.Web.Script.Services.ScriptService]
- public class MyWebService : System.Web.Services.WebService {
- public MyWebService () {
- //如果使用设计的组件,请取消注释以下行
- //InitializeComponent();
- }
- [WebMethod]
- public string HelloWorld() {
- return "Hello World";
- }
- }
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;/// <summary>
///MyWebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService {public MyWebService () {//如果使用设计的组件,请取消注释以下行 //InitializeComponent(); }[WebMethod]public string HelloWorld() {return "Hello World";}}
写好WebService以后,在项目上右键选择“添加Web引用”,可以给WebService起一个命名空间localhost。
添加Web引用成功如下图所示:
下面我们写客户端代码
前端代码:
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="Button1" runat="server" Text="同步调用WebService" />
- <asp:Button ID="Button2" runat="server" Text="异步调用WebService" />
- <asp:Button ID="Button3" runat="server" Text="另一种异步调用WebService" />
- </div>
- </form>
<form id="form1" runat="server"><div><asp:Button ID="Button1" runat="server" Text="同步调用WebService" /><asp:Button ID="Button2" runat="server" Text="异步调用WebService" /><asp:Button ID="Button3" runat="server" Text="另一种异步调用WebService" /></div>
</form>
后台代码:
- using System;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Button1.Click += new EventHandler(Button1_Click);
- Button2.Click += new EventHandler(Button2_Click);
- Button3.Click += new EventHandler(Button3_Click);
- }
- #region 同步调用
- void Button1_Click(object sender, EventArgs e)
- {
- localhost.MyWebService mws = new localhost.MyWebService();
- string s = mws.HelloWorld();
- Response.Write("<script>alert('"+s+"')</script>");
- //Response.End();
- }
- #endregion
- #region 异步调用
- void Button2_Click(object sender, EventArgs e)
- {
- localhost.MyWebService AsyMWS = new localhost.MyWebService();
- IAsyncResult asyncResult = AsyMWS.BeginHelloWorld(new AsyncCallback(CallbackFunc), "AsyncState");
- string s = AsyMWS.EndHelloWorld(asyncResult);
- Response.Write("<script>alert('" + s + "')</script>");
- }
- void CallbackFunc(IAsyncResult r)
- {
- Response.Write("<script>alert('异步调用完成!')</script>");
- }
- #endregion
- #region 另一种异步调用
- void Button3_Click(object sender, EventArgs e)
- {
- localhost.MyWebService AsyMWS = new localhost.MyWebService();
- AsyMWS.HelloWorldCompleted += new localhost.HelloWorldCompletedEventHandler(AsyMWS_HelloWorldCompleted);
- AsyMWS.HelloWorldAsync();
- }
- void AsyMWS_HelloWorldCompleted(object sender, localhost.HelloWorldCompletedEventArgs e)
- {
- string s = e.Result;
- Response.Write("<script>alert('"+s+"')</script>");
- }
- #endregion
- }
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){ Button1.Click += new EventHandler(Button1_Click);Button2.Click += new EventHandler(Button2_Click);Button3.Click += new EventHandler(Button3_Click);}#region 同步调用void Button1_Click(object sender, EventArgs e){localhost.MyWebService mws = new localhost.MyWebService();string s = mws.HelloWorld();Response.Write("<script>alert('"+s+"')</script>");//Response.End();}#endregion#region 异步调用void Button2_Click(object sender, EventArgs e){localhost.MyWebService AsyMWS = new localhost.MyWebService();IAsyncResult asyncResult = AsyMWS.BeginHelloWorld(new AsyncCallback(CallbackFunc), "AsyncState");string s = AsyMWS.EndHelloWorld(asyncResult);Response.Write("<script>alert('" + s + "')</script>");}void CallbackFunc(IAsyncResult r){Response.Write("<script>alert('异步调用完成!')</script>");}#endregion#region 另一种异步调用void Button3_Click(object sender, EventArgs e){localhost.MyWebService AsyMWS = new localhost.MyWebService();AsyMWS.HelloWorldCompleted += new localhost.HelloWorldCompletedEventHandler(AsyMWS_HelloWorldCompleted);AsyMWS.HelloWorldAsync();}void AsyMWS_HelloWorldCompleted(object sender, localhost.HelloWorldCompletedEventArgs e){string s = e.Result;Response.Write("<script>alert('"+s+"')</script>");}#endregion
}
注:若要使用最后一种异步调用方式需要在前端代码中写入Async="true",如下图
这篇关于ASP. NET中的webservice的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!