環境:
vs版本:vs2013
windows版本:win7
IIS版本:IIS7.0
(如果覺得對您有用,請點擊右下角【推薦】一下,讓更多人看到,謝謝)
配置環境:
主要針對於IIS
·首先,有很多人的機器上都沒有打開IIS服務
控制面板->程序和功能->打開或關閉windows功能(左側,較慢,稍等)->Internet信息服務(默認打開的功能不能完全滿足之后的需要,可以全部打開或者網上查詢一下需要打開哪些)
·接着,在管理工具中打開Internet 信息服務(IIS)管理器
·最后,在網頁上輸入http://127.0.0.1后能看到IIS的主頁,就ok了。(這里隱藏了一個問題,就是先安裝了framework后安裝IIS會有一個問題,稍后解決)
防火牆配置:(如不配置,在其他機器上訪問不到發布在你機器上的服務接口或者其他網站)
網上有說直接關了防火牆,就好比人家惹到你,你非得整死他一樣。。。好慘
打開防火牆,點擊左側菜單里面的“高級設置”,會看到有“入站規則”和“出站規則”,添加一個入站規則端口就好了,這樣,你在下面的流程中配置的那個端口,在其他位置訪問你機器上的這個端口的時候,就不會被攔住了...何必置人於死地呢!
實現過程之編寫WebService
我使用的是vs2013,過程如下:
1、創建空解決方案
2、創建空Web應用程序工程(這里面沒有web服務工程...)
3、創建Web服務(asmx)
這是IDE會給你初始化一個開發框架,你只需要在里面加上你需要公開的方法就可以了,[WebService]特性的使用就是用來修飾將要公布出來的服務接口。(具體原理這里不講)
代碼如下:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Services; 6 7 namespace WebService 8 { 9 /// <summary> 10 /// WebService1 的摘要說明 11 /// </summary> 12 [WebService(Namespace = "http://MrHouJL/WebServices")] 13 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 14 [System.ComponentModel.ToolboxItem(false)] 15 // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消注釋以下行。 16 [System.Web.Script.Services.ScriptService] 17 public class WebService1 : System.Web.Services.WebService 18 { 19 20 [WebMethod] 21 public string HelloWorld(string str) 22 { 23 return "Hello World" + str; 24 } 25 [WebMethod] 26 public string HelloWorld1() 27 { 28 return "Hello World 1"; 29 } 30 [WebMethod] 31 public string HelloWorld2() 32 { 33 return "Hello World 2"; 34 } 35 [WebMethod] 36 public string HelloWorld3() 37 { 38 return "Hello World 3"; 39 } 40 [WebMethod(Description = "求和的方法")] 41 public double addition(double i, double j) 42 { 43 return i + j; 44 } 45 [WebMethod(Description = "求差的方法")] 46 public double subtract(double i, double j) 47 { 48 return i - j; 49 } 50 [WebMethod(Description = "求積的方法")] 51 public double multiplication(double i, double j) 52 { 53 return i * j; 54 } 55 [WebMethod(Description = "求商的方法")] 56 public double division(double i, double j) 57 { 58 if (j != 0) 59 return i / j; 60 else 61 return 0; 62 } 63 } 64 }
功能編寫完畢,接下來就是發布在剛剛准備好的IIS環境上面了。
1、右鍵點擊工程,發布,選擇一個文件夾物理路徑。
2、打開IIS管理器
3、右擊“網站”,添加網站,配置“網站名稱”,“物理路徑”,“IP”,“端口”OK
4、注意:這里面的身份驗證要允許匿名,目錄瀏覽要啟用(雙擊點擊右側啟用,為了之后可以瀏覽WebService目錄)
5、運行網站,你在瀏覽器中輸入之前輸入的IP+端口號就能訪問到目錄了(如有問題留言或者度娘)。
實現過程之訪問WebService
在這里主要是介紹使用后台訪問:
首先,在工程里面右鍵點擊引用,添加服務引用,輸入IP+端口,點擊“轉到”,應該就能看到之前的所寫的服務接口了。起個名就添加進去了。
添加webform界面,前台代碼如下

1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebService.WebForm1" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8 <title></title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <div> 13 14 </div> 15 <asp:TextBox ID="TextBox1" runat="server" Height="15px" Width="50px"></asp:TextBox> 16 <asp:DropDownList ID="DropDownList1" runat="server"> 17 <asp:ListItem>+</asp:ListItem> 18 <asp:ListItem>-</asp:ListItem> 19 <asp:ListItem>*</asp:ListItem> 20 <asp:ListItem>/</asp:ListItem> 21 </asp:DropDownList> 22 <asp:TextBox ID="TextBox2" runat="server" Height="15px" Width="50px"></asp:TextBox> 23 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=" = " /> 24 <asp:TextBox ID="TextBox3" runat="server" Height="15px" Width="50px"></asp:TextBox> 25 </form> 26 </body> 27 </html>
后台代碼如下:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 namespace WebService 9 { 10 public partial class WebForm1 : System.Web.UI.Page 11 { 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 15 } 16 17 protected void Button1_Click(object sender, EventArgs e) 18 { 19 string oper = DropDownList1.Text; 20 double a = Convert.ToDouble(TextBox1.Text); 21 double b = Convert.ToDouble(TextBox2.Text); 22 ServiceReference1.WebService1SoapClient ws = new ServiceReference1.WebService1SoapClient(); 23 switch (oper) 24 { 25 case "+": TextBox3.Text = ws.addition(a, b).ToString(); break; 26 case "-": TextBox3.Text = ws.subtract(a, b).ToString(); break; 27 case "*": TextBox3.Text = ws.multiplication(a, b).ToString(); break; 28 case "/": TextBox3.Text = ws.division(a, b).ToString(); break; 29 default: 30 break; 31 } 32 Response.Write(ws.HelloWorld(TextBox3.Text)); 33 } 34 } 35 }
建議各位小主,還是自己寫寫較好。
遇到的問題
HTTP 錯誤 500.21 - Internal Server Error處理程序“NickLeeCallbackHandler”在其模塊列表中有一個錯誤模塊“ManagedPipelineHandler”
原因:在安裝Framework v4.0之后,再啟用IIS,導致Framework沒有完全安裝
解決:開始->所有程序->附件->鼠標右鍵點擊“命令提示符”->以管理員身份運行->%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
如果還不行,可檢查IIS的應用程序池,是否使用集成模式,如果不是則改成集成模式
至此,整個webservice入門就告一段落了,大家僅作參考,如有問題,快來指正...