我的目标是在.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
所以我的问题是:
Task
尝试加载Microsoft.AspNetCore.Mvc.ViewFeatures
,根据NuGet docs,包含:
查看呈现功能,例如视图引擎,视图,视图组件和HTML帮助程序
我的Task
什么都不做?
答案 0 :(得分:1)
我今晚得到了这个工作。您可以看到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运行 PublishPipelineTesting.csproj
构建失败。请修复构建错误,然后再次运行。
当任务返回true时,构建将通过,项目将运行:
C:\ git \ CustomBuildTargets \ PublishPipelineTesting> dotnet运行 PublishPipelineTesting.csproj
世界,您好!
答案 1 :(得分:0)
我遇到的问题是包含我的任务的项目的目标是netcoreapp2.1框架...。我也将其更改为目标netstandard2.0,然后进入目标并更改了程序集文件路径是netstandard2.0而不是netcoreapp2.1。在那之后,我的自定义任务运行良好。