How to deploy Windows Service projects with Team Build 2010

北城以北 提交于 2019-11-29 09:29:06

问题


I have a VS2010 solution, which includes several Windows Service projects. I need to deploy these services as part of a build in Team Build 2010, and the Windows Services have to be deployed on several Windows Server machines.

How can I do this?


回答1:


You could conditionally invoke the SC.exe command from your Windows Service project file (*.csproj) to install the Windows Service on a remote machine.

Here's an example:

<PropertyGroup>
  <DeployWinService>false</DeployWinService>
  <WinServiceName>MyService</WinServiceName>
  <TargetWinServiceHost Condition="'$(TargetWinServiceHost)' == ''">localhost</TargetWinServiceHost>
</PropertyGroup>

<Target Name="AfterCompile">
  <CallTarget Targets="PublishWinService" />
</Target>

<Target Name="PublishWinService"
        Condition="'$(DeployWinService)' == 'true'">
  <Exec Command="sc stop $(WinServiceName)" ContinueOnError="true" />
  <Exec Command="sc \\$(TargetWinServiceHost) create $(WinServiceName) binpath= '$(OutDir)\$(AssemblyName).exe' start= auto" />
</Target>

Here we are defining the custom MSBuild properties DeployWinService and TargetWinServiceHost that are used to control whether the output of the Windows Service project will be installed after compilation and to which machine. The WinServiceName property simply specifies the name that the Windows Service will have on the target machine.

In your build definition you'll have to explicitly set the DeployWinService and TargetWinServiceHost properties in the MSBuild Arguments field of the Advanced section:

/p:DeployWinService=true;TargetWinServiceHost=MACHINENAME

Related resources:

  • How to create a Windows service by using Sc.exe
  • Common MSBuild Project Properties


来源:https://stackoverflow.com/questions/5788648/how-to-deploy-windows-service-projects-with-team-build-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!