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

Notification

Icon
Error

MSpec test using RazorEngine fails on some machines but does not fail in Resharper or MSpec runner
ljohnston
#1 Posted : Tuesday, September 29, 2015 5:54:19 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 1/17/2014(UTC)
Posts: 31

Thanks: 17 times
Was thanked: 5 time(s) in 5 post(s)
I'm sorry I don't have an MCVE or more information on the cause of this, but it's only happening for one test in one large project and every time I try to narrow it down I can no longer reproduce the issue. I'm happy to gather more information for you if I can if you can tell me what you need. Unfortunately I can't provide the solution/code in question.

We use RazorEngine (v3.7.2 from NuGet) to compile some cshtml templates in a .NET 4.6 project. We do this by calling Razor.Engine.RunCompile and supplying the template as a string and a strongly typed model object. We have an MSpec (v0.9.3) test that compiles the template along with some sample data and does some basic validation of the resulting HTML.

I recently added a call to the .Any() LINQ extension method to the template, and suddenly the test starting failing with the below exception, but only when run in the NCrunch runner. The test never fails when run in the Resharper runner or on our build server (which uses the TeamCity MSpec runner). Also it doesn't even fail reliably in NCrunch. I've had it fail on my machine, changed the code to remove the .Any() call, and then re-added the .Any() call and had the test succeed again. If I switch the call to .Any() to .Length != 0, the test always passes. It's failing right now on my machine, but it's passing just fine on one of my colleagues machines with the same template. Yesterday it was failing on his machine but passing on mine. So obviously there's some caching or a race condition or something going on somewhere.

We don't have any using statements in the template other than one for two of our own projects, but I've tried adding using statements with no luck and as I've said the test runs fine in the non-NCrunch runners. I've also tried enabling Copy referenced assemblies to workspace with no luck.

We don't explicitly include the System.Runtime in the DLLs supplied to RazorEngine (via code or in our app.config in either our main or test project), but I've tried to do this via system.web.compilation.assemblies.add in the app.config with no luck.

Could you please take a look at this and see if you can figure out what the issue is?

Here's the failure:
Quote:

RazorEngine.Templating.TemplateCompilationException: Errors while compiling a Template.
Please try the following to solve the situation:
* If the problem is about missing/invalid references or multiple defines either try to load
the missing references manually (in the compiling appdomain!) or
Specify your references manually by providing your own IReferenceResolver implementation.
See https://antaris.github.i.../ReferenceResolver.html for details.
Currently all references have to be available as files!
* If you get 'class' does not contain a definition for 'member':
try another modelType (for example 'null' to make the model dynamic).
NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
Or try to use static instead of anonymous/dynamic types.
More details about the error:
- error: (574, 62) The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Collections.Generic.IList`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Collections.Generic.ICollection`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.IEquatable`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Collections.Generic.IReadOnlyList`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Collections.Generic.IReadOnlyCollection`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Collections.Generic.IEnumerable`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Collections.IEnumerable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Collections.IList' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Collections.ICollection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Collections.IStructuralComparable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Collections.IStructuralEquatable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
- error: (574, 62) The type 'System.Func`2<T0,T1>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Temporary files of the compilation can be found in (please delete the folder): C:\Users\ljohnston\AppData\Local\Temp\RazorEngine_ngbf2fpl.bov
The template we tried to compile is:

[...template snipped...]

List of loaded Assemblies:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll
Loaded Assembly: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Remco Software\NCrunch for Visual Studio 2015\nCrunch.TaskRunner.dll
Loaded Assembly: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Remco Software\NCrunch for Visual Studio 2015\nCrunch.Common.dll
Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll
Loaded Assembly: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Remco Software\NCrunch for Visual Studio 2015\nCrunch.TestExecution.dll
Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
Loaded Assembly: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Remco Software\NCrunch for Visual Studio 2015\nCrunch.Reflection.dll
Loaded Assembly: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Remco Software\NCrunch for Visual Studio 2015\nCrunch.Module.MSpec.dll
Loaded Assembly: D:\Development\NCrunchWorkspace\14020\20\Companion\Tests\bin\Debug\Companion.Tests.dll
Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll
Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll
Loaded Assembly: D:\Development\NCrunchWorkspace\14020\20\_ncrunchreferences\Machine.Specifications.dll
Loaded Assembly: C:\Users\ljohnston\AppData\Local\Temp\NCrunchDynamicProxy\NCrunch.DynamicProxy_28bd7f517a434bed8220da95814c6cc1_14020.dll
Loaded Assembly: D:\Development\NCrunchWorkspace\14020\25\Companion\bin\Debug\Companion.exe
Loaded Assembly: D:\Development\NCrunchWorkspace\14020\20\_ncrunchreferences\NodaTime.dll
Loaded Assembly: D:\Development\NCrunchWorkspace\14020\20\_ncrunchreferences\Sepialine.Commons.dll
Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Collections.Immutable\v4.0_1.1.36.0__b03f5f7f11d50a3a\System.Collections.Immutable.dll
Loaded Assembly: D:\Development\NCrunchWorkspace\14020\1\Model\bin\Debug\Model.dll
Loaded Assembly: D:\Development\NCrunchWorkspace\14020\7\Presentation\bin\Debug\Presentation.dll
Loaded Assembly: D:\Development\NCrunchWorkspace\14020\20\_ncrunchreferences\CsvHelper.dll
Loaded Assembly: C:\WINDOWS\assembly\GAC_MSIL\nCrunch.TestRuntime\2.16.0.13__01d101bf6f3e0aea\nCrunch.TestRuntime.dll
Loaded Assembly: D:\Development\NCrunchWorkspace\14020\20\_ncrunchreferences\RazorEngine.dll
Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll
Loaded Assembly: D:\Development\NCrunchWorkspace\14020\20\_ncrunchreferences\System.Web.Razor.dll
Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll
at RazorEngine.Compilation.DirectCompilerServiceBase.CompileTypeImpl(TypeContext context)
at RazorEngine.Compilation.DirectCompilerServiceBase.CompileType_Windows(TypeContext context)
at RazorEngine.Compilation.DirectCompilerServiceBase.CompileType(TypeContext context)
at RazorEngine.Templating.RazorEngineCore.CreateTemplateType(ITemplateSource razorTemplate, Type modelType)
at RazorEngine.Templating.RazorEngineCore.Compile(ITemplateKey key, Type modelType)
at RazorEngine.Templating.RazorEngineService.CompileAndCacheInternal(ITemplateKey key, Type modelType)
at RazorEngine.Templating.RazorEngineService.GetCompiledTemplate(ITemplateKey key, Type modelType, Boolean compileOnCacheMiss)
at RazorEngine.Templating.RazorEngineService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.DynamicWrapperService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass16_0.<RunCompile>b__0(TextWriter writer)
at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
at RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(IRazorEngineService service, String name, Type modelType, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(IRazorEngineService service, String templateSource, String name, Type modelType, Object model, DynamicViewBag viewBag)
at [...location in our code snipped...]



Remco
#2 Posted : Tuesday, September 29, 2015 9:33:23 PM(UTC)
Rank: NCrunch Developer

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

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi, thanks for sharing this issue.

Does enabling the NCrunch project-level 'Pre-load all assembly references into test environment' setting on the relevant test project resolve this issue?
1 user thanked Remco for this useful post.
ljohnston on 9/29/2015(UTC)
ljohnston
#3 Posted : Tuesday, September 29, 2015 10:26:26 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 1/17/2014(UTC)
Posts: 31

Thanks: 17 times
Was thanked: 5 time(s) in 5 post(s)
Yup, that fixed it. Is keeping this setting enabled the best way to fix the issue long term or are there other options we should consider?

Thanks for the help.
Remco
#4 Posted : Tuesday, September 29, 2015 11:19:04 PM(UTC)
Rank: NCrunch Developer

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

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Excellent. I think that leaving this setting enabled for you would be best solution to this problem. I've been doing some digging around here to try and solve a very similar seeming issue with System.Runtime, and concluded that the behaviour under the hood here is likely to be quite complex.

Basically there are multiple versions of System.Runtime. The standard distributed one (v4.0.0.0) is installed in the GAC, and ships with the .NET Framework.

However, there is another version (4.0.10.0) which ships with the Windows 8 SDK. The resolution of this assembly seems to be more complex somehow. It is used by portable assemblies and is often indirectly referenced from assemblies that make use of these.

I'm not sure what kind of logic the RazorEngine compiler applies to its assembly resolution, but somehow there is a consistency issue here. By telling NCrunch to load the assembly up-front, you by-pass the RazorEngine assembly resolution logic and simply feed it what it needs. A side-effect of this is that your test environment will take slightly longer to initialise, as NCrunch needs to do more work upfront (by loading the referenced assemblies) than normal. With the setting disabled, assemblies will instead be loaded on-demand. In most cases the difference is not significant.
ljohnston
#5 Posted : Tuesday, September 29, 2015 11:24:48 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 1/17/2014(UTC)
Posts: 31

Thanks: 17 times
Was thanked: 5 time(s) in 5 post(s)
Thanks. We'll stick with this solution then.
Users browsing this topic
Guest
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.078 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download