.net-core - 在 .NET Core 中通过 UsingTask 编写自定义任务一直无法构建(缺少程序集)

标签 .net-core

我的目标是在 .NET Core 中运行任务(类似于 Rails 领域中的 rake),这样我就可以在应用程序生命周期的正常控制流之外执行代码。我看到有这样的项目 albacore据说可以做到这一点,但我正在尝试以“.NET 方式”来做到这一点,而不是引入单独的 ruby​​ 依赖项来实现这一点。

关注后 this article关于 msbuild 的任务写作,我设法创建了一个简单的 Task实现 ITask文章中建议的界面:

电子商务网站/任务/ScrapeAll.cs

using System;
using Microsoft.Build.Framework;  
using Microsoft.Build.Utilities;

namespace Tasks
{
    public class ScrapeAll : Task
    {
        public override bool Execute()
        {
            return true;
        }
    }
}

我的 .csproj文件使用 UsingTask元素来注册我的任务代码,我有一个 Target调用任务:

EcommerceSite/EcommerceSite.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup> 

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.3" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
    <PackageReference Include="Microsoft.Build.Framework" Version="15.3.409" />
    <PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.409" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Views\ItemPage\" />
    <Folder Include="Tasks\" />
  </ItemGroup>

  <UsingTask TaskName="Tasks.ScrapeAll" AssemblyFile="bin/Debug/netcoreapp2.0/EcommerceSite.dll" />

  <Target Name="ScrapeAll">
    <Tasks.ScrapeAll />
  </Target>
</Project>

所以现在,在我的命令行上,我可以调用:dotnet msbuild /t:ScrapeAll然而,我收到此错误:
error MSB4062: The "Tasks.ScrapeAll" task could not be loaded from the assembly 
[redacted]/EcommerceSite/bin/Debug/netcoreapp2.0/EcommerceSite.dll. 
Could not load file or assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures, 
Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. 
The system cannot find the file specified.

[redacted]/EcommerceSite/EcommerceSite.csproj(33,5): error MSB4062:  
Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, 
and that the task contains a public class that implements Microsoft.Build.Framework.ITask

所以我的问题是:
  • 为什么是我的 Task正在尝试加载 Microsoft.AspNetCore.Mvc.ViewFeatures ,根据 NuGet docs ,包含:

    view rendering features such as view engines, views, view components, and HTML helpers


  • 其中我的Task什么都没有?
  • 我如何让它尝试加载这个程序集,或者如果这不是一个选项,我如何解决依赖问题?
  • 最佳答案

    我今晚开始工作了。你可以看到my solution here .

    本质上,我遇到了两个问题:

  • 我在元素中包含命名空间。一旦我删除它并只使用类名,错误就消失了。

  • <UsingTask TaskName="PublishValidationTask"
    AssemblyFile="C:\git\CustomBuildTargets\PublishPipelineTesting\bin\debug\netcoreapp2.0\PublishPipelineTesting.dll"
    />
    

  • 只有当我设置 重要性="高"文本会显示在命令行上。

  • C:\git\CustomBuildTargets\PublishPipelineTesting>dotnet msbuild PublishPipelineTesting.csproj
    Microsoft (R) Build Engine version 15.7.179.6572 for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      PublishPipelineTesting -> C:\git\CustomBuildTargets\PublishPipelineTesting\bin\Debug\netcoreapp2.0\PublishPipelineTesting.dll
      This is a test
      Hello World
    

  • 路径也让事情变得奇怪。我从绝对路径开始,效果很好。从那里我能够找出相对路径,它只是从 开始。垃圾箱目录:

  • AssemblyFile="bin\debug\netcoreapp2.0\PublishPipelineTesting.dll" />



    一旦我开始工作并运行自定义任务,返回 true/false 将允许构建通过或失败,这正是我想要的。当返回“false”时,我现在会得到:

    C:\git\CustomBuildTargets\PublishPipelineTesting>dotnet run PublishPipelineTesting.csproj

    The build failed. Please fix the build errors and run again.



    当任务返回 true 时,构建将通过,项目将运行:

    C:\git\CustomBuildTargets\PublishPipelineTesting>dotnet run PublishPipelineTesting.csproj
    Hello World!

    关于.net-core - 在 .NET Core 中通过 UsingTask 编写自定义任务一直无法构建(缺少程序集),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46952968/

    相关文章:

    c# - 在单独的表 EF Core 中插入 2 个项目

    在 Azure 中运行的 .NET Core 中的 Azure WebJob

    c# - 创建一个平台来处理未知实体的 CRUD 场景

    c# - ConfigurationManager 是否与 ASP.NET Core 的 appsettings.json 一起工作?

    c# - HtmlHelper 的 'ViewContext.Controller' 发生了什么?

    c# - 在 dotnet core 中添加 Firebase 日志事件(分析)

    asp.net-core - Asp.net Core 如何在 ViewModel 中使用 ReflectionIT.Mvc.Paging?

    c# - 如何在 Ubuntu 上运行已经开发的 ASP.NET Core 应用程序?

    c# - C#中return()是什么意思?

    .net-core - EntityFramework Core 数据库首先,Scaffold-DbContext A positional parameter cannot be found that accepts argument '--use-database-names' 错误