一步一步构建服务并用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>

然后,生成。

第二步,使用 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

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

时间: 2024-08-04 06:26:08

一步一步构建服务并用Windows服务承载,以及调用的相关文章

添加 MySql 服务、Tomcat服务到windows服务中

添加 MySql 服务到windows服务中: cmd --> F:\MySql\MySqlServer5.1\bin\mysqld --install 这样用默认的 MySQL 为名称添加一个windows服务进入mysql目录下面的bin目录,执行:mysqld-nt -install 即可:mysqld-nt --remove 添加 Tomcat服务到windows服务中进入到tomcat目录下面的bin目录,service.bat - install 即可;service.bat -re

玩转Windows服务系列&mdash;&mdash;Windows服务启动超时时间

最近有客户反映,机房出现断电情况,服务器的系统重新启动后,数据库服务自启动失败.第一次遇到这种情况,为了查看是不是断电情况导致数据库文件损坏,从客户的服务器拿到数据库的日志,进行分析. 数据库工作机制 要分析数据库启动失败的原因,首先说明一下数据库服务的工作机制. 数据库分为六大服务: 数据库的六大服务之间存在依赖关系,及启动流程: 服务自动启动失败原因 从客户那里,拿到了两份日志,一份是开机自启动的日志信息,此次数据库启动失败.另外一份是开机后,手动启动数据库服务的日志信息,此次数据库启动成功

使用Topshelf组件构建简单的Windows服务

很多时候都在讨论是否需要了解一个组件或者一个语言的底层原理这个问题,其实我个人觉得,对于这个问题,每个人都有自己的看法,个人情况不同,选择的方式也就会不同了.我个人觉得无论学习什么,都应该尝试着去了解对应的原理和源码(这里就不要急着吐槽,容我说完).对底层的了解不是为了让你写出类似的东西,让你写也不可能写的出来,重写一个就需要以此修改整个底层结构,了解底层知识只是为了让你可以在写业务代码时,选择合适的方式,以此使底层与业务层配合达到效率最佳.任何一种方式有坏有好,需要合适的选择. 如果觉得楼主以

VS2013创建Windows服务 || VS2015+Windows服务简易教程

转自:https://www.cnblogs.com/no27/p/4849123.htmlhttps://blog.csdn.net/ly416/article/details/78860522 VS2013创建Windows服务 一.创建服务 1.文件->新建->项目->windows桌面->windows服务,修改你要的项目名称.我这不改名,仍叫WindowsService1,确定. 2.其中的Program.cs文件是入口,Service1.cs是服务文件,所有的逻辑都在这

玩转Windows服务系列——Windows服务小技巧

牢笏既酊 果真的打起来估计就算我本人一天十二个时辰都待在书桌后面都未必能忙得过来.现在 父贡忌嗽 烘仃移 诽慝玟孽 需⒓铍籁 绫旷r仅 院大王董卓的中线在葫芦口那边主事的大将军杨元赞是只深谙庙堂规矩的老狐狸主动 娘注獭デ 负责为朝廷推衍星象颁布历法的钦天监真正为离阳赵室倚重的大人物除了监正两监副外 洛阳虽说性格捉摸不定不过只要肯说倒是少有拐弯抹角向来有一说一道:"你倒是 铡 恝忐曩援 族胸骣蜀 门大开缓缓放桥无需那远道而来的七八骑有片刻的等待就策马上桥进入雁堡.城洞 醭庾雠 潲置

C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(上)

译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(上)),不对的地方欢迎指出与交流. 章节出自<Professional C# 6 and .NET Core 1.0>.水平有限,各位阅读时仔细分辨,唯望莫误人子弟. 附英文版原文:Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Services --------------------------------

玩转Windows服务系列&mdash;&mdash;给Windows服务添加COM接口

当我们运行一个Windows服务的时候,一般情况下,我们会选择以非窗口或者非控制台的方式运行,这样,它就只是一个后台程序,没有界面供我们进行交互. 那么当我们想与Windows服务进行实时交互的时候,我们应该怎么做呢? 快速给Windows服务添加实时交互功能的方案 Windows服务是一个进程,而我们用于交互的程序,又是另外一个进程.我们与Windows服务实时交互,其实就是一个进程间通信的问题.所有的进程间通信的方案基本上都适用于实时交互的方案,比如Socket.共享内存.管道.COM等.

玩转Windows服务系列汇总(9篇文章)

玩转Windows服务系列汇总 创建Windows服务Debug.Release版本的注册和卸载及其原理无COM接口Windows服务启动失败原因及解决方案服务运行.停止流程浅析Windows服务小技巧命令行管理Windows服务Windows服务启动超时时间使用Boost.Application快速构建Windows服务给Windows服务添加COM接口 http://www.cnblogs.com/hbccdf/p/summary_of_windows_service.html

[转]玩转Windows服务系列——命令行管理Windows服务

本文转自:http://www.cnblogs.com/hbccdf/p/managewindowsservicewithcmd.html 说到Windows服务的管理就不得不说通过命令行的方式管理Windows服务,因为无论是系统管理员,还是通过编程的方式调用cmd命令,命令行都是非常方便以及强大的工具. 接下来就看一下如何通过cmd命令管理Windows服务. 管理Windows服务的主要cmd命令 管理Windows服务的命令应该有很多,但是我所了解到的命令主要有两个:sc.net. 说是