Open vicinting opened 6 years ago
Good luck.... We are moving to azure function too... Honestly I think I will change engine... It's about a week I am trying to make it work and there is no support. Here people answer rarely, you have been lucky because I am here to doing a new question... So, to solve your problem you must write a custom ReferenceResolver... This WAS mine:
internal class ExternalAssemblyReferenceResolver : IReferenceResolver
{
private string[] _assembliesToLoad;
public ExternalAssemblyReferenceResolver(params string[] assembliesToLoad)
{
_assembliesToLoad = assembliesToLoad;
}
public IEnumerable<CompilerReference> GetReferences(TypeContext context, IEnumerable<CompilerReference> includeAssemblies = null)
{
IEnumerable<string> loadedAssemblies = CompilerServicesUtility
.GetLoadedAssemblies()
.Where(a => !a.IsDynamic && !a.FullName.Contains("Version=0.0.0.0") && File.Exists(a.Location) && !a.Location.Contains("CompiledRazorTemplates.Dynamic"))
.GroupBy(a => a.GetName().Name).Select(grp => grp.First(y => y.GetName().Version == grp.Max(x => x.GetName().Version))) // only select distinct assemblies based on FullName to avoid loading duplicate assemblies
.Select(a => CompilerReference.From(a))
.Concat(includeAssemblies ?? Enumerable.Empty<CompilerReference>())
.Select(r => r.GetFile())
.ToArray();
yield return CompilerReference.From(FindLoaded(loadedAssemblies, "mscorlib.dll"));
yield return CompilerReference.From(FindLoaded(loadedAssemblies, "netstandard.dll"));
yield return CompilerReference.From(FindLoaded(loadedAssemblies, "System.dll"));
yield return CompilerReference.From(FindLoaded(loadedAssemblies, "System.Core.dll"));
yield return CompilerReference.From(FindLoaded(loadedAssemblies, "RazorEngine.dll"));
yield return CompilerReference.From(typeof(ExternalAssemblyReferenceResolver).Assembly); // Assembly
foreach (var assembly in this._assembliesToLoad)
{
yield return CompilerReference.From(FindLoaded(loadedAssemblies, assembly));
}
}
private string FindLoaded(IEnumerable<string> refs, string find)
{
return refs.First(r => r.EndsWith(System.IO.Path.DirectorySeparatorChar + find));
}
}
What I understood is that you must load all references that are necessary to compile the cshtml... so you must add system.dll
, mscorlib.dll
... and so on (perhaps you do not need netstandard.dll
, it depends on the type of project where your templates are saved)... than you must add also your dll where your models are saved... you cannot see it in my code because I passed the name of my dll inside the variable _assembliesToLoad
...
I think you can use my code and pass in input your dll name...
Finally, in your configuration you have to use your new ReferenceResolver:
TemplateServiceConfiguration configuration = new TemplateServiceConfiguration();
configuration.ReferenceResolver = new ExternalAssemblyReferenceResolver(assembliesReferencePaths);
It should start to work but you should receive an advice that it is better that you run razorEngine in a new app domain, otherwise temp file are not deleted... infact if you check temp file will not delete.... It is about 3 days I am trying to resolve that problem.... but nothing...
If it still does not work let me know... I do not remember anymore all the passes I did to let it works (I am not sure you have to change also the template manager)... I have done a lot of reverse engineering to solve all my problems.. and I have not finished yet...
Hope this help
PS.: here one question I have done here (sadly the answer is mine too :( ) I dont know if it can help you in future: https://github.com/Antaris/RazorEngine/issues/526
UPDATE: We are using RazorEngine v3.8.2
I want to start off by saying I only encounter this issue on our test environment (deployed in azure app service) but not locally. Possibly due to debug configuration?
I am trying to compile e-mail templates. Originally, this was done in a worker role (cloud service) without issues. Now, I have enabled a preview functionality within our web application, and RazorEngine now runs in our web app to compile and display the e-mail template preview.
However, I run into issues once deployed to azure app service (not locally).
This is the error message details:
This is the template (redacted some parts) it tried to compile:
The interesting part is that the list of loaded assemblies is crazy long. These are the first 10 lines:
However, it goes on for another 10,000+ more lines, 10,153 to be exact.
The stack trace points to the Compile method:
Any ideas? Thanks!