我的目标是在 .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/