adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
576 stars 85 forks source link

assembly.GetType returning null, and "Could not load file or assembly RazorEngineCore" #27

Closed Merlin04 closed 4 years ago

Merlin04 commented 4 years ago

I'm working on a PR that will add support for compiling multiple templates in one assembly, but I can't get any of the unit tests that involve running a template to run successfully. I get an exception like this:

Test method RazorEngineCore.Tests.TestCompileAndRun.TestCompileAndRun_HtmlLiteral threw exception: 
System.ArgumentNullException: Value cannot be null. (Parameter 'type')
    at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type)
   at RazorEngineCore.RazorEngineCompiledTemplate.RunAsync(Object model)

It seems like the issue is with line 18 of RazorEngineCompiledTemplate.cs:

this.templateType = assembly.GetType("TemplateNamespace.Template");

The call to GetType doesn't throw an exception, but it returns null. If I use the debugger to look at the DefinedTypes property of assembly, I get this exception:

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'RazorEngineCore, Version=2020.6.1.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

Any idea why I'm getting this error?

adoconnection commented 4 years ago

Can you push your code to github so I can examine it? https://github.com/Merlin04/RazorEngineCore

Merlin04 commented 4 years ago

This is happening even when I make a new clone of the repository, so it isn't related to my changes.

adoconnection commented 4 years ago

It’s hard to tell without seeing a code :)

wdcossey commented 4 years ago

If you want to precompile templates during the build process (not runtime) and embed then as resources have a look at my repository.

https://www.nuget.org/packages/RazorEngineCore.Precompiler/

https://github.com/wdcossey/RazorEngineCore.Extensions

There's a project called RazorEngineCore.Precompiler that will do this, you can use it as a starting point.

Merlin04 commented 4 years ago

The code I'm trying to run the tests for is this repository, not my modifications.

I run:

git clone https://github.com/adoconnection/RazorEngineCore

Then open the solution in Rider, and try running the tests, but it fails.

Merlin04 commented 4 years ago

The tests run successfully with dotnet test, so this must be an issue with Rider.

Merlin04 commented 4 years ago

Just found an update that fixes the Rider issue (EAP9)