【发布时间】:2017-03-30 12:42:44
【问题描述】:
目标
我有两个 .NET Core 控制台应用程序 - Application.dll 和 Updater.dll。我想将Updater.dll(及其所有依赖项)与Application.dll 一起发布到使用MSBuild 发布的文件夹中。
我尝试了什么(最小的工作示例)
我创建了一个非常小的玩具示例来说明我的问题(解决方案可以下载here)。
Updater 源和项目文件如下所示:
using System;
using System.ServiceProcess;
namespace Updater
{
class Program
{
static void Main(string[] args)
{
const string SERVICE_NAME = "TestService";
try
{
var serviceController = new ServiceController(SERVICE_NAME);
Console.WriteLine($"Status of {SERVICE_NAME}: {serviceController.Status}");
}
catch
{
Console.WriteLine($"Error getting {SERVICE_NAME}");
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.ServiceProcess.ServiceController" Version="4.3.0" />
</ItemGroup>
</Project>
应用程序源和项目文件如下所示:
using System;
using System.Diagnostics;
namespace Application
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Updater\Updater.csproj" />
</ItemGroup>
<!-- make sure the runtimeconfig of the referenced Updater is also included in the output -->
<ItemGroup>
<Content Include="..\Updater\bin\$(Configuration)\netcoreapp1.1\Updater.runtimeconfig.json" CopyToOutputDirectory="Always"></Content>
</ItemGroup>
</Project>
Ant 这是我用于应用程序的发布配置:
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Debug</Configuration>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PublishDir>bin\Debug\PublishOutput</PublishDir>
</PropertyGroup>
</Project>
我可以在调试器中成功构建和运行这两个程序。我还可以发布应用程序项目和更新程序及其依赖项:
PublishOutput\runtimes\unix\lib\netstandard1.5\System.ServiceProcess.ServiceController.dll
PublishOutput\runtimes\win\lib\netstandard1.5\System.ServiceProcess.ServiceController.dll
PublishOutput\Application.deps.json
PublishOutput\Application.dll
PublishOutput\Application.pdb
PublishOutput\Application.runtimeconfig.json
PublishOutput\Updater.dll
PublishOutput\Updater.pdb
PublishOutput\Updater.runtimeconfig.json
我也可以像这样从已发布的输出中运行应用程序本身:
dotnet Application.dll
问题
我无法从发布的输出运行更新程序。当我尝试这个时:
dotnet Updater.dll
它崩溃并出现以下异常:
未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集“System.ServiceProcess.ServiceController,版本=4.1.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a”。该系统找不到指定的文件。 在 Updater.Program.Main(String[] args)
有趣的是,它正在寻找 System.ServiceProcess.ServiceController 的 4.1.0.0 版本。但正如您在项目文件中看到的,我实际上引用的是 4.3.0 版
我在这里做错了什么?我需要更改哪些内容才能从发布的输出运行更新程序?
【问题讨论】:
标签: .net msbuild .net-core publish visual-studio-2017