Skip to content

.net 6 core compatibility #14

@GDreyV

Description

@GDreyV

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
            });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions