一个WebService Demo

1.建立一个Asp.net Web网站,添加新项Web服务MyMath.asmx。编写如下代码:

 1 using System;  2 using System.Collections.Generic;  3 using System.Web;  4 using System.Web.Services;  5 

 6 /// <summary>

 7 ///MyMath 的摘要说明  8 /// </summary>

 9 [WebService(Namespace = "http://tempuri.org/")] 10 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 11 //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 12 // [System.Web.Script.Services.ScriptService]

13 public class MyMath : System.Web.Services.WebService { 14 

15     public MyMath () { 16 

17         //如果使用设计的组件,请取消注释以下行 18         //InitializeComponent(); 

19  } 20 

21  [WebMethod] 22     public string HelloWorld() { 23         return "Hello World"; 24  } 25  [WebMethod] 26     public int Add(int num1, int num2) 27  { 28         return num1 + num2; 29  } 30 }
MyMath

2.使用wsdl.exe来生成客户端代理类的代码:

一个WebService Demo

//------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:4.0.30319.1 //

// 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------



using System; using System.ComponentModel; using System.Diagnostics; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Serialization; // 

// 此源代码由 wsdl 自动生成, Version=4.0.30319.1。 // 





/// <remarks/>

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Web.Services.WebServiceBindingAttribute(Name="MyMathSoap", Namespace="http://tempuri.org/")] public partial class MyMath : System.Web.Services.Protocols.SoapHttpClientProtocol { private System.Threading.SendOrPostCallback HelloWorldOperationCompleted; private System.Threading.SendOrPostCallback AddOperationCompleted; /// <remarks/>

    public MyMath() { this.Url = "http://localhost:7594/MathWebService/MyMath.asmx"; } /// <remarks/>

    public event HelloWorldCompletedEventHandler HelloWorldCompleted; /// <remarks/>

    public event AddCompletedEventHandler AddCompleted; /// <remarks/>

    [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)] public string HelloWorld() { object[] results = this.Invoke("HelloWorld", new object[0]); return ((string)(results[0])); } /// <remarks/>

    public System.IAsyncResult BeginHelloWorld(System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("HelloWorld", new object[0], callback, asyncState); } /// <remarks/>

    public string EndHelloWorld(System.IAsyncResult asyncResult) { object[] results = this.EndInvoke(asyncResult); return ((string)(results[0])); } /// <remarks/>

    public void HelloWorldAsync() { this.HelloWorldAsync(null); } /// <remarks/>

    public void HelloWorldAsync(object userState) { if ((this.HelloWorldOperationCompleted == null)) { this.HelloWorldOperationCompleted = new System.Threading.SendOrPostCallback(this.OnHelloWorldOperationCompleted); } this.InvokeAsync("HelloWorld", new object[0], this.HelloWorldOperationCompleted, userState); } private void OnHelloWorldOperationCompleted(object arg) { if ((this.HelloWorldCompleted != null)) { System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg)); this.HelloWorldCompleted(this, new HelloWorldCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState)); } } /// <remarks/>

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Add", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] public int Add(int num1, int num2) { object[] results = this.Invoke("Add", new object[] { num1, num2}); return ((int)(results[0])); } /// <remarks/>

    public System.IAsyncResult BeginAdd(int num1, int num2, System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("Add", new object[] { num1, num2}, callback, asyncState); } /// <remarks/>

    public int EndAdd(System.IAsyncResult asyncResult) { object[] results = this.EndInvoke(asyncResult); return ((int)(results[0])); } /// <remarks/>

    public void AddAsync(int num1, int num2) { this.AddAsync(num1, num2, null); } /// <remarks/>

    public void AddAsync(int num1, int num2, object userState) { if ((this.AddOperationCompleted == null)) { this.AddOperationCompleted = new System.Threading.SendOrPostCallback(this.OnAddOperationCompleted); } this.InvokeAsync("Add", new object[] { num1, num2}, this.AddOperationCompleted, userState); } private void OnAddOperationCompleted(object arg) { if ((this.AddCompleted != null)) { System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg)); this.AddCompleted(this, new AddCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState)); } } /// <remarks/>

    public new void CancelAsync(object userState) { base.CancelAsync(userState); } } /// <remarks/>

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")] public delegate void HelloWorldCompletedEventHandler(object sender, HelloWorldCompletedEventArgs e); /// <remarks/>

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] public partial class HelloWorldCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs { private object[] results; internal HelloWorldCompletedEventArgs(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("wsdl", "4.0.30319.1")] public delegate void AddCompletedEventHandler(object sender, AddCompletedEventArgs e); /// <remarks/>

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] public partial class AddCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs { private object[] results; internal AddCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : base(exception, cancelled, userState) { this.results = results; } /// <remarks/>

    public int Result { get { this.RaiseExceptionIfNecessary(); return ((int)(this.results[0])); } } }
View Code

调用代码:

 private void button1_Click(object sender, EventArgs e) { MyMath math = new MyMath(); math.Url = "http://localhost:7594/MathWebService/MyMath.asmx"; int a = math.Add(10, 100); MessageBox.Show(a.ToString()); } private void button2_Click(object sender, EventArgs e) { MyMath math = new MyMath(); math.Url = "http://localhost:7594/MathWebService/MyMath.asmx"; string a = math.HelloWorld(); MessageBox.Show(a.ToString()); }
View Code

3.设置网站属性,生成网站,发布网站.

4.发表服务过程中报错误:

HTTP 错误 500.21 - Internal Server Error

处理程序“WebServiceHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

HTTP 错误 404.17 - Not Found

请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。

以管理员执行下面命令:

一个WebService Demo

 

设置目录浏览为启动。

6. 服务访问界面:

一个WebService Demo

查资料发现的一个查询天气服务:http://www.webservicex.net/globalweather.asmx

你可能感兴趣的:(webservice)