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. Linux网卡高级命令

    网卡的高级命令 [root@gechong ~]# mii-tool No interface specified usage: mii-tool [-VvRrwl] [-A media,... | ...

  2. vue 渲染流程

    1.DOM 节点树 高效的更新所有这些节点会是比较困难的,因为原生的DOM节点属性很多,渲染性能差. 2.虚拟 DOM “虚拟 DOM”是我们对由 Vue 组件树建立起来的整个 VNode 树的称呼. ...

  3. red5源代码编译并打包公布

    编译环境:ubuntu14.04/JDK7 步骤: 1.svn检出源代码(两种方式) svn co --depth empty https://github.com/Red5/red5-server ...

  4. 1、创建一个空白的xls和xlsx文件

    1.创建一个空白的xls文件 Step1:先引入库NPOI.dll文件 Step2: ①:实例化一个workbook,实为在内存表中创建一个xls文件 NPOI.HSSF.UserModel.HSSF ...

  5. 赵雅智_android多线程下载带进度条

    progressBar说明 在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度. 一个进度条也可不确定其进度.在不确定模式下, ...

  6. Web Service基础——四种客户端调用方式

    通过访问公网服务地址 http://www.webxml.com.cn/zh_cn/index.aspx 来演示四种不同的客户端调用方式 1. 生成客户端调用方式 1.1 Wsimport命令介绍 首 ...

  7. JavaScript-jQuery插件开发全解析

    摘自:http://www.iteye.com/topic/545971 jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法 ...

  8. HDUOJ----2647Reward

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. Linux时间子系统(四) timekeeping

    一.前言 timekeeping模块是一个提供时间服务的基础模块.Linux内核提供各种time line,real time clock,monotonic clock.monotonic raw ...

  10. Spring注解方式配置说明

    1.<context:annotation-config/>与<context:component-scan base-package=”XX.XX”/> 在基于主机方式配置S ...