-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderer.cs
80 lines (66 loc) · 3.03 KB
/
Renderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
namespace RazorRenderer
{
public class Renderer
{
public static IServiceScopeFactory ServiceScopeFactory { get; set; }
public static void Initialize()
{
ServiceScopeFactory = InitializeServices();
}
public static Task<string> RenderViewAsync<TModel>(string viewName, TModel model) =>
RenderViewAsync(ServiceScopeFactory, viewName, model);
private static IServiceScopeFactory InitializeServices(string customApplicationBasePath = null)
{
// Initialize the necessary services
var services = new ServiceCollection();
ConfigureDefaultServices(services, customApplicationBasePath);
// Add a custom service that is used in the view.
services.AddSingleton<EmailReportGenerator>();
var serviceProvider = services.BuildServiceProvider();
return serviceProvider.GetRequiredService<IServiceScopeFactory>();
}
private static Task<string> RenderViewAsync<TModel>(IServiceScopeFactory scopeFactory, string viewName, TModel model)
{
IServiceScope serviceScope = scopeFactory.CreateScope();
RazorViewToStringRenderer renderer = serviceScope.ServiceProvider
.GetRequiredService<RazorViewToStringRenderer>();
return renderer.RenderViewToStringAsync(viewName, model);
}
private static void ConfigureDefaultServices(IServiceCollection services, string customApplicationBasePath)
{
string applicationName;
IFileProvider fileProvider;
if (!string.IsNullOrEmpty(customApplicationBasePath))
{
applicationName = Path.GetFileName(customApplicationBasePath);
fileProvider = new PhysicalFileProvider(customApplicationBasePath);
}
else
{
applicationName = Assembly.GetEntryAssembly().GetName().Name;
fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
}
services.AddSingleton<IWebHostEnvironment>(new WebHostEnvironment
{
ApplicationName = applicationName,
WebRootFileProvider = fileProvider,
});
var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton<DiagnosticSource>(diagnosticSource);
services.AddSingleton<DiagnosticListener>(diagnosticSource);
services.AddLogging();
services.AddControllersWithViews();
services.AddTransient<RazorViewToStringRenderer>();
}
}
}