很多人在論壇裏說,在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方法了:
- 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";
- }
- }
- }
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就可以看到結果:
然後我們改寫這段代碼,添加我們自己的方法進去:
- 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;
- }
- }
- }
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,這纔是最合理的作法。