【发布时间】:2018-11-11 03:28:31
【问题描述】:
我正在创建一个 ASP.net core 2.0 WEB API 项目,该项目需要在使用 MSI 设置的系统上部署为 Windows 服务。有什么办法可以吗?
我已经尝试过的事情:
【问题讨论】:
标签: c# asp.net-core wix windows-services windows-installer
我正在创建一个 ASP.net core 2.0 WEB API 项目,该项目需要在使用 MSI 设置的系统上部署为 Windows 服务。有什么办法可以吗?
我已经尝试过的事情:
【问题讨论】:
标签: c# asp.net-core wix windows-services windows-installer
时间紧迫,只是一些链接,看看它是否能让你继续前进:
基本上:
我喜欢将主 WiX 安装目录中的 bin 文件夹添加到 Path 环境变量中,以便能够从任何地方调用 WiX 构建工具 - candle.exe、light.exe、etc...。
您也可以在 Visual Studio 之外编译 WiX 源文件。最简单的形式:
set SetupName=MySetup
candle.exe %SetupName%.wxs >> %SetupName%.log
light.exe -out %SetupName%.msi %SetupName%.wixobj >> %SetupName%.log
或者,没有线路噪音:
candle.exe Setup.wxs
light.exe -out Setup.msi Setup.wixobj
类似问题:
【讨论】:
我通过使用 Wix 创建一个 msi 解决了这个问题。我看了this Video 并按照它的指示进行操作。
为了创建我使用 NSSM 的 Windows 服务,我复制了 nssm.exe 作为安装程序的一部分。 使用以下命令: nssm 安装服务名称
为了自动创建服务,我使用了 WiX 的 CustomAction。
【讨论】:
要为 .Net core 2 创建 MSI……首先像这样发布您的项目
dotnet publish --configuration Release --runtime win7-x64 --self-contained false --output c:\outputfolder
您可以通过添加使其成为 .wixproj 的一部分
<Target Name="BeforeBuild">
<PropertyGroup>
<BasePath>$(SolutionDir)\publish\bin\$(Configuration)\FilesToPackage</BasePath>
</PropertyGroup>
<!-- Clean previous build folder -->
<Exec Command="rd /s /q $(BasePath)" />
<!-- Publish dotnet core app -->
<Exec Command="dotnet publish $(MSBuildThisFileDirectory)\..\..\src\yourproject.csproj -r win-x64 --self-contained false --output $(BasePath)" />
<!-- Get assembly version -->
<GetAssemblyIdentity AssemblyFiles="$(BasePath)\yourproject.dll">
<Output TaskParameter="Assemblies" ItemName="AssemblyVersion" />
</GetAssemblyIdentity>
<!-- Define some variables we need -->
<PropertyGroup>
<DefineConstants>ProductVersion=%(AssemblyVersion.Version);BasePath=$(BasePath)</DefineConstants>
</PropertyGroup>
<HeatDirectory
OutputFile="YourServiceComponent.wxs"
DirectoryRefId="INSTALLFOLDER"
ComponentGroupName="yourproject_component"
SuppressCom="true" Directory="$(BasePath)"
SuppressFragments="true"
SuppressRegistry="true"
SuppressRootDirectory="true"
AutoGenerateGuids="false"
GenerateGuidsNow="true"
ToolPath="$(WixToolPath)"
PreprocessorVariable="var.BasePath"
Transforms="RemoveFiles.xslt"
/>
</Target>
Heat 将使用输出中的所有文件创建 wxs 文件,但您需要删除 yourservice.exe,因此将该信息添加到 RemoveFiles.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="pdb-search" match="wix:Component[contains(wix:File/@Source, '.pdb')]" use="@Id" />
<xsl:template match="wix:Component[key('pdb-search', @Id)]" />
<xsl:template match="wix:ComponentRef[key('pdb-search', @Id)]" />
<xsl:key name="service-search" match="wix:Component[contains(wix:File/@Source, 'Your.Service.exe')]" use="@Id"/>
<xsl:template match="wix:Component[key('service-search', @Id)]"/>
<xsl:template match="wix:ComponentRef[key('service-search', @Id)]"/>
</xsl:stylesheet>
最后,您希望 Wix 向 Windows 服务控制管理器 (SCM) 注册您的服务,因此将以下内容添加到您的 Product.wxs
<Fragment>
<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
<ComponentGroupRef Id="yourproject_component" />
<Component Id="ServiceAssemblyComponent" Guid="{GUID}">
<File Id="ServiceAssembly" Source="$(var.BasePath)\Your.Service.exe" KeyPath="yes" />
<ServiceInstall Id="ServiceInstallation" DisplayName="$(var.ProductName)" Name="$(var.ProductName)" ErrorControl="normal" Start="auto" Type="ownProcess" Vital="yes" />
<ServiceControl Id="ServiceControl" Name="$(var.ProductName)" Stop="both" Remove="uninstall" />
</Component>
</ComponentGroup>
</Fragment>
【讨论】: