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,然后打开这个文件,里面代码如下:

 using System.ServiceProcess;
using System.ServiceModel;
using WcfServices.Services; namespace WcfServices.HostingWindowsService
{
public partial class CalculatorHost : ServiceBase
{
private ServiceHost _host; public ServiceHost Host
{
get { return _host; }
set { _host = value; }
} public CalculatorHost()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
Host = new ServiceHost(typeof(CalculatorService));
Host.Open();
} protected override void OnStop()
{
if (Host != null)
{
Host.Close();
Host = null;
} }
}
}

4.CalculatorHost.cs[Design]的界面上右键,选择添加安装器(Add Installer)。这时,项目里会自动生成一个ProjectInstaller.cs文件。

5.打开ProjectInstaller.cs[Design]的设计界面,将会出现下图:

6.选中serviceInstaller1,打开它的属性视图(Ctrl W,P),修改属性。如下图所示:

7.接着选中serviceProcessInstaller1,打开它的属性视图,修改属性。如下图:(这里服务账号也可以是其他的。)

8.接下来我们看看这个项目里的program.cs文件。代码如下:

 using System.ServiceProcess;

 namespace WcfServices.HostingWindowsService
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new CalculatorHost()
};
ServiceBase.Run(ServicesToRun);
}
}
}

9.这些都做好了之后,在WcfServices.HostingWindowsSerice项目中添加服务端的配置文件。这个在上一节中也写过,代码如下:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="CaclulaterBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="CaclulaterBehavior" name="WcfServices.Services.CalculatorService">
<endpoint address="http://localhost:8080/calculatorservice" binding="basicHttpBinding"
bindingConfiguration="" contract="WcfServices.Contracts.ICalculator" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/calculatorservice" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>

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

WCF寄宿到Windows Service的更多相关文章

  1. WCF寄宿到Windows Service[1]

    WCF寄宿到Windows Service 返回 在前面创建一个简单的WCF程序,我们把WCF的服务寄宿到了Host这个控制台项目中了.下面将介绍如何把WCF的服务寄宿到Windows服务中(源代码) ...

  2. 重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载

    参考地址:http://www.cnblogs.com/zhili/p/4039111.html 一.如何在Windows Services中寄宿WCF服务 第一步:创建Windows 服务项目,具体 ...

  3. WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)

    上接    WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) 七 WCF服务的Windows 服务程序寄宿 这种方式的服务寄宿,和IIS一样有一个一样 ...

  4. 编写寄宿于windows服务的WCF服务

    由于业务中有些任务需要在后台静默长期运行,或者有些服务队响应的要求比较苛刻,这样的WCF服务就不适合寄宿于IIS中.IIS每隔一段时间w3wp进程会闲置超时,造成服务的运行停止,因此这种耗时或者定时任 ...

  5. 将WCF寄宿在托管的Windows服务中

    在我之前的一篇博客中我介绍了如何发布WCF服务并将该服务寄宿于IIS上,今天我再来介绍一种方式,就是将WCF服务寄宿在Windows服务中,这样做有什么好处呢?当然可以省去部署IIS等一系列的问题,能 ...

  6. WCF寄宿控制台.WindowsService.WinFrom.WebAPI寄宿控制台和windows服务

    先建立wcf类库.会默认生成一些试用代码.如下: public class Service1 { public string GetData(int value) { return string.Fo ...

  7. 创建寄宿在Windows服务中的WCF服务

    1.创建Windows服务项目 2.Server1改名为你想要的名称,比如WinServer 3.在项目中新建一个WCF文件夹,用于存放wcf服务文件. 注:在WcfServer类的上面还要添加 [S ...

  8. WCF Windows Service Using TopShelf and ServiceModelEx z

    http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are ...

  9. WCF - Windows Service Hosting

    WCF - Windows Service Hosting The operation of Windows service hosting is a simple one. Given below ...

随机推荐

  1. xcode 打包

    iOS的要安装Xcode,否则执行下面命令的时候报错 ionic platform add ios ionic build ios ionic emulate ios 方法二: 1.打开终端 -- b ...

  2. 如何固定OpenERP顶的主菜单,方便滚动至第二屏以及多屏时,快速切换主菜单

    如何固定OpenERP顶的主菜单,方便滚动至第二屏以及多屏时,快速切换主菜单 作者:广州-步科,来自OpenERP应用群() 将“addons\web\static\src\css”目录下的“base ...

  3. 【docker】挂载web应用

    现在将webapps目录挂载在宿主机目录,方便运维 docker run -p 8090:8080 --name app -v /usr/app:/usr/local/tomcat/webapps d ...

  4. 图文例解C++类的多重继承与虚拟继承

    文章导读:C++允许为一个派生类指定多个基类,这样的继承结构被称做多重继承. 在过去的学习中,我们始终接触的单个类的继承,但是在现实生活中,一些新事物往往会拥有两个或者两个以上事物的属性,为了解决这个 ...

  5. iOS-仿京东6位密码支付输入框

    概述 用于安全支付的密码支付输入框. 详细 代码下载:http://www.demodashi.com/demo/10709.html 开发需求中有时候我们需要用于安全支付的功能, 需要设置APP钱包 ...

  6. Vijos1935不可思议的清晨题解

    - 题目来源 https://vijos.org/p/1935 描写叙述 今天,是2015年2月14日,星期六,情人节. 这真是一个不可思议的日子.今天早上.我打开窗户,太阳居然从西側升了起来. 我与 ...

  7. 在ListView的右边添加字母列表

    在ListView的右边添加字母列表,点击某个字母时,列表就滚动到预期位置. <!-- 数字和字母栏在标题栏下边并且停靠在右边 --> <com.txrj.sms.component ...

  8. Java设计模式中的单例模式

    有时候在实际项目的开发中,我们会碰到这样一种情况,该类只允许存在一个实例化的对象,不允许存在一个以上的实例化对象,我们将这种情况称为Java设计模式中的单例模式.设计单例模式主要采用了Java的pri ...

  9. SQL plus连接远程Oralce数据库

    如果要连接远程数据库,传统的一定可行的方法是在本地装一个oracle,然后使用“Network Configuration Assistant”配置,之后用PL/SQL Dev连接 oracle官网上 ...

  10. 【LeetCode】19. Remove Nth Node From End of List (2 solutions)

    Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...