Windows服务发布WCF服务

添加Windows服务项目

1、添加项目

添加项目

项目初始化

2、添加引用

  • 添加WCF服务的Services库的引用

  • 添加System.ServiceModel的引用

3、修改Service1的属性(可选)

  • 解决方案管理器中右键 Service1.cs,重命名为 MyServiceHost.cs

  • 设计视图中右键属性,修改ServiceName为MyServiceHost

修改服务属性

4、切换到代码视图,编写服务启动与停止的代码

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();
        }
    }
}

5、配置App.config,直接将Service项目中的system.serviceModel的配置拷贝过来就可以

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="webHttpBehavior">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="metadata" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBinding" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="webHttpBehavior" name="Services.MyService">
        <endpoint address="" behaviorConfiguration="webHttpBehavior"
                  binding="webHttpBinding" bindingConfiguration="webHttpBinding"
                  contract="Services.IMyService"
                  name="myservice"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9998/mywcfservice" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

到这里,可以发布 MyService 服务的 Windows Service 宿主程序已经完成了。

接下来就是为该服务添加安装程序。


添加Windows服务安装程序

1、在MyServiceHost的设计视图,右键“添加安装程序”

添加安装程序

2、修改serviceProcessInstall1的属性信息(服务安装信息)

serviceProcessInstall1

3、修改serviceInstall1的属性信息(服务本身的信息)

serviceInstal1

到这里,服务安装程序已经完成。

接下来,编写安装和卸载的批处理文件。


批处理文件

安装

32位操作系统

service_install.bat

@echo off
set /p var=是否要安装 WCF 服务(Y/N):
if "%var%" == "y" (goto install) else (goto batexit)

:install
copy C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.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:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe  InstallUtil.exe /Y
call InstallUtil.exe MyWindowsServiceHost.exe
call sc start "MyServiceHost"
pause

:batexit
exit

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

卸载

@echo off
set /p var=是否要卸载 WCF服务(Y/N):
if "%var%" == "y" (goto uninstall) else (goto batexit)

:uninstall
call sc stop "MyServiceHost"
call sc delete "MyServiceHost"
pause

:batexit
exit

批处理文件要放到Windows服务程序生成的exe同级目录下。

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


安装卸载测试

安装

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

安装

输入y,回车

安装

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

查看服务

卸载

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

卸载

输入y,回车

卸载

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

查看服务


参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值