如何为ASP.NET Core 2.0 Web API制作MSI安装包并部署为Windows服务?
Hey there! Deploying your ASP.NET Core 2.0 Web API as a Windows Service via MSI is totally feasible—let’s work through the issues you’ve hit and walk through a solid end-to-end solution.
First: Make sure your Web API is set up to run as a Windows Service
Before diving into MSI packaging, confirm your project is configured correctly for Windows Service execution:
- Install the
Microsoft.AspNetCore.Hosting.WindowsServicesNuGet package (this is non-negotiable for running ASP.NET Core apps as services). - Update your
Program.csto handle both console and service modes:public static void Main(string[] args) { var isRunningAsService = !(Debugger.IsAttached || args.Contains("--console")); var webHostBuilder = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup<Startup>() .UseUrls("http://localhost:5000"); // Adjust to your API's URL // When running as a service, set content root to the service's installation directory if (isRunningAsService) { var exePath = Process.GetCurrentProcess().MainModule.FileName; webHostBuilder.UseContentRoot(Path.GetDirectoryName(exePath)); } var host = webHostBuilder.Build(); if (isRunningAsService) { host.RunAsService(); } else { host.Run(); } }
Fix 1: Setup Project not loading dependencies
The old Visual Studio Setup Project has poor out-of-the-box support for .NET Core apps, especially 2.0. Here’s how to fix the dependency issue:
- First, publish your API as a self-contained deployment:
Open your project’s terminal and run:
This packages your API with all .NET Core runtime dependencies and libraries into a single folder—no need to rely on the target machine having .NET Core installed.dotnet publish -c Release -r win-x64 --self-contained - Update your Setup Project:
Instead of adding the project’s output directly, add all files from the published folder (usually inbin/Release/netcoreapp2.0/win-x64/publish) to your Setup Project. This ensures every required dependency gets included in the MSI. - Add service installation logic:
You’ll need a custom action to register the service during installation. Add aServiceInstallercomponent to your Setup Project, or useinstallutil.exein a post-install action pointing to your API’s executable.
Fix 2: WiX Toolset build error ("must be installed")
If you’ve already tried common fixes but still get this error, here are some less obvious checks:
- Verify version compatibility: Ensure you’re using WiX Toolset v3.11+ that matches your Visual Studio version (e.g., WiX 3.11.2+ works with VS 2017, which is typical for ASP.NET Core 2.0 projects).
- Check the VS Extension: Open Visual Studio’s Extension Manager, search for "WiX Toolset Visual Studio Extension", and confirm it’s installed and enabled. Sometimes the extension can become disabled after VS updates.
- Validate PATH environment variable: WiX installs its tools to
C:\Program Files (x86)\WiX Toolset v3.x\bin. Check that this path is in your system’s PATH. Test it by opening a command prompt and runningcandle.exe—if it’s not found, manually add the path to your system environment variables. - Clean project cache: Delete your WiX project’s
binandobjfolders, then restart Visual Studio and rebuild. Cached build artifacts often cause this error. - Align NuGet and tool versions: If you’re using WiX NuGet packages in your project, make sure their version matches the installed WiX Toolset version (e.g., if you have WiX 3.14 installed, use NuGet packages v3.14.x).
Full WiX MSI Deployment Walkthrough (Recommended)
WiX is the most reliable way to build MSIs for .NET Core services once you get past the setup hurdles. Here’s a step-by-step workflow:
- Publish your API as a self-contained deployment (using the
dotnet publishcommand above). - Create a WiX Setup Project in Visual Studio (make sure the WiX extension is installed).
- Auto-generate file components with Heat:
Open a command prompt, navigate to your WiX project folder, and run:
This generates aheat dir "C:\Path\To\Your\Published\Api" -dr INSTALLFOLDER -cg ProductComponents -gg -g1 -sfrag -srd -out Files.wxsFiles.wxsfile that includes all your API’s files as WiX components. Add this file to your WiX project. - Add service installation logic to your main
Product.wxsfile:<Directory Id="TARGETDIR" Name="SourceDir"> <Directory Id="ProgramFilesFolder"> <Directory Id="INSTALLFOLDER" Name="YourApiService" /> </Directory> </Directory> <ComponentGroupRef Id="ProductComponents" /> <!-- Service installation component --> <Component Id="ServiceComponent" Directory="INSTALLFOLDER" Guid="*"> <File Id="ApiExe" Source="C:\Path\To\Your\Published\Api\YourApi.exe" KeyPath="yes" /> <ServiceInstall Id="ApiServiceInstaller" Name="YourApiService" DisplayName="Your ASP.NET Core Web API Service" Description="Hosts your ASP.NET Core 2.0 Web API as a Windows Service" Start="auto" Account="LocalSystem" ErrorControl="normal" Type="ownProcess" /> <ServiceControl Id="ApiServiceControl" Name="YourApiService" Start="install" Stop="both" Remove="uninstall" Wait="yes" /> </Component> <Feature Id="ProductFeature" Title="YourApiService" Level="1"> <ComponentGroupRef Id="ProductComponents" /> <ComponentRef Id="ServiceComponent" /> </Feature> - Build the WiX project—this will generate your MSI. When you run the MSI, it will install your API to
Program Files, register it as a Windows Service, and start it automatically.
内容的提问来源于stack exchange,提问作者Mohit Bora

