Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

2 Pages<12
NCrunch, Core 1.0.1 and BadImageFormatException
Remco
#21 Posted : Wednesday, March 22, 2017 11:53:49 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,122

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
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.
Remco
#22 Posted : Thursday, March 23, 2017 2:49:51 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,122

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
Der-Albert.com
#23 Posted : Thursday, March 23, 2017 7:11:28 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 5/17/2011(UTC)
Posts: 208

Thanks: 11 times
Was thanked: 53 time(s) in 50 post(s)
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.
Remco
#24 Posted : Friday, March 24, 2017 5:52:51 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,122

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
Der-Albert.com
#25 Posted : Friday, March 24, 2017 7:56:52 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 5/17/2011(UTC)
Posts: 208

Thanks: 11 times
Was thanked: 53 time(s) in 50 post(s)
All Tests are green now. Thanks.
Rasmus
#26 Posted : Friday, March 24, 2017 7:57:44 AM(UTC)
Rank: Member

Groups: Registered
Joined: 3/17/2017(UTC)
Posts: 13
Location: Denmark

Was thanked: 2 time(s) in 2 post(s)
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?
Der-Albert.com
#27 Posted : Friday, March 24, 2017 8:10:27 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 5/17/2011(UTC)
Posts: 208

Thanks: 11 times
Was thanked: 53 time(s) in 50 post(s)
@Rasmus don't use kestrel in Integration Tests, use the Microsoft.AspNetCore.TestHost https://docs.microsoft.c...ing/integration-testing https://www.nuget.org/packages/Microsoft.AspNetCore.TestHost/
Don't need a real server, is all done inprocess.
Remco
#28 Posted : Friday, March 24, 2017 8:31:32 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,122

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
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.
adalbus
#29 Posted : Wednesday, March 29, 2017 7:46:02 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/29/2017(UTC)
Posts: 2
Location: United States of America

Thanks: 1 times
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]
Der-Albert.com
#30 Posted : Wednesday, March 29, 2017 7:50:12 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 5/17/2011(UTC)
Posts: 208

Thanks: 11 times
Was thanked: 53 time(s) in 50 post(s)
Take a look at this post http://forum.ncrunch.net...xception.aspx#post10108 with Downloads of a PreVersion of 3.7.2 which fixed that problem or me.
1 user thanked Der-Albert.com for this useful post.
adalbus on 3/30/2017(UTC)
adalbus
#31 Posted : Thursday, March 30, 2017 6:11:21 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/29/2017(UTC)
Posts: 2
Location: United States of America

Thanks: 1 times
Der-Albert.com;10148 wrote:
Take a look at this post http://forum.ncrunch.net...xception.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!
yanglee
#32 Posted : Wednesday, August 23, 2017 7:25:54 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/25/2012(UTC)
Posts: 89
Location: China

Thanks: 23 times
Was thanked: 19 time(s) in 18 post(s)
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:

Code:

<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:
Code:
    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)
Remco
#33 Posted : Thursday, August 24, 2017 12:09:18 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,122

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
Thanks, this looks like a whole new problem. I'll let you know when I've found a solution.
Remco
#34 Posted : Sunday, August 27, 2017 2:58:36 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,122

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
1 user thanked Remco for this useful post.
yanglee on 8/27/2017(UTC)
yanglee
#35 Posted : Sunday, August 27, 2017 3:06:24 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/25/2012(UTC)
Posts: 89
Location: China

Thanks: 23 times
Was thanked: 19 time(s) in 18 post(s)
I can confirm this issue has been fixed in 3.11.0.6, thanks!
1 user thanked yanglee for this useful post.
Remco on 8/27/2017(UTC)
Users browsing this topic
Guest
2 Pages<12
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.138 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download