Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET 6 - Functions v4 #3

Open
RichardPoes opened this issue Nov 2, 2022 · 4 comments
Open

.NET 6 - Functions v4 #3

RichardPoes opened this issue Nov 2, 2022 · 4 comments

Comments

@RichardPoes
Copy link

RichardPoes commented Nov 2, 2022

Hi,
A little far fetched maybe, but when I try to port this to .NET 6.0 with Functions v4 I first got the following error:

Message=Could not load file or assembly 'C:\CodeFiddles\AzureFunctionsRazorEmailTemplateSample\bin\Debug\net6.0\bin\AzureFunctionsRazorEmailTemplateSample.Views.dll'. 
The system cannot find the file specified.

This is because in v4/NET6 this dll is not generated. Therefore I thought, let's change the line

var compiledViewAssembly = Assembly.LoadFile(Path.Combine(executionPath, AzureFunctionsRazorEmailTemplateSample.Views.dll"));

to

var compiledViewAssembly = Assembly.LoadFile(Path.Combine(executionPath, "AzureFunctionsRazorEmailTemplateSample.dll"));

Upon which the whole project can build, but when trying to run the function, I get the exception:

System.Private.CoreLib: Exception while executing function: 
SendRazorTemplateEmail. Microsoft.AspNetCore.Mvc.ViewFeatures:
The model item passed into the ViewDataDictionary is of type 'AzureFunctionsRazorEmailTemplateSample.Views.SampleEmailTemplateModel', 
but this ViewDataDictionary instance requires a model item of type 'AzureFunctionsRazorEmailTemplateSample.Views.SampleEmailTemplateModel'.

I'm still studying how your example exactly works, so maybe I will be able to find out on my own.
Specifically I'm investigating what the these two lines below, listed inside the Configure method inside the Startup class, are doing.

var executionPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var compiledViewAssembly = Assembly.LoadFile(Path.Combine(executionPath, "AzureFunctionsRazorEmailTemplateSample.Views.dll"));

But please let me know if you have any ideas.

@EtiennePeeters
Copy link
Owner

Hi,

Thanks for your detailed question! I just tried to convert it to .NET 6, and I'm running into the same problem as you do.

The 2 lines, together with .AddApplicationPart(compiledViewAssembly), were to load the views dll. It was only necessary because the Azure Functions runtime would not load external dlls.

But because the .NET 6 Razor compiler no longer produces a Views assembly, the views should be included in the main dll. So that code is not necessary anymore.

So I think there could be 2 things happening:

  1. The views are somehow not in the main dll.
  2. The RazorViewToStringRenderer does not load the views correctly.

In the linked github discussion it looks like someone found out how to find the views, but I have not read that in detail yet.

I will try to look into this soon, but I have little time the coming days.
If you figure this out, please let me know. I have not worked with Azure Functions for almost a year, but this made me very curious!

@RichardPoes
Copy link
Author

RichardPoes commented Nov 4, 2022

Okay, so I did not manage to make it work. But I think I have at least some interesting information to share, so that either one of us can continue trying to fix this.

First things first: I have managed to make it work differently, using the [Razor.Templating.Core] package and following the tutorial here. The person who made this package also is engaging in the discussion you mentioned.

However, after reading your reply I continued working on trying to make this example work, as I preferably don't rely on external packages and in your example everything is dependency injected, instead of relying on a static class to do the rendering for us. Furthermore this problem just grabs me.

So I refactored and changed your code a bit into this. Then I tried upgrading again and, given your explanation about the two lines I asked about, I removed those lines. The Configure method then looks like:

 var services = builder.Services;
services.AddSingleton<IStringLocalizerFactory, ResourceManagerStringLocalizerFactory>();
services.AddScoped<IRazorViewRenderer, RazorViewRenderer>();
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services
    .AddMvcCore()
    .AddViews()
    .AddRazorViewEngine();

Sadly I still run into an error, but I think that that one is exactly telling us about what problem needs to be solved.

Exception while executing function: SendRazorTemplate. 
RRS.FunctionApp: Unable to find view 'PocTemplate'. The following locations were searched:
/Views/PocTemplate.cshtml
/Views/Shared/PocTemplate.cshtml.

As the creator of aforementioned package might have run into this exact same problem, I think it might be worthwhile to investigate their code, which is open source as well.

@hardeepsidhu9
Copy link

Hi @EtiennePeeters and @RichardPoes,
Did either of you manage to get further with this in the end? I'm encountering the same issue

@RichardPoes
Copy link
Author

I did not. In the end I went to solve my problems with Blazor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants