question

babakfakhriloo avatar image
0 Votes"
babakfakhriloo asked babakfakhriloo edited

Custom Buld Task when used in csproject, throws 'Caould not find System.Runtime'

I created a library project, using .netcore 5.0 which has a Task :

 using Microsoft.Build.Utitilites; 
    
 public class SimpleTask : Task
 {
    
      public override bool Execute()
      {
             var processInfo = new ProcessStartInfo();
             processInfo.FileName = "cmd";

             Process.Start(processInfo );
      }
    
 }

I add the Task config into a another project (Project B) csproject config :

 <UsingTask ... />
    
 <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
      <SimpleTask />
 </ItemGroup>


When I build (Project B), it throws error in Visual Studio :

MSB4018 "The "SimpleTask" task failed unexpectedly"

FileNotFoundException : Could not load file or assembly 'System.Runtime Version = 5.0.0.0'



The source code : https://github.com/persiandeveloper/custom-build-task

dotnet-csharpvs-msbuild
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @babakfakhriloo
May I know whether your issue has been solved or not? If not, please share it in here.

0 Votes 0 ·
lextm avatar image
0 Votes"
lextm answered babakfakhriloo edited

You are doing the wrong thing as .NET 5 based custom task cannot be used inside Visual Studio (VS loads an MSBuild version targeting .NET Framework).

It has been well discussed in this GitHub thread a few months ago.

The solution is also simple. Change your task project target framework to .NET Standard 2.0. Make sure you restart all VS instances so the old task assembly is cleaned up from the memory.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for sharing the answer.

I know it has been while since you replied, but I noticed that if I close Visual studio, open it and try to build, I get the following error.

But it builds if I use "dotnet build" command.

Error MSB4018 The "SimpleTask " task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its ....

=== Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/MSBuild/Current/Bin/
LOG: Initial PrivatePath = NULL
Calling assembly : System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a



0 Votes 0 ·
PengGe-MSFT avatar image
0 Votes"
PengGe-MSFT answered babakfakhriloo commented

Hi, @babakfakhriloo

Welcome to Microsoft Q&A!

Your project references the System.Runtime package. Has it been installed?
You can install this nuget package into your project and then try to build.
If you have already installed this package, please check your app.proj and package's properties to see if the version of System.Runtime match.

Sincerely,
Peng


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread


· 16
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I already have System.RunTime referenced in both projects, however the latest version in nuget is 4.3.1 (https://www.nuget.org/packages/System.Runtime/), but the error complains about missing ''System.Runtime Version = 5.0.0.0''

0 Votes 0 ·

Hi, @babakfakhriloo
Can you check the reference tag of System.Runtime in the properties of your project?
like this:

 <ItemGroup>
      <PackageReference Include="System.Runtime" Version="4.3.1" />
  </ItemGroup>



0 Votes 0 ·

IIt is already there in both projects.

0 Votes 0 ·
Show more comments

Hi, @babakfakhriloo
I opened the project you uploaded in Visual Studio, but did not encounter the exception you mentioned. Can you provide more information? For example, a screenshot of exception.
You can try to re-create a project, import the relevant package and then try to build the project.
About MSBuild, you can refer to this and this documents.


0 Votes 0 ·

Hi PengGe

Here is a screenshot. 111793-snyk-vs-error.png


0 Votes 0 ·
snyk-vs-error.png (21.2 KiB)
Show more comments