VS2008中C#开发webservice简单实例

时间:2021-10-28 21:02:44

1.创建工程

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

VS2008中C#开发webservice简单实例

 

工程建好后,会自动添加如下代码:

 1 using System;
 2 using System.Linq;
 3 using System.Web;
 4 using System.Web.Services;
 5 using System.Web.Services.Protocols;
 6 using System.Xml.Linq;
 7 
 8 [WebService(Namespace = "http://tempuri.org/")]
 9 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
10 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
11 // [System.Web.Script.Services.ScriptService]
12 public class Service : System.Web.Services.WebService
13 {
14     public Service () {
15 
16         //如果使用设计的组件,请取消注释以下行 
17         //InitializeComponent(); 
18     }
19 
20     [WebMethod]
21     public string HelloWorld() {
22         return "Hello World";
23     }
24     
25 }

 

可以运行一遍看看效果。

2.添加代码增强webservice的功能

增加加减乘除的功能。

代码如下:

 1 using System;
 2 using System.Linq;
 3 using System.Web;
 4 using System.Web.Services;
 5 using System.Web.Services.Protocols;
 6 using System.Xml.Linq;
 7 
 8 [WebService(Namespace = "http://tempuri.org/")]
 9 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
10 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
11 // [System.Web.Script.Services.ScriptService]
12 public class Service : System.Web.Services.WebService
13 {
14     public Service () {
15 
16         //如果使用设计的组件,请取消注释以下行 
17         //InitializeComponent(); 
18     }
19 
20 
21     //[WebMethod]
22     //public string HelloWorld()
23     //{
24     //    return "Hello World";
25     //}
26 
27     [WebMethod(Description = "求和的方法")]
28     public double addition(double i, double j)
29     {
30         return i + j;
31     }
32 
33     [WebMethod(Description = "求差的方法")]
34     public double subtract(double i, double j)
35     {
36         return i - j;
37     }
38 
39     [WebMethod(Description = "求积的方法")]
40     public double mutiplication(double i, double j)
41     {
42         return i * j;
43     }
44 
45     [WebMethod(Description = "求商的方法")]
46     public double division(double i, double j)
47     {
48         if(j!=0)
49             return i/j;
50         else
51             return 0;
52     }
53 }

运行效果如下:

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文件如下:

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <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/">
  3   <wsdl:types>
  4     <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
  5       <s:element name="addition">
  6         <s:complexType>
  7           <s:sequence>
  8             <s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
  9             <s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
 10           </s:sequence>
 11         </s:complexType>
 12       </s:element>
 13       <s:element name="additionResponse">
 14         <s:complexType>
 15           <s:sequence>
 16             <s:element minOccurs="1" maxOccurs="1" name="additionResult" type="s:double" />
 17           </s:sequence>
 18         </s:complexType>
 19       </s:element>
 20       <s:element name="subtract">
 21         <s:complexType>
 22           <s:sequence>
 23             <s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
 24             <s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
 25           </s:sequence>
 26         </s:complexType>
 27       </s:element>
 28       <s:element name="subtractResponse">
 29         <s:complexType>
 30           <s:sequence>
 31             <s:element minOccurs="1" maxOccurs="1" name="subtractResult" type="s:double" />
 32           </s:sequence>
 33         </s:complexType>
 34       </s:element>
 35       <s:element name="mutiplication">
 36         <s:complexType>
 37           <s:sequence>
 38             <s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
 39             <s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
 40           </s:sequence>
 41         </s:complexType>
 42       </s:element>
 43       <s:element name="mutiplicationResponse">
 44         <s:complexType>
 45           <s:sequence>
 46             <s:element minOccurs="1" maxOccurs="1" name="mutiplicationResult" type="s:double" />
 47           </s:sequence>
 48         </s:complexType>
 49       </s:element>
 50       <s:element name="division">
 51         <s:complexType>
 52           <s:sequence>
 53             <s:element minOccurs="1" maxOccurs="1" name="i" type="s:double" />
 54             <s:element minOccurs="1" maxOccurs="1" name="j" type="s:double" />
 55           </s:sequence>
 56         </s:complexType>
 57       </s:element>
 58       <s:element name="divisionResponse">
 59         <s:complexType>
 60           <s:sequence>
 61             <s:element minOccurs="1" maxOccurs="1" name="divisionResult" type="s:double" />
 62           </s:sequence>
 63         </s:complexType>
 64       </s:element>
 65     </s:schema>
 66   </wsdl:types>
 67   <wsdl:message name="additionSoapIn">
 68     <wsdl:part name="parameters" element="tns:addition" />
 69   </wsdl:message>
 70   <wsdl:message name="additionSoapOut">
 71     <wsdl:part name="parameters" element="tns:additionResponse" />
 72   </wsdl:message>
 73   <wsdl:message name="subtractSoapIn">
 74     <wsdl:part name="parameters" element="tns:subtract" />
 75   </wsdl:message>
 76   <wsdl:message name="subtractSoapOut">
 77     <wsdl:part name="parameters" element="tns:subtractResponse" />
 78   </wsdl:message>
 79   <wsdl:message name="mutiplicationSoapIn">
 80     <wsdl:part name="parameters" element="tns:mutiplication" />
 81   </wsdl:message>
 82   <wsdl:message name="mutiplicationSoapOut">
 83     <wsdl:part name="parameters" element="tns:mutiplicationResponse" />
 84   </wsdl:message>
 85   <wsdl:message name="divisionSoapIn">
 86     <wsdl:part name="parameters" element="tns:division" />
 87   </wsdl:message>
 88   <wsdl:message name="divisionSoapOut">
 89     <wsdl:part name="parameters" element="tns:divisionResponse" />
 90   </wsdl:message>
 91   <wsdl:portType name="ServiceSoap">
 92     <wsdl:operation name="addition">
 93       <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求和的方法</wsdl:documentation>
 94       <wsdl:input message="tns:additionSoapIn" />
 95       <wsdl:output message="tns:additionSoapOut" />
 96     </wsdl:operation>
 97     <wsdl:operation name="subtract">
 98       <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求插的方法</wsdl:documentation>
 99       <wsdl:input message="tns:subtractSoapIn" />
100       <wsdl:output message="tns:subtractSoapOut" />
101     </wsdl:operation>
102     <wsdl:operation name="mutiplication">
103       <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求积的方法</wsdl:documentation>
104       <wsdl:input message="tns:mutiplicationSoapIn" />
105       <wsdl:output message="tns:mutiplicationSoapOut" />
106     </wsdl:operation>
107     <wsdl:operation name="division">
108       <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">求商的方法</wsdl:documentation>
109       <wsdl:input message="tns:divisionSoapIn" />
110       <wsdl:output message="tns:divisionSoapOut" />
111     </wsdl:operation>
112   </wsdl:portType>
113   <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
114     <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
115     <wsdl:operation name="addition">
116       <soap:operation soapAction="http://tempuri.org/addition" style="document" />
117       <wsdl:input>
118         <soap:body use="literal" />
119       </wsdl:input>
120       <wsdl:output>
121         <soap:body use="literal" />
122       </wsdl:output>
123     </wsdl:operation>
124     <wsdl:operation name="subtract">
125       <soap:operation soapAction="http://tempuri.org/subtract" style="document" />
126       <wsdl:input>
127         <soap:body use="literal" />
128       </wsdl:input>
129       <wsdl:output>
130         <soap:body use="literal" />
131       </wsdl:output>
132     </wsdl:operation>
133     <wsdl:operation name="mutiplication">
134       <soap:operation soapAction="http://tempuri.org/mutiplication" style="document" />
135       <wsdl:input>
136         <soap:body use="literal" />
137       </wsdl:input>
138       <wsdl:output>
139         <soap:body use="literal" />
140       </wsdl:output>
141     </wsdl:operation>
142     <wsdl:operation name="division">
143       <soap:operation soapAction="http://tempuri.org/division" style="document" />
144       <wsdl:input>
145         <soap:body use="literal" />
146       </wsdl:input>
147       <wsdl:output>
148         <soap:body use="literal" />
149       </wsdl:output>
150     </wsdl:operation>
151   </wsdl:binding>
152   <wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
153     <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
154     <wsdl:operation name="addition">
155       <soap12:operation soapAction="http://tempuri.org/addition" style="document" />
156       <wsdl:input>
157         <soap12:body use="literal" />
158       </wsdl:input>
159       <wsdl:output>
160         <soap12:body use="literal" />
161       </wsdl:output>
162     </wsdl:operation>
163     <wsdl:operation name="subtract">
164       <soap12:operation soapAction="http://tempuri.org/subtract" style="document" />
165       <wsdl:input>
166         <soap12:body use="literal" />
167       </wsdl:input>
168       <wsdl:output>
169         <soap12:body use="literal" />
170       </wsdl:output>
171     </wsdl:operation>
172     <wsdl:operation name="mutiplication">
173       <soap12:operation soapAction="http://tempuri.org/mutiplication" style="document" />
174       <wsdl:input>
175         <soap12:body use="literal" />
176       </wsdl:input>
177       <wsdl:output>
178         <soap12:body use="literal" />
179       </wsdl:output>
180     </wsdl:operation>
181     <wsdl:operation name="division">
182       <soap12:operation soapAction="http://tempuri.org/division" style="document" />
183       <wsdl:input>
184         <soap12:body use="literal" />
185       </wsdl:input>
186       <wsdl:output>
187         <soap12:body use="literal" />
188       </wsdl:output>
189     </wsdl:operation>
190   </wsdl:binding>
191   <wsdl:service name="Service">
192     <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
193       <soap:address location="http://localhost:12989/WebSite1/Service.asmx" />
194     </wsdl:port>
195     <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">
196       <soap12:address location="http://localhost:12989/WebSite1/Service.asmx" />
197     </wsdl:port>
198   </wsdl:service>
199 </wsdl:definitions>

 

我们在这就练习调用webservice的四个方法,做一个简单的调用的例子,先在网站的前台添加几个控件(Default.aspx),代码如下:

 1 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml" >
 6  <head id="Head1" runat="server">
 7       <title>Webservice调用实例</title>
 8   </head>
 9   <body>
10       <form id="form1" runat="server">
11          <div>
12              <asp:TextBox ID="Num1" runat="server"></asp:TextBox>
13              <select id="selectOper" runat = "server">
14                  <option>+</option>
15                  <option>-</option>
16                  <option>*</option>
17                  <option>/</option>
18              </select>
19              <asp:TextBox ID="Num2" runat="server"></asp:TextBox>
20              <span id = E runat = "server"></span>
21              <asp:TextBox ID="Result" runat="server"></asp:TextBox>
22          </div>
23  </form>
24  </body>
25  </html>

到此一个一个简单的WebService的开发和调用就已经完成了,在实际应用中可以根据自己的需要,写一些功能强大的,复杂的WebService,不管多么复杂,整个流程都是这样的。

修改Default.cs

 1 using System;
 2 using System.Configuration;
 3 using System.Data;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.Security;
 7 using System.Web.UI;
 8 using System.Web.UI.HtmlControls;
 9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12 
13 public partial class _Default : System.Web.UI.Page 
14 {
15     protected void Page_Load(object sender, EventArgs e)
16     {
17         //在页面加载的时候动态创建一个按钮,在它的事件里调用Webservice
18         Button btn = new Button();
19         btn.Width = 20;
20         btn.Text = " = ";
21         btn.Click += new EventHandler(btn_Click);
22         E.Controls.Add(btn);
23     }
24     /// <summary>
25     /// 定义动态创建Button的Click事件,在这个事件中调用Webservice
26     /// </summary>
27     /// <param name="sender"></param>
28     /// <param name="e"></param>
29     void btn_Click(object sender, EventArgs e)
30     {
31         if (Num1.Text != "" && Num2.Text != "")
32         {
33             //实例化引用的webservice对象
34             localhost.Service WebserviceInstance = new localhost.Service();
35             int Oper = selectOper.SelectedIndex;
36             switch (Oper)
37             {
38                 //通过实例化的webservice对象来调用Webservice暴露的方法
39                 case 0:
40                     Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
41                     break;
42                 case 1:
43                     Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
44                     break;
45                 case 2:
46                     Result.Text = WebserviceInstance.mutiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
47                     break;
48                 case 3:
49                     Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
50                     break;
51             }
52         }
53     }
54 }

可以运行该网站了。