代码改变世界

使用Windows Service托管WCF服务

2011-08-01 13:50  雪中风筝  阅读(305)  评论(1编辑  收藏  举报

声明服务接口

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.ServiceModel;
6
7 namespace Microsoft.ServiceModel.Samples
8 {
9 [ServiceContract]
10 interface ICalculator
11 {
12 [OperationContract]
13 double Add(double n1, double n2);
14 [OperationContract]
15 double Subtract(double n1, double n2);
16 [OperationContract]
17 double Multiply(double n1, double n2);
18 [OperationContract]
19 double Divide(double n1, double n2);
20 }
21 }

接口的实现

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Microsoft.ServiceModel.Samples
7 {
8 class CalculatorService : ICalculator
9 {
10 public double Add(double n1, double n2)
11 {
12 double result = n1 + n2;
13 return result;
14 }
15
16 public double Subtract(double n1, double n2)
17 {
18 double result = n1 - n2;
19 return result;
20 }
21
22 public double Multiply(double n1, double n2)
23 {
24 double result = n1 * n2;
25 return result;
26 }
27
28 public double Divide(double n1, double n2)
29 {
30 double result = n1 / n2;
31 return result;
32 }
33 }
34 }

 实现Windows Service安装的主线程

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Configuration;
6 using System.Configuration.Install;
7 using System.ServiceProcess;
8 using System.ServiceModel;
9
10 namespace Microsoft.ServiceModel.Samples
11 {
12 public class CalculatorWindowsService : ServiceBase
13 {
14 public ServiceHost host = null;
15 public CalculatorWindowsService()
16 {
17 base.ServiceName = "WCFWindowsServiceSample";
18 }
19
20 public static void Main()
21 {
22 ServiceBase.Run(new CalculatorWindowsService());
23 }
24
25 protected override void OnStart(string[] args)
26 {
27 if (host!=null)
28 {
29 host.Close();
30 }
31 //这里使用的默认的EndPoint,通过配置文件来读取
32 host = new ServiceHost(typeof(CalculatorService));
33
34 host.Open();
35 }
36
37 protected override void OnStop()
38 {
39 if (host !=null)
40 {
41 host.Close();
42 host = null;
43 }
44 }
45 }
46 }

安装Windows Service的类

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Configuration.Install;
6 using System.Configuration;
7 using System.ServiceProcess;
8 using System.ComponentModel;
9
10 namespace Microsoft.ServiceModel.Samples
11 {
12 [RunInstaller(true)]
13 public class ProjectInstaller : Installer
14 {
15 private ServiceProcessInstaller process;
16 private ServiceInstaller service;
17
18 public ProjectInstaller()
19 {
20 process = new ServiceProcessInstaller();
21 process.Account = ServiceAccount.LocalSystem;
22 service = new ServiceInstaller();
23 service.ServiceName = "WCFWindowsServiceSample";
24 Installers.Add(process);
25 Installers.Add(service);
26 }
27 }
28 }

使用配置文件进行EndPoint的配置

 1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <system.serviceModel>
4 <services>
5 <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">
6 <host>
7 <baseAddresses>
8 <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
9 </baseAddresses>
10 </host>
11
12 <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator">
13
14 </endpoint>
15 </service>
16 </services>
17 <behaviors>
18 <serviceBehaviors>
19 <behavior name="CalculatorServiceBehavior">
20 <serviceMetadata httpGetEnabled="true"/>
21 </behavior>
22 </serviceBehaviors>
23 </behaviors>
24 </system.serviceModel>
25 </configuration>

调用InstallerUtil.exe进行安装

打开VS2010的命令行工具,CD到项目目录下,运行installutil WCFService.exe(根据自己项目的可执行文件),打开服务就可以看到这个服务了

启动服务,打开WCFTestClient.exe进行测试