博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个WebService Demo
阅读量:6342 次
发布时间:2019-06-22

本文共 8447 字,大约阅读时间需要 28 分钟。

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 ///  7 ///MyMath 的摘要说明 8 ///  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来生成客户端代理类的代码:

//------------------------------------------------------------------------------// 
// 此代码由工具生成。// 运行时版本:4.0.30319.1//// 对此文件的更改可能会导致不正确的行为,并且如果// 重新生成代码,这些更改将会丢失。//
//------------------------------------------------------------------------------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。// ///
[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; ///
public MyMath() { this.Url = "http://localhost:7594/MathWebService/MyMath.asmx"; } ///
public event HelloWorldCompletedEventHandler HelloWorldCompleted; ///
public event AddCompletedEventHandler AddCompleted; ///
[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])); } ///
public System.IAsyncResult BeginHelloWorld(System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("HelloWorld", new object[0], callback, asyncState); } ///
public string EndHelloWorld(System.IAsyncResult asyncResult) { object[] results = this.EndInvoke(asyncResult); return ((string)(results[0])); } ///
public void HelloWorldAsync() { this.HelloWorldAsync(null); } ///
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)); } } ///
[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])); } ///
public System.IAsyncResult BeginAdd(int num1, int num2, System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("Add", new object[] { num1, num2}, callback, asyncState); } ///
public int EndAdd(System.IAsyncResult asyncResult) { object[] results = this.EndInvoke(asyncResult); return ((int)(results[0])); } ///
public void AddAsync(int num1, int num2) { this.AddAsync(num1, num2, null); } ///
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)); } } ///
public new void CancelAsync(object userState) { base.CancelAsync(userState); }}///
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]public delegate void HelloWorldCompletedEventHandler(object sender, HelloWorldCompletedEventArgs e);///
[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; } ///
public string Result { get { this.RaiseExceptionIfNecessary(); return ((string)(this.results[0])); } }}///
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]public delegate void AddCompletedEventHandler(object sender, AddCompletedEventArgs e);///
[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; } ///
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

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

以管理员执行下面命令:

 

设置目录浏览为启动。

6. 服务访问界面:

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

转载于:https://www.cnblogs.com/yhlx125/p/3690273.html

你可能感兴趣的文章
webstorm常用功能FTP,及常用快捷键
查看>>
eclipse html 打开方式
查看>>
[求助] win7 x64 封装 出现 Administrator.xxxxx 的问题
查看>>
人类投资经理再也无法击败电脑的时代终将到来了...
查看>>
一个最小手势库的实现
查看>>
HoloLens开发手记 - Vuforia开发概述 Vuforia development overview
查看>>
Android支付之支付宝封装类
查看>>
<亲测>CentOS中yum安装ffmpeg
查看>>
【分享】马化腾:产品设计与用户体验
查看>>
【机器学习PAI实践十】深度学习Caffe框架实现图像分类的模型训练
查看>>
全智慧的网络:思科十年来最具颠覆性的创新
查看>>
怎样将现有应用迁移到 VMware NSX
查看>>
赛门铁克收购以色列移动安全初创公司Skycure 旨在构建网络安全防御平台
查看>>
《Photoshop蒙版与合成(第2版)》目录—导读
查看>>
“最佳人气奖”出炉!4月27号,谁能拿到阿里聚安全算法挑战赛的桂冠?
查看>>
《网页美工设计Photoshop+Flash+Dreamweaver从入门到精通》——2.6 图层与图层样式...
查看>>
今天的学习
查看>>
面试必问之JVM原理
查看>>
mysql主主同步+Keepalived
查看>>
研究音频编解码要看什么书
查看>>