為了驗證書上有關Visual Studio 2010添加服務引用和Web引用的差別,進行實驗。
一、建立一個Web服務程式項目
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Hello
{
/// <summary>
/// Hello 的摘要說明
/// </summary>
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
// [System.Web.Script.Services.ScriptService]
public class Hello : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
二、建立測試程式
窗體應用程式,在項目中選擇添加服務引用,選擇進階,添加Web引用來使用,引用後可以看到在Service References檔案夾下出現的ServiceReferrence1中有:configuration.svcinfo,configuration91.svcinfo,hello.disco,hello.wsdl,Reference.cs,Reference.svcmap,共六個檔案,或者直接添加服務引用。要使用的淚和方法在下圖:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuUjNiRDO1YGZ4YTY1UmM5cTMhBDOzIGNwQGO1ETYkVDOfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
在Web項目和Web網站解決方案引用時可以選擇添加Web引用還是服務引用。
1.添加Web引用,選擇命名空間為localhost,在App_WebReferences檔案夾下出現引用資訊,localhost檔案夾下有如下檔案:Hello.disco,Hello.discomap,Hello.wsdl。
2.添加服務引用,命名空間為local1,local1檔案夾中有以下檔案:configuration.svcinfo,configuration91.svcinfo,hello.disco,Hello.wsdl,Reference.svcmap。
效果如下