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. PHP 循环

    PHP 中的循环语句用于执行相同的代码块指定的次数. 循环 在您编写代码时,您经常需要让相同的代码块运行很多次.您可以在代码中使用循环语句来完成这个任务. 在 PHP 中,我们可以使用下列循环语句: ...

  2. zookeeper工作原理解析

    zookeeper一般用于distributed locking,并不适合用于distributed storage,由于zookeeper的每一个node.也叫做znode的存储容量限制是1M. z ...

  3. iOS 推断设备为iPhone还是iPad

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { self.viewControlle ...

  4. Win10系统安装Office2016错误,提示需要更新客户端的解决方法

    下载Troubleshoot.zip 运行 OffScrubC2R.vbs ok

  5. IOS版微信小视频导出方法

    1.在电脑上连接手机,打开iTools 选择 应用-应用-文件共享. 2.依次打开/Library/WechatPrivate/6e2809aac61608de6a6cc55d9570d25b/Sig ...

  6. Java Method Area

    ref http://blog.csdn.net/huangfan322/article/details/53220169 https://docs.oracle.com/javase/specs/j ...

  7. 【Java虚拟机】运行时数据区

    Java在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途.创建和销毁的时间,有一些是随虚拟机的启动而创建,随虚拟机的退出而销毁,有些则是与线程一一对应,随 ...

  8. JDBC实例--JDBC连接池技术解密,连接池对我们不再陌生

    一.为什么我们要用连接池技术? 前面的数据库连接的建立及关闭资源的方法有些缺陷.统舱传统数据库访问方式:一次数据库访问对应一个物理连接,每次操作数据库都要打开.关闭该物理连接, 系统性能严重受损. 解 ...

  9. Linux内核源代码分析方法

    Linux内核源代码分析方法   一.内核源代码之我见 Linux内核代码的庞大令不少人"望而生畏",也正由于如此,使得人们对Linux的了解仅处于泛泛的层次.假设想透析Linux ...

  10. What is the difference between J2EE and Spring

    来自于:https://www.quora.com/What-is-the-difference-between-J2EE-and-Spring Lot of people specially tho ...