Build/Test Issues

NCrunch, Core 1.0.1 and BadImageFormatException

Started by Rasmus on 31,827 views

Thanks for your help. I've managed to reproduce and isolate this issue.

The problem is that the CLR seems to have an issue with loading assemblies dynamically from the user's nuget packages directory. Not all assemblies are affected by this, which has me somewhat confused. System.Net.Http seems to be the main victim of this problem.

This really does look like a problem with the CLR itself. I feel it should be reasonable that an assembly can be loaded from the Nuget packages using Assembly.LoadFrom, as there is otherwise no way to actually load it without first copying it somewhere else.

It may be possible to work around this problem by making a copy of System.Net.Http.dll and moving this into your solution, then just adding an assembly reference. In doing this, NCrunch will treat it as a standard non-nuget reference and will copy it to _ncrunchreferences, resolving it from there.

I may need to raise this with Microsoft. While I can easily patch NCrunch to treat Nuget-based references as normal assembly references (to be copied to _ncrunchreferences), this feels messy and doesn't seem to fit with the design of the CLR.

I've noticed another NCrunch user has reported a very similar seeming problem to MS - https://connect.microsoft.com/VisualStudio/feedback/details/3119877/could-not-load-file-or-assembly-system-security-cryptography-algorithms-primitives.
Ok, I think I have a solution to this issue. Try the build below and let me know how you go:

http://downloads.ncrunch.net/NCrunch_Console_3.7.0.1.msi
http://downloads.ncrunch.net/NCrunch_Console_3.7.0.1.zip
http://downloads.ncrunch.net/NCrunch_GridNodeServer_3.7.0.1.msi
http://downloads.ncrunch.net/NCrunch_GridNodeServer_3.7.0.1.zip
http://downloads.ncrunch.net/NCrunch_LicenseServer_3.7.0.1.zip
http://downloads.ncrunch.net/NCrunch_VS2008_3.7.0.1.msi
http://downloads.ncrunch.net/NCrunch_VS2010_3.7.0.1.msi
http://downloads.ncrunch.net/NCrunch_VS2010_3.7.0.1.zip
http://downloads.ncrunch.net/NCrunch_VS2012_3.7.0.1.msi
http://downloads.ncrunch.net/NCrunch_VS2012_3.7.0.1.zip
http://downloads.ncrunch.net/NCrunch_VS2013_3.7.0.1.msi
http://downloads.ncrunch.net/NCrunch_VS2013_3.7.0.1.zip
http://downloads.ncrunch.net/NCrunch_VS2015_3.7.0.1.msi
http://downloads.ncrunch.net/NCrunch_VS2015_3.7.0.1.zip
http://downloads.ncrunch.net/NCrunch_VS2017_3.7.0.1.msi
http://downloads.ncrunch.net/NCrunch_VS2017_3.7.0.1.zip
It builds the projects now, but ....

System.IO.FileNotFoundException: Die Datei oder Assembly "FluentAssertions, Version=4.19.2.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden.

This can't load the FluentAssertions Assembly while running the tests.
Ok, here's the next try!

http://downloads.ncrunch.net/NCrunch_Console_3.7.0.2.msi
http://downloads.ncrunch.net/NCrunch_Console_3.7.0.2.zip
http://downloads.ncrunch.net/NCrunch_GridNodeServer_3.7.0.2.msi
http://downloads.ncrunch.net/NCrunch_GridNodeServer_3.7.0.2.zip
http://downloads.ncrunch.net/NCrunch_LicenseServer_3.7.0.2.zip
http://downloads.ncrunch.net/NCrunch_VS2008_3.7.0.2.msi
http://downloads.ncrunch.net/NCrunch_VS2010_3.7.0.2.msi
http://downloads.ncrunch.net/NCrunch_VS2010_3.7.0.2.zip
http://downloads.ncrunch.net/NCrunch_VS2012_3.7.0.2.msi
http://downloads.ncrunch.net/NCrunch_VS2012_3.7.0.2.zip
http://downloads.ncrunch.net/NCrunch_VS2013_3.7.0.2.msi
http://downloads.ncrunch.net/NCrunch_VS2013_3.7.0.2.zip
http://downloads.ncrunch.net/NCrunch_VS2015_3.7.0.2.msi
http://downloads.ncrunch.net/NCrunch_VS2015_3.7.0.2.zip
http://downloads.ncrunch.net/NCrunch_VS2017_3.7.0.2.msi
http://downloads.ncrunch.net/NCrunch_VS2017_3.7.0.2.zip


These problems are all because of differences in the build system with the new CPS projects. Even when targeting the same versions of .NET as the old classic projects, the build steps in CPS have some fundamental differences. I hope the above build should address the last of these.
Hi,

This works for me, to a large extent. I still get errors in the selfhosted tests with Kestrel, due to Kestrel relying on a native dll (libuv.dll) that is not copied into place correctly. This is also something we have seen in our regular build/deployments and, if I remember correctly, ended up with us specifying runtimes directly when restoring nuget packages into get build (--runtime win7-x86). THB, these selfhosted integration tests are getting difficult enough for us to consider junking them completely.

Another sticking point was that I have automatic generation of swagger client code as a build step:

<Target Name="RunAutoRest" BeforeTargets="Build" Condition="'$(DesignTimeBuild)' != 'true'">
<Delete Files="@(AutoGeneratedFiles)" Condition="Exists('$(ProjectDir)swagger.json')" />
<Exec Command="$(AutoRestCom)" WorkingDirectory="$(ProjectDir)" Condition="Exists('$(ProjectDir)swagger.json')" />
</Target>

The pre- and postbuild steps for the project is set to false, so I had not expected this to run. But it does, tripping up ncrunch. Am I missing something?
@Rasmus don't use kestrel in Integration Tests, use the Microsoft.AspNetCore.TestHost https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing https://www.nuget.org/packages/Microsoft.AspNetCore.TestHost/
Don't need a real server, is all done inprocess.
Hi Rasmus,

The pre/post build step disabling in NCrunch (via settings) only controls the PreBuildEvent and PostBuildEvent properties; it won't suppress any custom build targets you've coded in yourself.

I recommend extending the condition on your build step to avoid running under NCrunch, Condition="'$(NCrunch)' != '1'"

The lubuv.dll problem is an interesting one. I'll be happy to take a closer look at this if you're able to submit any code that can reproduce this issue. It might be possible to work around it by adding some setup code to your test that specifically copies this file into your working directory.
Hi,

Looks like I am experiencing the same issue in this thread.

- I am using ASP.NET Core but targeting net461
- Visual Studio 2017 Community with NCrunch v3.6.

NCrunch is able to compile my projects, execute tests on regular projects, but not able to run the tests on my Web.Tests project, which is using Microsoft.AspNetCore.TestHost. I can run these test without issues in VSTest.

I am including the csproj for my test project as well as the NCrunch trace. I've tried both Sdk="Microsoft.NET.Sdk.Web" and just Sdk="Microsoft.NET.Sdk"

I could send you the full trace over email and a sample project to repro it if you need it.


<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.3.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.0.0" />
<PackageReference Include="FluentAssertions" Version="4.19.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.11" />
<PackageReference Include="MSTest.TestFramework" Version="1.1.11" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="NSubstitute" Version="2.0.2" />
<PackageReference Include="System.Net.Http" Version="4.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Construio.Web\Construio.Web.csproj" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>

System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\adalbus\.nuget\packages\system.net.http\4.1.1\ref\net46\System.Net.Http.dll' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
File name: 'file:///C:\Users\adalbus\.nuget\packages\system.net.http\4.1.1\ref\net46\System.Net.Http.dll' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at nCrunch.Common.CustomAssemblyResolver.ResolveAssembly(ParsedAssemblyName parsedAssemblyName)
at nCrunch.Common.CustomAssemblyResolver.(Object , ResolveEventArgs )
at System.AppDomain.OnAssemblyResolveEvent(RuntimeAssembly assembly, String assemblyFullName)


at nCrunch.TestExecution.Reflection.Clr.ClrAssembly.GetTypes()
at nCrunch.TestExecution.Reflection.ReflectedAssembly.GetFlattenedTypes()
at nCrunch.TestExecution.Reflection.ReflectedAssembly.()
at nCrunch.TestExecution.Reflection.ReflectedAssembly.GetTypeFromTestName(TestName testName)
at nCrunch.TestExecution.Reflection.ReflectedAssembly.GetParameterlessMethodFromTestName(TestName testName)
at nCrunch.Module.MSTest.Integration.MSTestWorkspace.findDeploymentItems(TestName[] testsToRunInWorkspace, ReflectedAssembly testAssembly)
at nCrunch.Module.MSTest.Integration.MSTestWorkspace..ctor(MSTestNames names, DirectoryPath solutionDirectory, DirectoryPath runtimeWorkspacePath, ReflectedAssembly testAssembly, TestName[] testsToRunInWorkspace)
at nCrunch.Module.MSTest.Integration.MSTestFrameworkRuntimeEnvironment.<>c__DisplayClass11_0.<RunTests>b__1()
at nCrunch.Common.PerformanceTracking.PerfTracker.TrackActivity(String name, Action activity)
at nCrunch.Common.PerformanceTracking.PerfTracker.TryTrackActivity(String name, Action activity)
at nCrunch.Module.MSTest.Integration.MSTestFrameworkRuntimeEnvironment.RunTests(TestTaskOutput output, TestExecutionMapSet testMapSet, TestExecutionParameters parameters, IIpcMessageProcessor messageProcessor)
at nCrunch.TestExecution.TestRunnerThread.()
Details:
]] is being processed on Core thread with subscriber: ErrorHandler.
[00:21:00.1618-Core-252] Event [ErrorEvent:[Error:
Context: (global)
Category: Internal
Message: System.Exception: System.BadImageFormatException: Could not load file or assembly 'System.Net.Http, Version=4.1.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
File name: 'System.Net.Http, Version=4.1.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\adalbus\.nuget\packages\system.net.http\4.1.1\ref\net46\System.Net.Http.dll' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
File name: 'file:///C:\Users\adalbus\.nuget\packages\system.net.http\4.1.1\ref\net46\System.Net.Http.dll' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at nCrunch.Common.CustomAssemblyResolver.ResolveAssembly(ParsedAssemblyName parsedAssemblyName)
at nCrunch.Common.CustomAssemblyResolver.(Object , ResolveEventArgs )
at System.AppDomain.OnAssemblyResolveEvent(RuntimeAssembly assembly, String assemblyFullName)[img]null[/img]
Take a look at this post http://forum.ncrunch.net/yaf_postsm10108_NCrunch--Core-1-0-1-and-BadImageFormatException.aspx#post10108 with Downloads of a PreVersion of 3.7.2 which fixed that problem or me.
Der-Albert.com wrote:Take a look at this post http://forum.ncrunch.net/yaf_postsm10108_NCrunch--Core-1-0-1-and-BadImageFormatException.aspx#post10108 with Downloads of a PreVersion of 3.7.2 which fixed that problem or me.


Somehow I missed this post. It did fixed my problem as well. Now I can run tests using Microsoft.AspNetCore.TestHost!

Thank you!
I think this issue is back.

Tested on 2 machines with same software configurations:

* Windows 10 64 bit
* Visual Studio Professional 2017 15.3.2
* NCrunch 3.11.0.4

Steps to reproduce:

1. Create a new xUnit test project;
2. Change content of the csproj file to:


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <RuntimeIdentifier>win-x86</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
    <PackageReference Include="xunit" Version="2.3.0-beta3-build3705" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta3-build3705" />
  </ItemGroup>

</Project>


3. Add a test file:
    public class Tests
    {
        public HttpClient HttpClient { get; set; }

        [Fact]
        public void Test()
        {
            Assert.True(true);
        }
    }


NCrunch throws the following error (Resharper doesn't have this issue):

An error occurred while analysing this project after it was built: System.BadImageFormatException: Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
File name: 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.BadImageFormatException: Could not load file or assembly 'file:///C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\ref\System.Net.Http.dll' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
File name: 'file:///C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\ref\System.Net.Http.dll' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at nCrunch.Common.CustomAssemblyResolver.ResolveAssembly(ParsedAssemblyName parsedAssemblyName)
at nCrunch.Common.CustomAssemblyResolver.(Object , ResolveEventArgs )
at System.AppDomain.OnAssemblyResolveEvent(RuntimeAssembly assembly, String assemblyFullName)

Edited

Thanks, this looks like a whole new problem. I'll let you know when I've found a solution.
I've prepared a new build including what I hope is a fix for this issue. Would you be interested in giving it a try?

http://downloads.ncrunch.net/NCrunch_Console_3.11.0.6.msi
http://downloads.ncrunch.net/NCrunch_Console_3.11.0.6.zip
http://downloads.ncrunch.net/NCrunch_GridNodeServer_3.11.0.6.msi
http://downloads.ncrunch.net/NCrunch_GridNodeServer_3.11.0.6.zip
http://downloads.ncrunch.net/NCrunch_LicenseServer_3.11.0.6.zip
http://downloads.ncrunch.net/NCrunch_VS2008_3.11.0.6.msi
http://downloads.ncrunch.net/NCrunch_VS2010_3.11.0.6.msi
http://downloads.ncrunch.net/NCrunch_VS2010_3.11.0.6.zip
http://downloads.ncrunch.net/NCrunch_VS2012_3.11.0.6.msi
http://downloads.ncrunch.net/NCrunch_VS2012_3.11.0.6.zip
http://downloads.ncrunch.net/NCrunch_VS2013_3.11.0.6.msi
http://downloads.ncrunch.net/NCrunch_VS2013_3.11.0.6.zip
http://downloads.ncrunch.net/NCrunch_VS2015_3.11.0.6.msi
http://downloads.ncrunch.net/NCrunch_VS2015_3.11.0.6.zip
http://downloads.ncrunch.net/NCrunch_VS2017_3.11.0.6.msi
http://downloads.ncrunch.net/NCrunch_VS2017_3.11.0.6.zip
I can confirm this issue has been fixed in 3.11.0.6, thanks!

Post a reply

Log in to reply.