已发布的输出寻找错误的依赖关系
Published output looking for wrong dependency
目标
我有两个 .NET Core 控制台应用程序 - Application.dll
和 Updater.dll
。我想使用 MSBuild publish 将 Updater.dll
(及其所有依赖项)与 Application.dll
一起发布到一个文件夹。
我尝试了什么(最小工作示例)
我创建了一个非常小的玩具示例来说明我的问题(可以下载解决方案 here)。
更新程序源和项目文件如下所示:
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
它因以下异常而崩溃:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.ServiceProcess.ServiceController, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
at Updater.Program.Main(String[] args)
有趣的是,它正在寻找 System.ServiceProcess.ServiceController
的 4.1.0.0 版。但是正如您在项目文件中看到的那样,我实际上引用的是版本 4.3.0
我在这里做错了什么?我需要更改什么才能 运行 来自已发布输出的更新程序?
已发布的输出中缺少 Updater.deps.json
。应用程序的项目文件中需要进行以下更改。
之前:
<ItemGroup>
<Content Include="..\Updater\bin$(Configuration)\netcoreapp1.1\Updater.runtimeconfig.json" CopyToOutputDirectory="Always"></Content>
</ItemGroup>
之后:
<ItemGroup>
<Content Include="..\Updater\bin$(Configuration)\netcoreapp1.1\Updater.runtimeconfig.json" CopyToOutputDirectory="Always"></Content>
<Content Include="..\Updater\bin$(Configuration)\netcoreapp1.1\Updater.deps.json" CopyToOutputDirectory="Always"></Content>
</ItemGroup>
目标
我有两个 .NET Core 控制台应用程序 - Application.dll
和 Updater.dll
。我想使用 MSBuild publish 将 Updater.dll
(及其所有依赖项)与 Application.dll
一起发布到一个文件夹。
我尝试了什么(最小工作示例)
我创建了一个非常小的玩具示例来说明我的问题(可以下载解决方案 here)。
更新程序源和项目文件如下所示:
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
它因以下异常而崩溃:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.ServiceProcess.ServiceController, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. at Updater.Program.Main(String[] args)
有趣的是,它正在寻找 System.ServiceProcess.ServiceController
的 4.1.0.0 版。但是正如您在项目文件中看到的那样,我实际上引用的是版本 4.3.0
我在这里做错了什么?我需要更改什么才能 运行 来自已发布输出的更新程序?
Updater.deps.json
。应用程序的项目文件中需要进行以下更改。
之前:
<ItemGroup>
<Content Include="..\Updater\bin$(Configuration)\netcoreapp1.1\Updater.runtimeconfig.json" CopyToOutputDirectory="Always"></Content>
</ItemGroup>
之后:
<ItemGroup>
<Content Include="..\Updater\bin$(Configuration)\netcoreapp1.1\Updater.runtimeconfig.json" CopyToOutputDirectory="Always"></Content>
<Content Include="..\Updater\bin$(Configuration)\netcoreapp1.1\Updater.deps.json" CopyToOutputDirectory="Always"></Content>
</ItemGroup>