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简单实例的更多相关文章

  1. 初始cfx开发webservice, 简单实例应用

    项目结构图: 步骤一: 添加maven 依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  2. 主题:Java WebService 简单实例

    链接地址:主题:Java WebService 简单实例    http://www.iteye.com/topic/1135747 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...

  3. (Hibernate进阶)Hibernate搭建开发环境+简单实例(二)

    hibernate是非常典型的持久层框架,持久化的思想是非常值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和 ...

  4. 【SSH进阶之路】Hibernate搭建开发环境+简单实例(二)

    Hibernate是很典型的持久层框架,持久化的思想是很值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和原理 ...

  5. Android Studio1.4.x JNI开发基础 - 简单实例

    接上一篇,搭建好基于Android Studio的环境之后,编写native代码相对来说也比较简单了.在Android上编写Native代码和在Linux编写C/C++代码还是有区别,Native代码 ...

  6. MVVM开发模式简单实例MVVM Demo

    本文主要是翻译Rachel Lim的一篇有关MVVM模式介绍的博文 A Simple MVVM Example 并具体给出了一个简单的Demo(原文是以WPF开发的,对于我自己添加或修改的一部分会用红 ...

  7. .net实现webservice简单实例分享

    原理:WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互.作用:主要用 ...

  8. 在MFC框架中使用OpenGL的简单实例

    引言 我们知道,在MFC框架中,用于绘图的接口是GDI.但GDI只能绘制简单的2D图形,要想制作精美的3D图形,一个可行的办法是使用OpenGL或者Direct3D等第三方库. 由于最近在给导师的一个 ...

  9. Java WebService 简单实例使用JDK

    原文地址:http://www.cnblogs.com/jasoncc/archive/2011/12/22/2296052.html     什么是WebServices? 它是一种构建应用程序的普 ...

随机推荐

  1. 关于sping quartz定时执行理解与思考

    转载请注明原创出处,谢谢! 一直以为自己理解spring quartz,忽然最近几天发现自己理解的不对,在4月18号的时候,我执行了一个spring quartz的计划如下: 1 0 0 */3 * ...

  2. angular $compiler

    directive是如何被compiled HTML编译发生在三个阶段: 1.$compile遍历DOM节点匹配directives 如果compiler找到元素上的directive,directi ...

  3. Redis的安装以及在项目中使用Redis的一些总结和体会

    第一部分:为什么我的项目中要使用Redis 我知道有些地方没说到位,希望大神们提出来,我会吸取教训,大家共同进步! 注册时邮件激活的部分使用Redis 发送邮件时使用Redis的消息队列,减轻网站压力 ...

  4. python基础之五大标准数据类型

    学习一门语言,往往都是从Hello World开始. 但是笔者认为,在一个黑框框中输出一个"你好,世界"并没有什么了不起,要看透事物的本质,熟悉一门语言,就要了解其底层,就是我们常 ...

  5. 日期小demo

    有个项目需求是做个在日期上选择的,就是这种: 网上看了几个日期的demo都太厚重了,移植起来太麻烦,然后打算自己写. 就先写个简化的demo看看,主要有几个关键点: 首先要根据当前日期获取这个月有几天 ...

  6. [SDOI2011]工作安排

    Description 你的公司接到了一批订单.订单要求你的公司提供n类产品,产品被编号为1~n,其中第i类产品共需要Ci件.公司共有m名员工,员工被编号为1~m员工能够制造的产品种类有所区别.一件产 ...

  7. NOIP2017SummerTraining0714

    个人感受:第一题做了字典树,还运行错误,然后就弃疗了,然后水了二三两题,总共拿了85分,倒数. 正确答案 时间限制: 2 Sec  内存限制: 256 MB提交: 702  解决: 82[提交][状态 ...

  8. 概率图模型PGM——D map, I map, perfect map

    若F分布的每个条件独立性质都反映在A图中,则A图被称为F分布的D map. 若A图表现出的所有条件独立性质都在F分布中满足(与F分布不矛盾),则A图被称为F分布的I map. 弱A图既是F分布的D m ...

  9. Judge Route Circle

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  10. linux上搭建ftp

    linux上搭建ftp 重要 解决如何搭建ftp         解决用户指定访问其根目录         解决访问ftp超时连接         解决ftp主动连接.被动连接的问题 1.安装ftp ...