WCF寄宿到Windows Service


WCF寄宿到Windows Service[1]

2014-06-14

WCF寄宿到Windows Service
參考

WCF寄宿到Windows Service


返回

在前面創建一個簡單的WCF程序,我們把WCF的服務寄宿到了Host這個控制台項目中了。下面將介紹如何把WCF的服務寄宿到Windows服務中(源代碼):

1. 刪除原來Host控制台項目,然后在solution上右鍵,新建一個WindowService項目。如下圖:


2.對WcfServices.HostingWindowsSerice項目添加對Contracts項目、Service項目和System.ServiceModel的引用。

3.將WcfServices.HostingWindowsSerice項目中的Class1.cs文件重命名為CalculatorHost.cs,然后打開這個文件,里面代碼如下:

 1 using System.ServiceProcess;
 2 using System.ServiceModel;
 3 using WcfServices.Services;
 4 
 5 namespace WcfServices.HostingWindowsService
 6 {
 7     public partial class CalculatorHost : ServiceBase
 8     {
 9         private ServiceHost _host;
10 
11         public ServiceHost Host
12         {
13             get { return _host; }
14             set { _host = value; }
15         }
16 
17 
18         public CalculatorHost()
19         {
20             InitializeComponent();
21         }
22 
23         protected override void OnStart(string[] args)
24         {
25             Host = new ServiceHost(typeof(CalculatorService));
26             Host.Open();
27         }
28 
29         protected override void OnStop()
30         {
31             if (Host != null)
32             {
33                 Host.Close();
34                 Host = null;
35             }
36 
37         }
38     }
39 }
View Code

4.CalculatorHost.cs[Design]的界面上右鍵,選擇添加安裝器(Add Installer)。這時,項目里會自動生成一個ProjectInstaller.cs文件。

5.打開ProjectInstaller.cs[Design]的設計界面,將會出現下圖:

6.選中serviceInstaller1,打開它的屬性視圖(Ctrl W,P),修改屬性。如下圖所示:

7.接着選中serviceProcessInstaller1,打開它的屬性視圖,修改屬性。如下圖:(這里服務賬號也可以是其他的。)

8.接下來我們看看這個項目里的program.cs文件。代碼如下:

 1 using System.ServiceProcess;
 2 
 3 namespace WcfServices.HostingWindowsService
 4 {
 5     static class Program
 6     {
 7         /// <summary>
 8         /// 應用程序的主入口點。
 9         /// </summary>
10         static void Main()
11         {
12             ServiceBase[] ServicesToRun;
13             ServicesToRun = new ServiceBase[] 
14             { 
15                 new CalculatorHost() 
16             };
17             ServiceBase.Run(ServicesToRun);
18         }
19     }
20 }
View Code

 

9.這些都做好了之后,在WcfServices.HostingWindowsSerice項目中添加服務端的配置文件。這個在上一節中也寫過,代碼如下: 

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3   <system.serviceModel>
 4     <behaviors>
 5       <serviceBehaviors>
 6         <behavior name="CaclulaterBehavior">
 7           <serviceMetadata httpGetEnabled="true" />
 8         </behavior>
 9       </serviceBehaviors>
10     </behaviors>
11     <services>
12       <service behaviorConfiguration="CaclulaterBehavior" name="WcfServices.Services.CalculatorService">
13         <endpoint address="http://localhost:8080/calculatorservice" binding="basicHttpBinding"
14             bindingConfiguration="" contract="WcfServices.Contracts.ICalculator" />
15         <host>
16           <baseAddresses>
17             <add baseAddress="http://localhost:8080/calculatorservice" />
18           </baseAddresses>
19         </host>
20       </service>
21     </services>
22   </system.serviceModel>
23 </configuration>
View Code

 

10.Build項目WcfServices.HostingWindowsSerice,查看生成的文件如下:

將整個debug文件夾中文件拷出來,放到另外一個目錄下。比如D:\WCF\CalculatorService中。后面我們注冊的windows服務將從這個目錄下找exe文件。

11.下面就是要注冊了。我們用InstallUtil.exe來注冊(當然你也可以用sc)。打開InstallUtil.exe在我的電腦的目錄是:C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319。你可以從命令行進如這個目錄,然后執行InstallUtil命令。也可以在所有程序中找到visual studio Tools,里面的visual studio command prompt命令行工具。執行安裝的命令是InstallUtil D:\WCF\CalculatorService\WcfServices.HostingWindowsSerice.exe

12.成功后,你就可以在控制面板->管理工具->服務中找到它了,如下圖:

 

 參考:

[1] WCF注冊Windows Service  http://www.cnblogs.com/judastree/archive/2012/08/29/2662184.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM