通过.NET Core中的UsingTask编写自定义任务无法构建(缺少程序集)

时间:2017-10-26 11:21:36

标签: .net-core

我的目标是在.NET Core中运行任务(类似于Rails中的rake),因此我可以在应用程序生命周期的正常控制流之外执行代码。我已经看到有像albacore这样的项目可以实现这一目标,但是我试图这样做,而不是引入一个单独的ruby依赖来实现这个目标。

msbuild任务写作上关注this article之后,我设法创建了一个简单的Task,可以按照建议实现ITask接口在文章中:

EcommerceSite /任务/ 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

所以我的问题是:

  1. 为什么我的Task尝试加载Microsoft.AspNetCore.Mvc.ViewFeatures,根据NuGet docs,包含:
      

    查看呈现功能,例如视图引擎,视图,视图组件和HTML帮助程序

  2. 我的Task什么都不做?

    1. 如何让它尝试加载此程序集,或者如果这不是一个选项,我该如何解决依赖项问题?

2 个答案:

答案 0 :(得分:1)

我今晚得到了这个工作。您可以看到my solution here

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

  1. 我在元素中包含了名称空间。一旦删除它并使用了类名,错误就消失了。
<UsingTask TaskName="PublishValidationTask"
AssemblyFile="C:\git\CustomBuildTargets\PublishPipelineTesting\bin\debug\netcoreapp2.0\PublishPipelineTesting.dll"
/>
  1. 仅当我设置 Importance =“ High” 时,文本才会显示在命令行中。
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
  1. 路径也使事情变得奇怪。我从绝对的路径开始,效果很好。从那里我可以找出相对路径,它只是从 bin 目录开始:
  

  AssemblyFile =“ bin \ debug \ netcoreapp2.0 \ PublishPipelineTesting.dll” />

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

  

C:\ git \ CustomBuildTargets \ PublishPipelineTesting> dotnet运行   PublishPipelineTesting.csproj

     

构建失败。请修复构建错误,然后再次运行。

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

  

C:\ git \ CustomBuildTargets \ PublishPipelineTesting> dotnet运行   PublishPipelineTesting.csproj
  世界,您好!

答案 1 :(得分:0)

我遇到的问题是包含我的任务的项目的目标是netcoreapp2.1框架...。我也将其更改为目标netstandard2.0,然后进入目标并更改了程序集文件路径是netstandard2.0而不是netcoreapp2.1。在那之后,我的自定义任务运行良好。