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 core compatibility #14

Open
GDreyV opened this issue Oct 1, 2022 · 1 comment
Open

.net 6 core compatibility #14

GDreyV opened this issue Oct 1, 2022 · 1 comment

Comments

@GDreyV
Copy link

GDreyV commented Oct 1, 2022

Hi! I really love the idea to use Razor in console applications or services, but it requires couple updates to make it work with .net 6

// It's required by IOC
var diagnosticListener = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton(diagnosticListener);
services.AddSingleton<DiagnosticSource>(diagnosticListener);

// Here we need to register assembly to make razor find compiled views            
services.AddRazorPages()
  .AddApplicationPart(Assembly.GetExecutingAssembly())
  .AddRazorRuntimeCompilation();

// Now we need to add IWebHostEnvironment with custom implementation as default one is internal.
// Implementation is just bunch of properties without any additional logic.
var fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
services.AddSingleton<Microsoft.AspNetCore.Hosting.IWebHostEnvironment>(new HostingEnvironment
            {
                ApplicationName = Assembly.GetEntryAssembly().GetName().Name,
                WebRootFileProvider = fileProvider,
                ContentRootFileProvider = fileProvider
            });
@rolfkbk
Copy link

rolfkbk commented Sep 8, 2023

Thank you very much for sharing this.
In .net 7, This: services.AddSingleton<Microsoft.AspNetCore.Hosting.IWebHostEnvironment>(new HostingEnvironment... didn't work. We had to create our own implementation of IWebHostEnvironment public sealed class WebHostEnvironment : IWebHostEnvironment {...} and then use that instaid of HostingEnvironment

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

2 participants