VS2008中C#開發webservice簡單例項
阿新 • • 發佈:2019-01-02
1.建立工程
檔案-> 新建->網站 如下圖。
工程建好後,會自動新增如下程式碼:
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 }
執行效果如下:
在這個URL後面新增?wsdl就可以獲取該webservice的wsdl。
3.使用生成的webservice
VS2008-> 檔案->新建->網站->ASP.NET網站->website2
接下來新增剛才生成的webservice應用:
website2郵右鍵->新增web引用
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 }
可以執行該網站了。