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来生成客户端代理类的代码:

  1. //------------------------------------------------------------------------------
  2. // <auto-generated>
  3. // 此代码由工具生成。
  4. // 运行时版本:4.0.30319.1
  5. //
  6. // 对此文件的更改可能会导致不正确的行为,并且如果
  7. // 重新生成代码,这些更改将会丢失。
  8. // </auto-generated>
  9. //------------------------------------------------------------------------------
  10.  
  11. using System;
  12. using System.ComponentModel;
  13. using System.Diagnostics;
  14. using System.Web.Services;
  15. using System.Web.Services.Protocols;
  16. using System.Xml.Serialization;
  17.  
  18. //
  19. // 此源代码由 wsdl 自动生成, Version=4.0.30319.1。
  20. //
  21.  
  22. /// <remarks/>
  23. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
  24. [System.Diagnostics.DebuggerStepThroughAttribute()]
  25. [System.ComponentModel.DesignerCategoryAttribute("code")]
  26. [System.Web.Services.WebServiceBindingAttribute(Name="MyMathSoap", Namespace="http://tempuri.org/")]
  27. public partial class MyMath : System.Web.Services.Protocols.SoapHttpClientProtocol {
  28.  
  29. private System.Threading.SendOrPostCallback HelloWorldOperationCompleted;
  30.  
  31. private System.Threading.SendOrPostCallback AddOperationCompleted;
  32.  
  33. /// <remarks/>
  34. public MyMath() {
  35. this.Url = "http://localhost:7594/MathWebService/MyMath.asmx";
  36. }
  37.  
  38. /// <remarks/>
  39. public event HelloWorldCompletedEventHandler HelloWorldCompleted;
  40.  
  41. /// <remarks/>
  42. public event AddCompletedEventHandler AddCompleted;
  43.  
  44. /// <remarks/>
  45. [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)]
  46. public string HelloWorld() {
  47. object[] results = this.Invoke("HelloWorld", new object[]);
  48. return ((string)(results[]));
  49. }
  50.  
  51. /// <remarks/>
  52. public System.IAsyncResult BeginHelloWorld(System.AsyncCallback callback, object asyncState) {
  53. return this.BeginInvoke("HelloWorld", new object[], callback, asyncState);
  54. }
  55.  
  56. /// <remarks/>
  57. public string EndHelloWorld(System.IAsyncResult asyncResult) {
  58. object[] results = this.EndInvoke(asyncResult);
  59. return ((string)(results[]));
  60. }
  61.  
  62. /// <remarks/>
  63. public void HelloWorldAsync() {
  64. this.HelloWorldAsync(null);
  65. }
  66.  
  67. /// <remarks/>
  68. public void HelloWorldAsync(object userState) {
  69. if ((this.HelloWorldOperationCompleted == null)) {
  70. this.HelloWorldOperationCompleted = new System.Threading.SendOrPostCallback(this.OnHelloWorldOperationCompleted);
  71. }
  72. this.InvokeAsync("HelloWorld", new object[], this.HelloWorldOperationCompleted, userState);
  73. }
  74.  
  75. private void OnHelloWorldOperationCompleted(object arg) {
  76. if ((this.HelloWorldCompleted != null)) {
  77. System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
  78. this.HelloWorldCompleted(this, new HelloWorldCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
  79. }
  80. }
  81.  
  82. /// <remarks/>
  83. [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)]
  84. public int Add(int num1, int num2) {
  85. object[] results = this.Invoke("Add", new object[] {
  86. num1,
  87. num2});
  88. return ((int)(results[]));
  89. }
  90.  
  91. /// <remarks/>
  92. public System.IAsyncResult BeginAdd(int num1, int num2, System.AsyncCallback callback, object asyncState) {
  93. return this.BeginInvoke("Add", new object[] {
  94. num1,
  95. num2}, callback, asyncState);
  96. }
  97.  
  98. /// <remarks/>
  99. public int EndAdd(System.IAsyncResult asyncResult) {
  100. object[] results = this.EndInvoke(asyncResult);
  101. return ((int)(results[]));
  102. }
  103.  
  104. /// <remarks/>
  105. public void AddAsync(int num1, int num2) {
  106. this.AddAsync(num1, num2, null);
  107. }
  108.  
  109. /// <remarks/>
  110. public void AddAsync(int num1, int num2, object userState) {
  111. if ((this.AddOperationCompleted == null)) {
  112. this.AddOperationCompleted = new System.Threading.SendOrPostCallback(this.OnAddOperationCompleted);
  113. }
  114. this.InvokeAsync("Add", new object[] {
  115. num1,
  116. num2}, this.AddOperationCompleted, userState);
  117. }
  118.  
  119. private void OnAddOperationCompleted(object arg) {
  120. if ((this.AddCompleted != null)) {
  121. System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
  122. this.AddCompleted(this, new AddCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
  123. }
  124. }
  125.  
  126. /// <remarks/>
  127. public new void CancelAsync(object userState) {
  128. base.CancelAsync(userState);
  129. }
  130. }
  131.  
  132. /// <remarks/>
  133. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
  134. public delegate void HelloWorldCompletedEventHandler(object sender, HelloWorldCompletedEventArgs e);
  135.  
  136. /// <remarks/>
  137. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
  138. [System.Diagnostics.DebuggerStepThroughAttribute()]
  139. [System.ComponentModel.DesignerCategoryAttribute("code")]
  140. public partial class HelloWorldCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
  141.  
  142. private object[] results;
  143.  
  144. internal HelloWorldCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
  145. base(exception, cancelled, userState) {
  146. this.results = results;
  147. }
  148.  
  149. /// <remarks/>
  150. public string Result {
  151. get {
  152. this.RaiseExceptionIfNecessary();
  153. return ((string)(this.results[]));
  154. }
  155. }
  156. }
  157.  
  158. /// <remarks/>
  159. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
  160. public delegate void AddCompletedEventHandler(object sender, AddCompletedEventArgs e);
  161.  
  162. /// <remarks/>
  163. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
  164. [System.Diagnostics.DebuggerStepThroughAttribute()]
  165. [System.ComponentModel.DesignerCategoryAttribute("code")]
  166. public partial class AddCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
  167.  
  168. private object[] results;
  169.  
  170. internal AddCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
  171. base(exception, cancelled, userState) {
  172. this.results = results;
  173. }
  174.  
  175. /// <remarks/>
  176. public int Result {
  177. get {
  178. this.RaiseExceptionIfNecessary();
  179. return ((int)(this.results[]));
  180. }
  181. }
  182. }

调用代码:

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. MyMath math = new MyMath();
  4. math.Url = "http://localhost:7594/MathWebService/MyMath.asmx";
  5. int a = math.Add(, );
  6. MessageBox.Show(a.ToString());
  7. }
  8.  
  9. private void button2_Click(object sender, EventArgs e)
  10. {
  11. MyMath math = new MyMath();
  12. math.Url = "http://localhost:7594/MathWebService/MyMath.asmx";
  13. string a = math.HelloWorld();
  14. MessageBox.Show(a.ToString());
  15. }

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

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

HTTP 错误 500.21 - Internal Server Error

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

HTTP 错误 404.17 - Not Found

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

以管理员执行下面命令:

设置目录浏览为启动。

6. 服务访问界面:

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

一个WebService Demo的更多相关文章

  1. LeadTools Android 入门教学——运行第一个Android Demo

    LeadTools 有很多Windows平台下的Demo,非常全面,但是目前开发手机应用的趋势也越来越明显,LeadTools也给大家提供了10个Android的Demo,这篇文章将会教你如何运行第一 ...

  2. ArcGIS API for JavaScript开发环境搭建及第一个实例demo

    原文:ArcGIS API for JavaScript开发环境搭建及第一个实例demo ESRI公司截止到目前已经发布了最新的ArcGIS Server for JavaScript API v3. ...

  3. 【Web学习日记】——在IIS上发布一个WebService

    没有开发过程,只是发布过程 一.前提 开发使用的是VS2013 从来没有做过Web的发布,在网上找例子,看到的总是与自己的情况不相符,而且也有人提出了VS2013发布网站的问题,但解决方案却很少,好不 ...

  4. 如何使用PHP实现一个WebService

    WSDL WSDL(网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及如何对它们进行访问.这种文 ...

  5. 模仿京东顶部搜索条效果制作的一个小demo

    最近模仿京东顶部搜索条效果制作的一个小demo,特贴到这里,今后如果有用到可以参考一下,代码如下 #define kScreenWidth [UIScreen mainScreen].bounds.s ...

  6. 在IIS上发布一个WebService,再发布一个网站调用这个WebService(实例)

    首先描述一下先决条件:IIS可用,VS2005可用. 好,现在开始: 首先写一个WebService并把它发布到IIS上: 在IIS上的默认网站下新建一个“虚拟目录”,取名为“webservice1” ...

  7. java线程间通信:一个小Demo完全搞懂

    版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程系列文章只是自己知识的总结梳理,都是最基础的玩意,已经掌握熟练的可以绕过. 一.从一个小Demo说起 上篇我们聊到了Java多线程的同步 ...

  8. 一个数据源demo

    前言 我们重复造轮子,不是为了证明我们比那些造轮子的人牛逼,而是明白那些造轮子的人有多牛逼. JDBC介绍 在JDBC中,我们可以通过DriverManager.getConnection()创建(而 ...

  9. 快速搭建一个直播Demo

    缘由 最近帮朋友看一个直播网站的源码,发现这份直播源码借助 阿里云 .腾讯云这些大公司提供的SDK 可以非常方便的搭建一个直播网站.下面我们来给大家讲解下如何借助 腾讯云 我们搭建一个简易的 直播示例 ...

随机推荐

  1. $_ 与 $PSItem

    PowerShell 3.0 中的$PSItem 此文章于2012年11月4日发表在PowershellPowershell小技巧并加以Powershell 3.0管道的标签 by Mooser Le ...

  2. 约跑APP测试报告

    用户需求规格说明书URL:http://www.cnblogs.com/liquan/p/6071804.html 组长博客URL:http://www.cnblogs.com/liquan/ 代码g ...

  3. could not build module 'uikit'

    今天 新建一个SingleView-APP 无法运行程序,在 AppDelegate.h中 第一行代码处报错. #import <UIKit/UIKit.h> /Users/wjw/Des ...

  4. php根据IP地址跳转对应的城市,淘宝REST api调用地址直接使用

    <?php // 定义一个函数getIP() function getIP(){ global $ip; if (getenv("HTTP_CLIENT_IP")) { $i ...

  5. commandname+commandargument

    (一) CommandName:其实可以设置成一种动作,比如Select,Update,Delete,等操作.就是说CommandName是确定他到底引发的是哪一事件,如果CommandName的名字 ...

  6. Bootstrap页面布局24 - BS旋转木马功能

    代码: <div class='container-fluid'> <h3 class='page-header'>Bootstrap 旋转木马</h3> < ...

  7. Smarty 配置文件的读取

    http://www.cnblogs.com/gbyukg/archive/2012/06/12/2539067.html

  8. w_all_checked - js -checkbox 多选、全选、submit、request

    <!doctype html> <html> <head> <meta charset="UTF-8"> </head> ...

  9. Delphi下的OpenGL开发入门

    unit Unit1; interface uses OpenGL,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls ...

  10. 【No.1 Ionic】基础环境配置

    Node 安装 git clone https://github.com/nodejs/node cd node ./configure make sudo make install node -v ...