添加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的属性信息(服务安装信息)
3、修改serviceInstall1的属性信息(服务本身的信息)
到这里,服务安装程序已经完成。
接下来,编写安装和卸载的批处理文件。
批处理文件
安装
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,回车
查看系统服务,已成功卸载
参考: