VS2008中C#开发webservice简单实例
1.创建工程
文件-> 新建->网站 如下图。

工程建好后,会自动添加如下代码:
using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq; [WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () { //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public string HelloWorld() {
return "Hello World";
} }
可以运行一遍看看效果。
2.添加代码增强webservice的功能
增加加减乘除的功能。
代码如下:
using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq; [WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () { //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} //[WebMethod]
//public string HelloWorld()
//{
// return "Hello World";
//} [WebMethod(Description = "求和的方法")]
public double addition(double i, double j)
{
return i + j;
} [WebMethod(Description = "求差的方法")]
public double subtract(double i, double j)
{
return i - j;
} [WebMethod(Description = "求积的方法")]
public double mutiplication(double i, double j)
{
return i * j;
} [WebMethod(Description = "求商的方法")]
public double division(double i, double j)
{
if(j!=)
return i/j;
else
return ;
}
}
运行效果如下:

在这个URL后面添加?wsdl就可以获取该webservice的wsdl。

3.使用生成的webservice
VS2008-> 文件->新建->网站->ASP.NET网站->website2

接下来添加刚才生成的webservice应用:
website2邮右键->添加web引用

URL是运行website1之后的网址(在使用刚才的webservice时,需要先把那个服务运行起来才行)
URL写好后,点击前往->添加应用->ok。
引入的web引用中有一个wsdl文件(此处对wsdl的提示与本文无关系)。wsdl文件如下:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="addition">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
<s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="additionResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="additionResult" type="s:double" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="subtract">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
<s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="subtractResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="subtractResult" type="s:double" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="mutiplication">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
<s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="mutiplicationResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="mutiplicationResult" type="s:double" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="division">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
<s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="divisionResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="divisionResult" type="s:double" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="additionSoapIn">
<wsdl:part name="parameters" element="tns:addition" />
</wsdl:message>
<wsdl:message name="additionSoapOut">
<wsdl:part name="parameters" element="tns:additionResponse" />
</wsdl:message>
<wsdl:message name="subtractSoapIn">
<wsdl:part name="parameters" element="tns:subtract" />
</wsdl:message>
<wsdl:message name="subtractSoapOut">
<wsdl:part name="parameters" element="tns:subtractResponse" />
</wsdl:message>
<wsdl:message name="mutiplicationSoapIn">
<wsdl:part name="parameters" element="tns:mutiplication" />
</wsdl:message>
<wsdl:message name="mutiplicationSoapOut">
<wsdl:part name="parameters" element="tns:mutiplicationResponse" />
</wsdl:message>
<wsdl:message name="divisionSoapIn">
<wsdl:part name="parameters" element="tns:division" />
</wsdl:message>
<wsdl:message name="divisionSoapOut">
<wsdl:part name="parameters" element="tns:divisionResponse" />
</wsdl:message>
<wsdl:portType name="ServiceSoap">
<wsdl:operation name="addition">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求和的方法</wsdl:documentation>
<wsdl:input message="tns:additionSoapIn" />
<wsdl:output message="tns:additionSoapOut" />
</wsdl:operation>
<wsdl:operation name="subtract">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求插的方法</wsdl:documentation>
<wsdl:input message="tns:subtractSoapIn" />
<wsdl:output message="tns:subtractSoapOut" />
</wsdl:operation>
<wsdl:operation name="mutiplication">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求积的方法</wsdl:documentation>
<wsdl:input message="tns:mutiplicationSoapIn" />
<wsdl:output message="tns:mutiplicationSoapOut" />
</wsdl:operation>
<wsdl:operation name="division">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求商的方法</wsdl:documentation>
<wsdl:input message="tns:divisionSoapIn" />
<wsdl:output message="tns:divisionSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="addition">
<soap:operation soapAction="http://tempuri.org/addition" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="subtract">
<soap:operation soapAction="http://tempuri.org/subtract" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="mutiplication">
<soap:operation soapAction="http://tempuri.org/mutiplication" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="division">
<soap:operation soapAction="http://tempuri.org/division" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="addition">
<soap12:operation soapAction="http://tempuri.org/addition" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="subtract">
<soap12:operation soapAction="http://tempuri.org/subtract" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="mutiplication">
<soap12:operation soapAction="http://tempuri.org/mutiplication" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="division">
<soap12:operation soapAction="http://tempuri.org/division" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Service">
<wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
<soap:address location="http://localhost:12989/WebSite1/Service.asmx" />
</wsdl:port>
<wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">
<soap12:address location="http://localhost:12989/WebSite1/Service.asmx" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
我们在这就练习调用webservice的四个方法,做一个简单的调用的例子,先在网站的前台添加几个控件(Default.aspx),代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Webservice调用实例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="Num1" runat="server"></asp:TextBox>
<select id="selectOper" runat = "server">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<asp:TextBox ID="Num2" runat="server"></asp:TextBox>
<span id = E runat = "server"></span>
<asp:TextBox ID="Result" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
到此一个一个简单的WebService的开发和调用就已经完成了,在实际应用中可以根据自己的需要,写一些功能强大的,复杂的WebService,不管多么复杂,整个流程都是这样的。
修改Default.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//在页面加载的时候动态创建一个按钮,在它的事件里调用Webservice
Button btn = new Button();
btn.Width = ;
btn.Text = " = ";
btn.Click += new EventHandler(btn_Click);
E.Controls.Add(btn);
}
/// <summary>
/// 定义动态创建Button的Click事件,在这个事件中调用Webservice
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void btn_Click(object sender, EventArgs e)
{
if (Num1.Text != "" && Num2.Text != "")
{
//实例化引用的webservice对象
localhost.Service WebserviceInstance = new localhost.Service();
int Oper = selectOper.SelectedIndex;
switch (Oper)
{
//通过实例化的webservice对象来调用Webservice暴露的方法
case :
Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
break;
case :
Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
break;
case :
Result.Text = WebserviceInstance.mutiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
break;
case :
Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
break;
}
}
}
}
可以运行该网站了。
VS2008中C#开发webservice简单实例的更多相关文章
- 初始cfx开发webservice, 简单实例应用
项目结构图: 步骤一: 添加maven 依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- 主题:Java WebService 简单实例
链接地址:主题:Java WebService 简单实例 http://www.iteye.com/topic/1135747 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...
- (Hibernate进阶)Hibernate搭建开发环境+简单实例(二)
hibernate是非常典型的持久层框架,持久化的思想是非常值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和 ...
- 【SSH进阶之路】Hibernate搭建开发环境+简单实例(二)
Hibernate是很典型的持久层框架,持久化的思想是很值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和原理 ...
- Android Studio1.4.x JNI开发基础 - 简单实例
接上一篇,搭建好基于Android Studio的环境之后,编写native代码相对来说也比较简单了.在Android上编写Native代码和在Linux编写C/C++代码还是有区别,Native代码 ...
- MVVM开发模式简单实例MVVM Demo
本文主要是翻译Rachel Lim的一篇有关MVVM模式介绍的博文 A Simple MVVM Example 并具体给出了一个简单的Demo(原文是以WPF开发的,对于我自己添加或修改的一部分会用红 ...
- .net实现webservice简单实例分享
原理:WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互.作用:主要用 ...
- 在MFC框架中使用OpenGL的简单实例
引言 我们知道,在MFC框架中,用于绘图的接口是GDI.但GDI只能绘制简单的2D图形,要想制作精美的3D图形,一个可行的办法是使用OpenGL或者Direct3D等第三方库. 由于最近在给导师的一个 ...
- Java WebService 简单实例使用JDK
原文地址:http://www.cnblogs.com/jasoncc/archive/2011/12/22/2296052.html 什么是WebServices? 它是一种构建应用程序的普 ...
随机推荐
- 你不容错过的 腾讯 AlloyTeam Web 前端大会 看点完全剖析
AC大会 ( Alloyteam Conf ),是由腾讯前端技术团队的标杆团队 AlloyTeam 发起的前端技术大会,旨在分享团队在技术研究.产品研发.开源项目的经验沉淀.AC2017 将会继续在工 ...
- vue实例讲解之vuex的使用
vuex是一个状态管理插件,本文通过一个简单的实例来讲解一下,vuex的使用. 先看一张官方的图: 这个图新手一看估计是蒙的,简单解释一下,这个图表示的就是vue通过Action Mutations ...
- 【转】Spark Streaming和Kafka整合开发指南
基于Receivers的方法 这个方法使用了Receivers来接收数据.Receivers的实现使用到Kafka高层次的消费者API.对于所有的Receivers,接收到的数据将会保存在Spark ...
- 51nod 1451 合法三角形 判斜率去重,时间复杂度O(n^2)
题目: 这题我WA了3次,那3次是用向量求角度去重算的,不知道错在哪了,不得不换思路. 第4次用斜率去重一次就过了. 注意:n定义成long long,不然求C(3,n)时会溢出. 代码: #incl ...
- HDU1251统计难题(水字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- c# gdi+输出成不同mime类型的图片
/// <summary> /// 通过图片的mime类型得到相应的编码器 /// </summary> /// <param name="mimeType&q ...
- Mysql 学习之EXPLAIN作用
一.MYSQL的索引 索引(Index):帮助Mysql高效获取数据的一种数据结构.用于提高查找效率,可以比作字典.可以简单理解为排好序的快速查找的数据结构.索引的作用:便于查询和排序(所以添加索引会 ...
- 【转载】兼容所有浏览器的JQuery zClip插件实现复制到剪贴板功能
文章转载自 代码家园 http://www.daimajiayuan.com/ 原文链接:http://www.daimajiayuan.com/sitejs-17973-1.html原文摘要: 相信 ...
- Django实现用户密码重置
使用Django内置的认证视图实现简单的通过邮箱重置密码的功能版本:django 1.11 在django.contrib.auth.views中提供了四个类视图用于密码重置 class Passwo ...
- 使用OLAMISDK实现一个语音输入数字进行24点计算的iOS程序
前言 在目前的软件应用中,输入方式还是以文字输入方式为主,但是语音输入的方式目前应用的越来越广泛.这是一个利用 Olami SDK 编写的一个24点iOS程序,是通过语音进行输入. Olami SDK ...