Razorlight: NullReferenceException in RazorLight.Compilation.RoslynCompilationService.IsAssemblyDebugBuild

Created on 7 Dec 2017  路  10Comments  路  Source: toddams/RazorLight

I'm using RazorLight v2.0.0-alpha3 and I'm getting a NRE when I try to render a razor template from my unit test project.

{System.NullReferenceException: Object reference not set to an instance of an object.
   at RazorLight.Compilation.RoslynCompilationService.IsAssemblyDebugBuild(Assembly assembly)
   at RazorLight.Compilation.RoslynCompilationService..ctor(IMetadataReferenceManager referenceManager)
   at RazorLight.EngineFactory.Create(RazorLightProject project, RazorLightOptions options)
   at RazorLight.EngineFactory.ForEmbeddedResources(Type rootType)

The constructor of the RoslynCompilationService is executing this line of code:
IsAssemblyDebugBuild(OperatingAssembly);

The OperatingAssembly property returns the result from Assembly.GetEntryAssembly(), but in my case this returns null. Because of this the IsAssemblyDebugBuild method throws a NRE:
return assembly.GetCustomAttributes(false) ==> assembly == null

bug

Most helpful comment

@toddams - we are getting this same error with .net 4.6.2. Your sandbox uses Dependency Injection 2.0.0.0 but the version 2.0.3 or higher have the error which you seem to have fixed in #159 (669cb741998b6248d7ccfa3f9da2d25acdeaa2f8)

Our project is awaiting your Beta2 release. Can you please provide an ETA?

All 10 comments

What test library do you use?

xunit version 2.3.1

Tests can be run under a different AppDomain, that's why the exception is thrown.
You can also test this with a .NET Framework application which runs RazorLight inside a different AppDomain.

@toddams any news on this?

@stevenlauwers22 you can try adding a xunit.runner.json file to your xunit test project with the following contents and set "Copy to output directory" to "Copy if newer".

{
  "appDomain": "denied"
}

Adding the xunit.runner.json file fixes the NRE I had ... however, now I get a new NRE.

at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(Assembly assembly) at RazorLight.Compilation.RoslynCompilationService.EnsureOptions() at RazorLight.Compilation.RoslynCompilationService.get_ParseOptions() at RazorLight.Compilation.RoslynCompilationService.CreateCompilation(String compilationContent, String assemblyName) at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(GeneratedRazorTemplate razorTemplate) at RazorLight.TemplateFactoryProvider.CreateFactory(GeneratedRazorTemplate razorTemplate) at RazorLight.TemplateFactoryProvider.<CreateFactoryAsync>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at RazorLight.RazorLightEngine.<CompileTemplateAsync>d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at RazorLight.RazorLightEngine.<CompileRenderAsync>d__9.MoveNext()

The following line in the DefaultMetadataReferenceManager.Resolve method returns null:
var dependencyContext = DependencyContext.Load(assembly);

The NRE occurs on the following line (since dependencyContext == null):
var references = dependencyContext.CompileLibraries.SelectMany(library => library.ResolveReferencePaths());

CSharp DefaultMetadataReferenceManager.Resolve()

This error is fixed in #159, so considering this issue solved

@toddams - we are getting this same error with .net 4.6.2. Your sandbox uses Dependency Injection 2.0.0.0 but the version 2.0.3 or higher have the error which you seem to have fixed in #159 (669cb741998b6248d7ccfa3f9da2d25acdeaa2f8)

Our project is awaiting your Beta2 release. Can you please provide an ETA?

Any news on this?

Was this page helpful?
0 / 5 - 0 ratings