发布的输出寻找错误的依赖

时间:2017-03-30 12:42:45

标签: .net msbuild .net-core publish visual-studio-2017

目标

我有两个.NET Core控制台应用程序 - Application.dllUpdater.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这是我用于Application的发布配置:

<?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>

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

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

问题

我无法从已发布的输出中运行Updater。当我尝试这个时:

dotnet Updater.dll

崩溃时出现以下异常:

  

未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集'System.ServiceProcess.ServiceController,Version = 4.1.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'。该系统找不到指定的文件。      在Updater.Program.Main(String [] args)

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

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

1 个答案:

答案 0 :(得分: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>