VS2008中C#开发webservice简单实例

时间:2023-03-08 20:58:35

1.创建工程

文件-> 新建->网站 如下图。

VS2008中C#开发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";
} }

可以运行一遍看看效果。

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 ;
}
}

运行效果如下:

VS2008中C#开发webservice简单实例

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

VS2008中C#开发webservice简单实例

3.使用生成的webservice

VS2008-> 文件->新建->网站->ASP.NET网站->website2

VS2008中C#开发webservice简单实例

接下来添加刚才生成的webservice应用:

website2邮右键->添加web引用

VS2008中C#开发webservice简单实例

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;
}
}
}
}

可以运行该网站了。

相关文章