-
Notifications
You must be signed in to change notification settings - Fork 0
/
IServiceCollectionExtensions.cs
144 lines (117 loc) · 4.88 KB
/
IServiceCollectionExtensions.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
namespace SystemLibrary.Common.Web.Extensions;
/// <summary>
/// Extension methods for IServiceCollection
/// </summary>
public static partial class IServiceCollectionExtensions
{
public static IServiceCollection AddCommonWebServices(this IServiceCollection services, ServicesCollectionOptions options = null)
{
if (options == null)
options = new ServicesCollectionOptions();
if (options.UseForwardedHeaders)
services = services.UseForwardedHeaders();
if (options.UseHttpsRedirection)
services.AddHttpsRedirection(opt => opt.HttpsPort = 443);
if (options.UseGzipResponseCompression)
services = services.UseGzipCompression();
if (options.UseBrotliResponseCompression)
services = services.UseBrotliCompression();
if (options.UseOutputCache)
services.AddOutputCache();
if (options.UseResponseCaching)
services.AddResponseCaching();
IMvcBuilder builder = null;
if (options.UseMvc)
builder = services.AddMvc();
if (options.UseRazorPages)
builder = services.UseAddRazorPages(options);
if (options.UseControllers)
builder = services.UseAddControllers(options);
if (options.UseAutomaticKeyGenerationFile)
services.UseAutomaticKeyGenerationFile(options);
if (options.AddApplicationAsPart)
{
var executingAssembliy = Assembly.GetCallingAssembly();
var entryAssembly = Assembly.GetEntryAssembly();
builder = AddApplicationPart(builder, options, executingAssembliy, entryAssembly);
}
if (options.ApplicationParts != null)
{
foreach (var part in options.ApplicationParts)
if (part != null)
builder = AddApplicationPart(builder, options, part, null);
}
if (options.AddRazorRuntimeCompilationOnChange)
AddRazorRuntimeCompilationOnChange(builder);
services = services.UseViews(options);
if (options.UseCookiePolicy)
services = services.UseCookiePolicy();
if (options.AddForwardStandardLogging)
services.AddLogging(builder =>
{
builder.AddProvider(new InternalLogProvider());
});
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
// NOTE: Can this be Scoped instead?
services.TryAddTransient<HtmlHelperFactory, HtmlHelperFactory>();
services = services.Configure<IISServerOptions>(options => { options.AllowSynchronousIO = true; });
Services.Collection = services;
return services;
}
/// <summary>
/// Configures ServiceCollection in one-line, so register all of your own or other service configurations after this one
///
/// Registers:
/// - MVC services
/// - Razor Page services
/// - Routing services
/// - ForwardedProtocol and ForwardedIp (XForwardedFor) headers
/// - Compression for Gzip and Brotli services
/// - Authentication and authorization services
/// - Output cache services
/// - Registers the main assembly and all its controllers (if any), as in: your Web Application Project's assembly
///
/// Optionally, through the argument ServicesCollectionOptions:
/// - Can register view locations
/// - Can register area view locations
/// - Can register one ViewLocationExpander
/// - and more...
/// </summary>
/// <example>
/// Startup.cs/Program.cs:
/// <code>
/// public void ConfigureServices(IServiceCollection services)
/// {
/// var options = new ServicesCollectionOptions();
///
/// options.ViewLocations = new string[] {
/// "~/Views/{0}/index.cshtml"
/// }
///
/// options.AreaViewLocations = new string[] {
/// "~/Area/{2}/{1}/{0}.cshtml"
/// }
///
/// options.ViewLocationExpander = null; //or create one based on the Interface 'IViewLocationExpander'
///
/// services.AddCommonWebServices(options);
/// }
/// </code>
/// </example>
public static IServiceCollection AddCommonWebServices<TLogWriter>(this IServiceCollection services, ServicesCollectionOptions options = null) where TLogWriter : class, ILogWriter
{
services = AddCommonWebServices(services, options);
services = services.AddTransient<ILogWriter, TLogWriter>();
return services;
}
}