查找错误依赖项的已发布输出



Goal

我有两个.NET Core控制台应用程序 -Application.dllUpdater.dll。我想使用 MSBuild 发布将Updater.dll(及其所有依赖项)与Application.dll一起发布到文件夹中。

我尝试过什么(最小工作示例)

我创建了一个非常小的玩具示例来说明我的问题(解决方案可以在这里下载)。

更新程序源和项目文件如下所示:

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="..UpdaterUpdater.csproj" />
</ItemGroup>
<!-- make sure the runtimeconfig of the referenced Updater is also included in the output -->
<ItemGroup>
<Content Include="..Updaterbin$(Configuration)netcoreapp1.1Updater.runtimeconfig.json" CopyToOutputDirectory="Always"></Content>
</ItemGroup>
</Project>

蚂蚁这是我用于应用程序的发布配置:

<?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>binDebugPublishOutput</PublishDir>
</PropertyGroup>
</Project>

我可以在调试器中成功构建和运行这两个程序。我还可以发布应用程序项目,并包括更新程序及其依赖项:

PublishOutputruntimesunixlibnetstandard1.5System.ServiceProcess.ServiceController.dll
PublishOutputruntimeswinlibnetstandard1.5System.ServiceProcess.ServiceController.dll
PublishOutputApplication.deps.json
PublishOutputApplication.dll
PublishOutputApplication.pdb
PublishOutputApplication.runtimeconfig.json
PublishOutputUpdater.dll
PublishOutputUpdater.pdb
PublishOutputUpdater.runtimeconfig.json

我也可以从发布的输出运行应用程序本身,如下所示:

dotnet Application.dll

问题

我无法从已发布的输出运行更新程序。当我尝试这个时:

dotnet Updater.dll

它崩溃并出现以下异常:

未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集"System.ServiceProcess.ServiceController,版本=4.1.0.0,区域性=中性,公钥令牌=b03f5f7f11d50a3a"。系统找不到指定的文件。 at Updater.Program.Main(String[] args)

有趣的是,它正在寻找System.ServiceProcess.ServiceController的4.1.0.0版本。但正如您在项目文件中看到的那样,我实际上是在引用版本 4.3.0

我在这里做错了什么?我需要更改什么才能从已发布的输出运行更新程序?

已发布的输出中缺少Updater.deps.json。需要在应用程序的项目文件中进行以下更改。

以前:

<ItemGroup>
<Content Include="..Updaterbin$(Configuration)netcoreapp1.1Updater.runtimeconfig.json" CopyToOutputDirectory="Always"></Content>
</ItemGroup>

后:

<ItemGroup>
<Content Include="..Updaterbin$(Configuration)netcoreapp1.1Updater.runtimeconfig.json" CopyToOutputDirectory="Always"></Content>
<Content Include="..Updaterbin$(Configuration)netcoreapp1.1Updater.deps.json" CopyToOutputDirectory="Always"></Content>
</ItemGroup>

最新更新