一个WebService Demo
1.建立一个Asp.net Web网站,添加新项Web服务MyMath.asmx。编写如下代码:
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.Services;
- /// <summary>
- ///MyMath 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
- // [System.Web.Script.Services.ScriptService]
- public class MyMath : System.Web.Services.WebService {
- public MyMath () {
- //如果使用设计的组件,请取消注释以下行
- //InitializeComponent();
- }
- [WebMethod]
- public string HelloWorld() {
- return "Hello World";
- }
- [WebMethod]
- public int Add(int num1, int num2)
- {
- return num1 + num2;
- }
- }
MyMath
2.使用wsdl.exe来生成客户端代理类的代码:
- //------------------------------------------------------------------------------
- // <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[]);
- return ((string)(results[]));
- }
- /// <remarks/>
- public System.IAsyncResult BeginHelloWorld(System.AsyncCallback callback, object asyncState) {
- return this.BeginInvoke("HelloWorld", new object[], callback, asyncState);
- }
- /// <remarks/>
- public string EndHelloWorld(System.IAsyncResult asyncResult) {
- object[] results = this.EndInvoke(asyncResult);
- return ((string)(results[]));
- }
- /// <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[], 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[]));
- }
- /// <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[]));
- }
- /// <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[]));
- }
- }
- }
- /// <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[]));
- }
- }
- }
调用代码:
- private void button1_Click(object sender, EventArgs e)
- {
- MyMath math = new MyMath();
- math.Url = "http://localhost:7594/MathWebService/MyMath.asmx";
- int a = math.Add(, );
- 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());
- }
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的更多相关文章
- LeadTools Android 入门教学——运行第一个Android Demo
LeadTools 有很多Windows平台下的Demo,非常全面,但是目前开发手机应用的趋势也越来越明显,LeadTools也给大家提供了10个Android的Demo,这篇文章将会教你如何运行第一 ...
- ArcGIS API for JavaScript开发环境搭建及第一个实例demo
原文:ArcGIS API for JavaScript开发环境搭建及第一个实例demo ESRI公司截止到目前已经发布了最新的ArcGIS Server for JavaScript API v3. ...
- 【Web学习日记】——在IIS上发布一个WebService
没有开发过程,只是发布过程 一.前提 开发使用的是VS2013 从来没有做过Web的发布,在网上找例子,看到的总是与自己的情况不相符,而且也有人提出了VS2013发布网站的问题,但解决方案却很少,好不 ...
- 如何使用PHP实现一个WebService
WSDL WSDL(网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及如何对它们进行访问.这种文 ...
- 模仿京东顶部搜索条效果制作的一个小demo
最近模仿京东顶部搜索条效果制作的一个小demo,特贴到这里,今后如果有用到可以参考一下,代码如下 #define kScreenWidth [UIScreen mainScreen].bounds.s ...
- 在IIS上发布一个WebService,再发布一个网站调用这个WebService(实例)
首先描述一下先决条件:IIS可用,VS2005可用. 好,现在开始: 首先写一个WebService并把它发布到IIS上: 在IIS上的默认网站下新建一个“虚拟目录”,取名为“webservice1” ...
- java线程间通信:一个小Demo完全搞懂
版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程系列文章只是自己知识的总结梳理,都是最基础的玩意,已经掌握熟练的可以绕过. 一.从一个小Demo说起 上篇我们聊到了Java多线程的同步 ...
- 一个数据源demo
前言 我们重复造轮子,不是为了证明我们比那些造轮子的人牛逼,而是明白那些造轮子的人有多牛逼. JDBC介绍 在JDBC中,我们可以通过DriverManager.getConnection()创建(而 ...
- 快速搭建一个直播Demo
缘由 最近帮朋友看一个直播网站的源码,发现这份直播源码借助 阿里云 .腾讯云这些大公司提供的SDK 可以非常方便的搭建一个直播网站.下面我们来给大家讲解下如何借助 腾讯云 我们搭建一个简易的 直播示例 ...
随机推荐
- $_ 与 $PSItem
PowerShell 3.0 中的$PSItem 此文章于2012年11月4日发表在PowershellPowershell小技巧并加以Powershell 3.0管道的标签 by Mooser Le ...
- 约跑APP测试报告
用户需求规格说明书URL:http://www.cnblogs.com/liquan/p/6071804.html 组长博客URL:http://www.cnblogs.com/liquan/ 代码g ...
- could not build module 'uikit'
今天 新建一个SingleView-APP 无法运行程序,在 AppDelegate.h中 第一行代码处报错. #import <UIKit/UIKit.h> /Users/wjw/Des ...
- php根据IP地址跳转对应的城市,淘宝REST api调用地址直接使用
<?php // 定义一个函数getIP() function getIP(){ global $ip; if (getenv("HTTP_CLIENT_IP")) { $i ...
- commandname+commandargument
(一) CommandName:其实可以设置成一种动作,比如Select,Update,Delete,等操作.就是说CommandName是确定他到底引发的是哪一事件,如果CommandName的名字 ...
- Bootstrap页面布局24 - BS旋转木马功能
代码: <div class='container-fluid'> <h3 class='page-header'>Bootstrap 旋转木马</h3> < ...
- Smarty 配置文件的读取
http://www.cnblogs.com/gbyukg/archive/2012/06/12/2539067.html
- w_all_checked - js -checkbox 多选、全选、submit、request
<!doctype html> <html> <head> <meta charset="UTF-8"> </head> ...
- Delphi下的OpenGL开发入门
unit Unit1; interface uses OpenGL,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls ...
- 【No.1 Ionic】基础环境配置
Node 安装 git clone https://github.com/nodejs/node cd node ./configure make sudo make install node -v ...