3F / DllExport

.NET DllExport with .NET Core support (aka 3F/DllExport aka DllExport.bat)
MIT License
937 stars 131 forks source link

Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context #141

Closed abnud1 closed 4 years ago

abnud1 commented 4 years ago

How to reproduce:

  1. Launch DLLExport.bat in the solution directory.
  2. Choose a .Net standard 2.1 project.
  3. choose platform x86.
  4. Click Apply.
  5. use [DllExport] attribute.
  6. build the project.
  7. this error message appears:

net.r_eg.DllExport.targets(76, 5): [MSB4062] The "RGiesecke.DllExport.MSBuild.DllExportAppDomainIsolatedTask" task could not be loaded from the assembly C:\Users\Administrator\Desktop\Workflow\Devices\packages\DllExport.1.7.0\tools\RGiesecke.DllExport.MSBuild.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058) Confirm that the 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.

When jumping to source it goes to here:

<DllExportAppDomainIsolatedTask
          Platform="$(DllExportPlatform)"
          CpuType="$(DllExportCpuType)"
          DllExportAttributeFullName="$(DllExportAttributeFullName)"
          EmitDebugSymbols="$(DllExportEmitDebugSymbols)"
          LeaveIntermediateFiles="$(DllExportLeaveIntermediateFiles)"
          Timeout="$(DllExportTimeout)"
          KeyContainer="$(DllExportKeyContainer)"
          KeyFile="$(DllExportKeyFile)"
          ProjectDirectory="$(DllExportProjectDirectory)"
          InputFileName="$(DllExportInputFileName)"
          FrameworkPath="$(DllExportFrameworkPath)"
          VsDevCmd="$(DllExportVsDevCmd)"
          VcVarsAll="$(DllExportVcVarsAll)"
          LibToolPath="$(DllExportLibToolPath)"
          LibToolDllPath="$(DllExportLibToolDllPath)"
          TargetFrameworkVersion="$(DllExportTargetFrameworkVersion)"
          SdkPath="$(DllExportSdkPath)"
          SkipOnAnyCpu="$(DllExportSkipOnAnyCpu)"
          OrdinalsBase="$(DllExportOrdinalsBase)"
          GenExpLib="$(DllExportGenExpLib)"
          OurILAsmPath="$(DllExportOurILAsmPath)"
          MetaLib="$(DllExportMetaLibFullPath)"
          PeCheckRaw="$(DllExportPeCheck)"
          PatchesRaw="$(DllExportPatches)"
          SysObjRebase="$(DllExportSysObjRebase)"
         />

at file net.r_eg.DllExport.targets

-version: v1.7.0.60761+0a002a7

-build-info:

S_NUM: 1.7.0 S_REV: 60761 S_NUM_REV: 1.7.0.60761 S_REL: bSha1: 0a002a7 bName: master bRevc: 201 MetaCor: netstandard1.1 MetaLib: v2.0 Wizard: v4.0 Configuration: PublicRelease Platform: Any CPU cfgname: Release revDeltaBase: 2016/10/12 revDeltaMin: 1000 revDeltaMax: 65534

The full command to Manager: ./DLLExport.bat

Project type: .Net standard project

Environment ( VS, MSBuild, ...): .Net core 3.1, IDE IS Rider 2019.3, not Visual Studio, MSBuild version is 16. log: DllExport.bat output hMSBuild.bat -debug -only-path output vswhere -requires Microsoft.Component.MSBuild -products * -latest output

3F commented 4 years ago

Thanks for the detailed information!

As I understand, now you have a completed installation:

isComplete: 1

However, could you please describe steps for how it was installed (net core 3.1 + MSBuild 16) ? I'll try to reproduce your environment on VM because something still is incompatible or incomplete.

Also, can you try to remove all or some of the problematic assemblies from packages\DllExport.1.7.0\tools\

Such:

Then try again.

All this, on the contrary, was added due to other problems in past.

abnud1 commented 4 years ago

@3F I tried removing the whole DllExport.1.7.0 folder from packages folder and still the same problem.

I installed visual studio using the usual installer, I'll export its configuration, you'll find it inside this zip file, then use vs installer import configuration feature.

3F commented 4 years ago

I tried removing the whole DllExport.1.7.0 folder

No, do not do this because it will be restored to initial state again! Try as I said in order as it was mentioned earlier. For the case when Clr will try to load assemblies from other available places for your environment (GAC etc). At least this might help to better understand the problem.

I'll export its configuration, you'll find it inside this zip file, then use vs installer import configuration feature.

Thanks, I'll try to reproduce this for clean VM as possible for my time.

3F commented 4 years ago

Just checked netstandard2.1 + [DllExport] public static int m1() => -1;

Ok for 16.5.1 on clean VM (Windows 10 image). Unfortunately I have no access to your 16.4.6 through latest official installer.

But here is another interest to your environment. I noticed that you have only:

"Microsoft.NetCore.Component.SDK",

Actually it cannot be installed separately from related a .NET Core Runtime component that obviously strange. Even import feature of the .vsconfig will forcibly activate two. So how did you get this?

More like you still have some incomplete or incorrectly installed instances such before

Please try to fix your installed products and make sure you have installed both following components:

"Microsoft.NetCore.Component.Runtime.3.1", "Microsoft.NetCore.Component.SDK",

abnud1 commented 4 years ago

@3F I tried removing problematic assemblies as you suggested, it now reports system cannot find the file specified error.

Please try to fix your installed products and make sure you have installed both following components:

"Microsoft.NetCore.Component.Runtime.3.1", "Microsoft.NetCore.Component.SDK",

I now have them both installed and tried to remove the DllExport folder from packages folder as well as removing problematic assemblies like you suggested, still Build fails with the same errors.

3F commented 4 years ago

The error actually depends on your environment and I don't know how to reproduce your case because:

As I know MS still provides compatible interface for tasks even for modern ways. This is relevant for Microsoft.Build.Framework.ITask (Microsoft.Build.Framework.dll) and Microsoft.Build.Utilities.AppDomainIsolatedTask (Microsoft.Build.Utilities.v4.0.dll). Therefore, your problem cannot be with incompatible or obsolete assemblies. I think.

On the contrary, it can require something more that you don't have. But here is actual problem, as I said, all required components should be installed as for my clean VM. I strongly recommend to update to 16.5.1 because it can be problem with components and/or incorrect vs-installer. By the way, what OS?

Please attach information using the following commands:

msbuild [your.sln] /t:Rebuild /m:4 /v:diag > build.log
tree /F C:\Windows\Microsoft.NET > netenv.txt

You also can try (I don't think that problem with this but just to be sure):

In tools\net.r_eg.DllExport.targets replace the 3rd line <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> on:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Then try again. If not, try this:

<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Most important: Reload project/solution for your IDE after each changes; or build with msbuild.

abnud1 commented 4 years ago

By the way, what OS? Windows 10 64 bit 1909 build

tree /F C:\Windows\Microsoft.NET > netenv.txt Output: netenv.txt

msbuild [your.sln] /t:Rebuild /m:4 /v:diag > build.log Output: build.log

You also can try (I don't think that problem with this but just to be sure):

In tools\net.r_eg.DllExport.targets replace the 3rd line on:

Then try again. If not, try this:

I tried both, still the same problem.

3F commented 4 years ago

@abnud1 Thanks for the new details! I'll try to inspect this of course, but to be sure, Is this the only project where this error appears? What about new or some others that are more trivial than 285K+ lines (22.4 MB) ?

3F commented 4 years ago

C:\Program Files\dotnet\sdk\3.1.201\MSBuild.dll -distributedlogger:Microsoft.DotNet.Tools.MSBuild.MSBuildLogger,C:\Program Files\dotnet\sdk\3.1.201\dotnet.dll*Microsoft.DotNet.Tools.MSBuild.MSBuildForwardingLogger,C:\Program Files\dotnet\sdk\3.1.201\dotnet.dll -maxcpucount /m:4 -p:Configuration=Release -verbosity:m /v:diag /t:Rebuild Devices.sln

Please try as I already said above. You can use hmsbuild or locate it manually:

hmsbuild Devices.sln /t:Rebuild /m:4 /v:diag > build.log

or for sure

hmsbuild -vsw-as "-requires Microsoft.NetCore.Component.SDK -products * -latest" Devices.sln /t:Rebuild /m:4 /v:diag > build.log

abnud1 commented 4 years ago

hmsbuild Devices.sln /t:Rebuild /m:4 /v:diag > build.log This command actually built project successfully !!!

I now know the problem, dotnet build command doesn't work, specifically, the MSBuild that comes with dotnet core doesn't work, what works is the MSBuild.exe that comes with visual studio.

I'm not sure if this issue is resolved though, dotnet build should work as it's a legal way to build dotnet core projects.

3F commented 4 years ago

You must use msbuild tools today instead of dotnet cli.

it's a legal way to build dotnet core projects.

Same for msbuild tools! Even officially they recommends to use exactly this instead of dotnet build for some related problems with tasks etc. Officially DllExport today provides support only for msbuild tools, visual studio, and related. I'm not using mentioned IDE IS Rider but I'm sure you can configure build through this easily.

I close this issue. You can also open a new one for add support or discuss an alternative to dotnet build.