首页 > 代码库 > 一步一步构建服务并用Windows服务承载,以及调用

一步一步构建服务并用Windows服务承载,以及调用

声明:试验环境,不与实际开发,勿混淆。(否则,责任自负)

第一步,构建服务。

新建 “WCF服务库项目”

IBasicMath.cs代码如下(别忘了添加引用了!)

 1 using System;
 2 using System.Runtime.Serialization;
 3 using System.ServiceModel;
 4 
 5 namespace MathServiceLibrary
 6 {
 7     [ServiceContract(Namespace="www.Intertech.com")]
 8     public interface IBasicMath
 9     {
10         [OperationContract]
11         int Add(int x, int y);
12     }
13 }

MathService.cs代码如下

 1 using System;
 2 using System.Runtime.Serialization;
 3 using System.ServiceModel;
 4 
 5 namespace MathServiceLibrary
 6 {
 7     public class MathService : IBasicMath
 8     {
 9 
10         public int Add(int x, int y)
11         {
12             return x + y;
13         }
14     }
15 }

App.config代码如下(它应该会自动创建的...)

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3 
 4   <appSettings>
 5     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
 6   </appSettings>
 7   <system.web>
 8     <compilation debug="true" />
 9   </system.web>
10   <system.serviceModel>
11     <services>
12       <service name="MathServiceLibrary.MathService">
13         <endpoint address="" binding="wsHttpBinding" bindingConfiguration=""
14           contract="MathServiceLibrary.IBasicMath">
15           <identity>
16             <dns value="localhost" />
17           </identity>
18         </endpoint>
19         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
20         <host>
21           <baseAddresses>
22             <add baseAddress="http://localhost:8733/Design_Time_Addresses/MathServiceLibrary/Service1/" />
23           </baseAddresses>
24         </host>
25       </service>
26     </services>
27     <behaviors>
28       <serviceBehaviors>
29         <behavior>
30           <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
31           <serviceDebug includeExceptionDetailInFaults="False" />
32         </behavior>
33       </serviceBehaviors>
34     </behaviors>
35   </system.serviceModel>
36 
37 </configuration>
View Code

然后,生成。


第二步,使用 Windows 承载 WCF 服务

 新建 “Windows服务” 项目

MathWinService 代码如下

 1 using System;
 2 using System.ServiceProcess;
 3 using MathServiceLibrary;
 4 using System.ServiceModel;
 5 
 6 namespace MathWindowsServiceHost
 7 {
 8     public partial class MathWinService : ServiceBase
 9     {
10         private ServiceHost myHost;
11         public MathWinService()
12         {
13             InitializeComponent();
14         }
15 
16         protected override void OnStart(string[] args)
17         {
18             //确保安全
19             if (myHost != null) {
20                 myHost.Close();
21                 myHost = null;
22             }
23             //创建宿主
24             myHost = new ServiceHost(typeof(MathService));
25             //ABC
26             Uri address = new Uri("http://localhost:8080/MathServiceLibrary");
27             WSHttpBinding binding = new WSHttpBinding();
28             Type contract = typeof(IBasicMath);
29             //增加端点
30             myHost.AddServiceEndpoint(contract, binding, address);
31             //打开宿主
32             myHost.Open();
33         }
34 
35         protected override void OnStop()
36         {
37             //关闭宿主
38             if (myHost != null)
39                 myHost.Close();
40         }
41     }
42 }
MathWinService.cs

App.config 代码如下

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3     <startup> 
 4         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
 5     </startup>
 6   <system.serviceModel>
 7     <services>
 8       <service name="MathServiceLibrary.MathService"
 9                behaviorConfiguration ="MathServiceMEXBehavior">
10         <endpoint address="mex"
11                   binding="mexHttpBinding"
12                   contract="IMetadataExchange"/>
13         <host>
14           <baseAddresses>
15             <add baseAddress="http://localhost:8080/MathService"/>
16           </baseAddresses>
17         </host>
18       </service>
19     </services>
20 
21     <behaviors>
22       <serviceBehaviors>
23         <behavior name="MathServiceMEXBehavior">
24           <serviceMetadata httpGetEnabled="true"/>
25         </behavior>
26       </serviceBehaviors>
27     </behaviors>
28   </system.serviceModel>
29 </configuration>
App.config

然后打开设计器,右键 “添加安装程序”

此时,窗体上应该会出现两个组件

第一个表示windows服务的类型,比如 Account 属性,指明了运行此服务的账户类型。 此处改为 “LocalSystem”

第二个表示此服务的类型,比如 ServiceName 属性,指明了 “服务名”, Description 指明了 “服务介绍”。生成它

最后,安装服务

找到 MathWindowsServiceHost.exe

使用开发人员命令提示: installutil MathWindowsServiceHost.exe

如果安装成功,服务里面就可以看到了

第三步,消费此服务

新建 “MathClient” 项目

添加服务引用

Program.cs代码如下

 1 using System;
 2 using MathClient.ServiceReference;
 3 
 4 namespace MathClient
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             Console.WriteLine("******* It‘s a new Day ! ********");
11             using (BasicMathClient proxy = new BasicMathClient()) {
12                 proxy.Open();
13                 Console.WriteLine("2 + 3 = {0}",proxy.Add(2, 3));
14             }
15             Console.ReadLine();
16         }
17     }
18 }
Program.cs

如果你的服务已经运行的话,那么启动它吧!