一步步教你建立.NET 4服務並且寄宿在IIS 7.5中

來源:互聯網
上載者:User

本文譯自Walkthrough on creating WCF 4.0 Service and Hosting in IIS 7.5

最近在學習WCF的時候。寄宿IIS7.5這部分總是搞不定。搜了很長時間。發現也是很多文章也是人云亦云。根本通不過。於是組合了一下關鍵字,搜了一下英文的文章。總算是搞定了。

目標

本文將會一步步教給你

  1. 怎麼樣建立一個基本的 WCF 4.0 服務?
  2. 怎麼樣把WCF服務寄宿在IIS 7.5?
  3. 用戶端如何測試服務可用
建立WCF服務

建立WCF服務,開啟VS,選擇新工程,然後從WCF的標籤頁裡,選擇WCF服務應用程式,來建立一個新的WCF服務。

在IService1 .cs 和Service1.svc.cs中刪除WCF自己添加的預設的代碼。

a. 因此刪除了資料契約
b. 修改服務契約如下. 僅僅寫一個方法契約返回一個字串

IService1.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.ServiceModel.Web;usingSystem.Text;namespaceWcfService5{    [ServiceContract]    publicinterfaceIService1    {        [OperationContract]        stringGetMessage();    }  }

 

Service1.svc.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.ServiceModel.Web;usingSystem.Text; namespaceWcfService5{    publicclassService1:IService1    {        publicstringGetMessage()        {            return"Hello From WCF Service ";        }    }}
設定檔就保留為WCF預設的就行了

 

Web.Config

<?xml version="1.0"?><configuration><system.web><compilation debug="true"targetFramework="4.0" /></system.web><system.serviceModel><behaviors><serviceBehaviors><behavior><serviceMetadata httpGetEnabled="true"/><serviceDebug includeExceptionDetailInFaults="false"/></behavior></serviceBehaviors></behaviors><serviceHostingEnvironment multipleSiteBindingsEnabled="true" /></system.serviceModel><system.webServer><modules runAllManagedModulesForAllRequests="true"/></system.webServer></configuration>

 

寄宿WCF服務在IIS7.5中

開啟IIS

a. 使用管理員權限開啟命令列. 點擊開始->運行 輸入“inetmgr”開啟 IIS .

然後你就會到下面這個地方。

b. 在“網站”上右擊,點擊“添加新網站”

c. 然後就開啟了新視窗

網站名字隨便給。. 我給了個叫做 HostedWcfService的名字

然後點擊選擇,在應用程式集區選擇 ASP.Net v4.0,(ps:如果沒有的話是IIS沒裝全)

現在在實體路徑這裡,我們需要把服務的實體路徑填上,為了知道這個實體路徑,我們在VS裡,資源管理檢視的的我們的WCF工程上點擊右鍵,選擇在windows資源管理員開啟。然後從資源管理員的地址欄把那個地址複製下來。或者你如果很清楚的話自己在瀏覽裡找到就行了

然後再綁定這裡我們選擇 HTTP. 給個任意的連接埠數字. 我寫個4567

主機名稱就不要亂填了,. 留空即可

立即開始網站的選項也選上。

點擊ok。

d. 然後以管理員權限開啟Visual Studio 命令提示(2010).

 

輸入如下命令

aspnet_regiis.exe /iru

一會會告訴你安裝完成 ASP.Net 4.0(注意,有些人可能在前面的應用程式集區那塊沒有.net 4 的選項,可以先把這一步做了再去執行前一步。)

e. 然後去 IIS, 會發現網站已經好了

發布WCF服務

回到我們的VS中的 WCF 服務項目. 右擊選擇發布。

On clicking of publish a window will come.

給一個服務的URL,你隨意給,只要保證名字唯一確定即可,服務名以svc結尾就好

把前面建立的網站的名字 HostedWcfService 寫到下面.

認證這塊就不要了

Now click on Publish.

點擊發布後提示發布成功

在IIS中瀏覽服務

開啟IIS,然後在 HostedWcfService.網站上點擊右鍵 ->管理網站->瀏覽

當你點擊瀏覽。會出現下面的情況

會得到上面的錯誤。但是這都不是事,在URL後面加上 Service1.svc 就開啟了

http://localhost:4567/Service1.svc

在瀏覽器裡你可以看到服務已經正常運行了

現在你已經成功的建立了 WCF 4.0 服務並且寄宿在了 IIS 7.5中

在用戶端測試WCF服務

a. 建立一個控制台工程

b. 右鍵點擊,加入服務參考

c. 給出服務的地址,點擊前往,(或者直接點擊發現)選中服務,點擊確定

d. 如下使用服務

Program.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingConsoleApplication1.ServiceReference1; namespaceConsoleApplication1{    classProgram    {        staticvoidMain(string[]args)        {            Service1Client proxy=newService1Client();            Console.WriteLine(proxy.GetMessage());            Console.Read();        }    }}

 

輸出結果。

原文連結:http://leaver.me/archives/1245.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.