c#創建webservice簡單實例

很多人在論壇裏說,在Visual Studio 2010中不能創建“ASP.Net Web Service”這種project了,下面跟帖者云云,有的說這是因爲微軟已經將Web Service整合進WCF,也有的提出一種先將.Net Framework Target設置爲3.5的一種很“Tricky”的作法,其實這些說法是不準確的。微軟確實用WCF整合了Web Service,但並不等於說微軟不準備讓大家在Visual Studio裏面創建傳統的Web Service了。其實正確的做法很簡單,大家一看就恍然大悟了。


第一步:創建一個“ASP.Net Empty Web Application”項目



第二步:在項目中添加“Web Service”新項目

第一步之後,Visual Studio 2010會創建一個僅含一個站點配製文件(Web.config)的空站點,其餘的什麼也沒有。

我們在Visual Studio 2010的Solution Explorer中,選中當前的這個project,添加新項目(右鍵菜單:Add --> New Item),選擇“Web Service”這種類型:


看到這裏讀者應該就恍然大悟了吧。


好,我們繼續:


第三步:編碼、運行


添加完Web Service這種new item之後,Visual Studio已經替我們寫了個示範的Web方法了:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6.   
  7. namespace sitedemo.Services  
  8. {  
  9.     /// <summary>   
  10.     /// Summary description for CalculateService  
  11.     /// </summary>   
  12.     [WebService(Namespace = "http://tempuri.org/")]  
  13.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  14.     [System.ComponentModel.ToolboxItem(false)]  
  15.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
  16.     // [System.Web.Script.Services.ScriptService]  
  17.     public class CalculateService : System.Web.Services.WebService  
  18.     {  
  19.   
  20.         [WebMethod]  
  21.         public string HelloWorld()  
  22.         {  
  23.             return "Hello World";  
  24.         }  
  25.     }  
  26. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace sitedemo.Services
{
    /// <summary>
    /// Summary description for CalculateService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class CalculateService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}


直接Press F5就可以看到結果:




然後我們改寫這段代碼,添加我們自己的方法進去:

  1. using System.Web.Services;  
  2.   
  3. namespace sitedemo.Services  
  4. {  
  5.     /// <summary>   
  6.     /// Summary description for CalculateService  
  7.     /// </summary>   
  8.     [WebService(Namespace = "http://tempuri.org/")]  
  9.     public class CalculateService : WebService  
  10.     {  
  11.         [WebMethod]  
  12.         public string HelloWorld()  
  13.         {  
  14.             return "Hello World";  
  15.         }  
  16.   
  17.         [WebMethod]  
  18.         public int Add(int x, int y)  
  19.         {  
  20.             return x + y;  
  21.         }  
  22.     }  
  23. }  
using System.Web.Services;

namespace sitedemo.Services
{
    /// <summary>
    /// Summary description for CalculateService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    public class CalculateService : WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
}


運行:




怎麼樣,是不是很簡單?微笑



總結

現在我們再回過頭來看看,從VS2010之前版本的舊的創建Web Service的方式,到現在新的變化,Visual Studio改動了什麼?

手頭的機器沒有裝舊版的Visual Studio,我就現從網上抓一張教程裏的截圖吧,讓我們看看舊版的Visual Studio裏面大家創建Web Service時創建新項目的截圖:


很多人說在Visual Studio 2010裏面無法創建Web Service,他們大概是在尋找上面截圖中的這種“ASP.Net Web Service”項目吧。

現在再回過頭來看看,其實微軟在Visual Studio 2010裏面作了一個相當合理(make sense)的改變。

Web Service並不能單獨存在,它必須Host在一個Web Site/Web Application上面。所以,在一個Web Site/Web Application裏面,通過Add new item添加一個Web Service,這纔是最合理的作法。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章