標籤:注釋 iis 允許 binding ati too 佔用 org als
1.建立一個web空項目:
2.添加web服務:
3.在添加的web服務中添加一個求和的方法:(註:必須在方法上面添加 [WebMethod]才會在網頁上顯示出來,其中(description為方法的描述))
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;namespace WebServiceDemo{ /// <summary> /// WebServiceDemo 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消對下行的注釋。 // [System.Web.Script.Services.ScriptService] public class WebServiceDemo : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } /// <summary> /// 兩個數求和的方法 /// </summary> /// <param name="a">第一個數字</param> /// <param name="b">第二個數字</param> /// <returns>返回兩個數位和</returns> [WebMethod(Description="求和的方法")] public int GetSum(int a,int b) { return a + b; } }}
4.本地瀏覽調用:
5.發布:(點擊項目-->滑鼠右鍵-->發布-->發布方法選擇檔案系統-->選擇發布目錄-->點擊發布)
6.安裝iis參考:https://jingyan.baidu.com/article/19192ad853224ce53f570748.html
7.部署,點擊網站-->滑鼠右鍵添加網站(實體路徑:選擇發布的檔案夾;ip地址:本機ip;連接埠:自訂未被佔用的任意的連接埠)-->確定
8.瀏覽
報如下錯誤:
添加預設文件即可:文檔名稱為:添加的web服務時候自訂的名稱
再瀏覽,可能報錯:
點擊應用程式集區,切換CLR版本;
再瀏覽:
成功!!
9.用戶端調用:
建立一個控制台來測試
點擊引用->滑鼠右鍵-->加入服務參考:
點擊進階
添加web引用:
將瀏覽器瀏覽時的地址複製並粘貼到URL
點擊箭頭(轉到),即可看到自己寫的方法;
點擊添加引用
添加成功!!
調用webservice中的方法:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WebServiceClient{ class Program { static void Main(string[] args) { //建立service對象 WebReference.WebServiceDemo service = new WebReference.WebServiceDemo(); int a = 100; int b = 100; Console.WriteLine("{0}+{1}={2}",a,b,service.GetSum(a,b)); Console.ReadKey(); } }}
運行:
成功!!
以上就是一個webservice建立、發布、部署、調用的簡單一實例、如有錯誤,歡迎指教!!
C#建立webservice並發布,部署到iis,用戶端調用