zoukankan      html  css  js  c++  java
  • WCF寄宿windows服务一

    如果只是寄宿单个wcf服务,方法很简单,步骤:
    1、创建好一个windows服务。关于windows服务内容见:http://www.cnblogs.com/zhaow/p/7866916.html
    2、在windows服务的OnStart、OnStop方法中,寄宿服务。实现参考如下:

    namespace MyWindowsServiceHost
    {
        public partial class MyServiceHost : ServiceBase
        {
            private ServiceHost myHost = new ServiceHost(typeof(MyService));
            public MyServiceHost()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                myHost.Open();
            }
    
            protected override void OnStop()
            {
                myHost.Close();
            }
        }
    }
    

    3、将WCF相关配置copy到windows服务的app.config中。
    4、写一个用于安装和卸载windows服务的bat文件,如:

    @echo off
    set /p var=是否要安装 WCF 服务(Y/N):
    if "%var%" == "y" (goto install) else (goto batexit)
    
    :install
    copy C:WindowsMicrosoft.NETFrameworkv4.0.30319InstallUtil.exe  InstallUtil.exe /Y
    call InstallUtil.exe WindowsService.exe
    pause
    
    :batexit
    exit
    
    //卸载
    
    @echo off
    set /p var=是否要卸载 WCF服务(Y/N):
    if "%var%" == "y" (goto uninstall) else (goto batexit)
    
    :uninstall
    copy C:WINDOWSMicrosoft.NETFrameworkv2.0.50727InstallUtil.exe  InstallUtil.exe /Y
    call InstallUtil.exe /u WindowsService.exe
    pause
    
    :batexit
    exit
    

    5、关于.bat批处理文件
    新建一个.txt,把代码copy进去,再把扩展名改成.bat。批处理文件要放到Windows服务程序生成的exe同级目录下。

    注意32位系统和64位系统的区别:
    32位操作系统:

    @echo off
    set /p var=是否要安装 WCF 服务(Y/N):
    if "%var%" == "y" (goto install) else (goto batexit)
    
    :install
    copy C:WindowsMicrosoft.NETFrameworkv4.0.30319InstallUtil.exe  InstallUtil.exe /Y
    call InstallUtil.exe MyWindowsServiceHost.exe
    call sc start "MyServiceHost"
    pause
    
    :batexit
    exit
    

    64位操作系统:

    @echo off
    set /p var=是否要安装 WCF 服务(Y/N):
    if "%var%" == "y" (goto install) else (goto batexit)
    
    :install
    copy C:WindowsMicrosoft.NETFramework64v4.0.30319InstallUtil.exe  InstallUtil.exe /Y
    call InstallUtil.exe MyWindowsServiceHost.exe
    call sc start "MyServiceHost"
    pause
    
    :batexit
    exit
    

    如果是在x86平台下生成的Windows服务安装程序,即使是在64位操作系统下,也需要使用32位下的批处理文件。

    接下来,测试下服务的安装与卸载:

    安装卸载测试

    安装

    右键service_install.bat,以管理员身份运行

    安装

    输入y,回车

    安装

    查看系统服务,已成功安装

    查看服务

    卸载

    右键service_uninstall.bat,以管理员身份运行

    卸载

    输入y,回车

    卸载

    查看系统服务,已成功卸载

    查看服务

    本文参考:
    http://blog.csdn.net/doris_d/article/details/46739193
    https://www.cnblogs.com/stulife/archive/2011/04/14/2016118.html
    https://www.cnblogs.com/seekdream/p/6821348.html

  • 相关阅读:
    hihocoder 1049 后序遍历
    hihocoder 1310 岛屿
    Leetcode 63. Unique Paths II
    Leetcode 62. Unique Paths
    Leetcode 70. Climbing Stairs
    poj 3544 Journey with Pigs
    Leetcode 338. Counting Bits
    Leetcode 136. Single Number
    Leetcode 342. Power of Four
    Leetcode 299. Bulls and Cows
  • 原文地址:https://www.cnblogs.com/zhaow/p/8288614.html
Copyright © 2011-2022 走看看