一个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 可以非常方便的搭建一个直播网站.下面我们来给大家讲解下如何借助 腾讯云 我们搭建一个简易的 直播示例 ...
随机推荐
- MySQL 授权远程登录(Ubuntu 环境)
环境:Ubuntu 13.10 (GNU/Linux 3.11.0-12-generic i686) 在用 Navicat 连接远程数据库时报错: ERROR (HY000): Host *** is ...
- cURL 学习笔记与总结(1)概念
概念: cURL(Client URL Library Functions)is a command line tool for transfering data with URL syntax(使用 ...
- Redis学习手册(Key操作命令)
一.概述: 在该系列的前几篇博客中,主要讲述的是与Redis数据类型相关的命令,如String.List.Set.Hashes和Sorted-Set.这些命 令都具有一个共同点,即所有的操作都是针对与 ...
- Bootstrap页面布局13 - BS按钮
bootstrap中的按钮类 一般可以作为按钮的标签有:<a></a> <button></button> <input type='butt ...
- linux下常用的命令
一. tomcat tail -f ../logs/catalina.out 最新更新的日志(tomcat) cat ../logs/catalina ...
- Listener-监听器+ServletContext+ApplicationContext
参考资料 ServletContext和ApplicationContext有什么区别 ServletContext:是web容器的东西, 一个webapp一个, 比session作用范围要大, 从中 ...
- mysql中文字段转拼音首字母,以及中文拼音模糊查询
创建存储过程,将中文字段转拼音首字母 CREATE DEFINER=`root`@`%` FUNCTION `fristPinyin`(P_NAME VARCHAR(255)) RETURNS var ...
- BLE协议栈及传统蓝牙协议栈对比图
1. BLE协议栈的层次图如下: 主机控制接口层: 为主机和控制器之间提供标准通信接口 逻辑链路控制及自适应协议层: 为上层提供数据封装服务 安全管理层: 定义配对和密钥分配方式,为协议栈其他层与另一 ...
- JavaSE的知识
一 SE的知识体系: java基础: 一基础语法 8个基本数据类型-->8个包装类 数据类型转换: 自动转换(从小到大) 强制转换(从大到小) 注意:int 和char 分支与判断: if(){ ...
- SQL GUID去除横线,并转换为小写
SELECT NEWID() --SQL GUID去除横线,并转换为小写,得到一个以 数字 和 字母 组合的 长度为32字节 的随机字符串 SELECT LOWER(REPLACE(LTRIM(NEW ...