forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsEngineSwitcherServiceCollectionExtensions.cs
More file actions
95 lines (80 loc) · 3.57 KB
/
JsEngineSwitcherServiceCollectionExtensions.cs
File metadata and controls
95 lines (80 loc) · 3.57 KB
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
using System;
using Microsoft.Extensions.DependencyInjection;
using JavaScriptEngineSwitcher.Core;
namespace JavaScriptEngineSwitcher.Extensions.MsDependencyInjection
{
/// <summary>
/// Extension methods for adding the JS engine switcher in an <see cref="IServiceCollection" />
/// </summary>
public static class JsEngineSwitcherServiceCollectionExtensions
{
/// <summary>
/// Adds a default instance of JS engine switcher to <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The services available in the application</param>
/// <returns>Instance of <see cref="JsEngineFactoryCollection" /></returns>
public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services)
{
return AddJsEngineSwitcher(services, (IJsEngineSwitcher)null);
}
/// <summary>
/// Adds a specified instance of JS engine switcher to <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The services available in the application</param>
/// <param name="engineSwitcher">Instance of JS engine switcher</param>
/// <returns>Instance of <see cref="JsEngineFactoryCollection" /></returns>
public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services,
IJsEngineSwitcher engineSwitcher)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
// Set the current instance of JS engine switcher
JsEngineSwitcher.Current = engineSwitcher;
// Get the current instance of JS engine switcher
IJsEngineSwitcher currentEngineSwitcher = JsEngineSwitcher.Current;
// Register the current instance of JS engine switcher as a service
services.AddSingleton(currentEngineSwitcher);
return currentEngineSwitcher.EngineFactories;
}
/// <summary>
/// Adds a default instance of JS engine switcher to <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The services available in the application</param>
/// <param name="configure">The <see cref="IJsEngineSwitcher"/> which need to be configured</param>
/// <returns>Instance of <see cref="JsEngineFactoryCollection" /></returns>
public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services,
Action<IJsEngineSwitcher> configure)
{
return AddJsEngineSwitcher(services, null, configure);
}
/// <summary>
/// Adds a specified instance of JS engine switcher to <see cref="IServiceCollection"/>
/// </summary>
/// <param name="services">The services available in the application</param>
/// <param name="engineSwitcher">Instance of JS engine switcher</param>
/// <param name="configure">The <see cref="IJsEngineSwitcher"/> which need to be configured</param>
/// <returns>Instance of <see cref="JsEngineFactoryCollection" /></returns>
public static JsEngineFactoryCollection AddJsEngineSwitcher(this IServiceCollection services,
IJsEngineSwitcher engineSwitcher, Action<IJsEngineSwitcher> configure)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
// Set the current instance of JS engine switcher
JsEngineSwitcher.Current = engineSwitcher;
// Get and configure the current instance of JS engine switcher
IJsEngineSwitcher currentEngineSwitcher = JsEngineSwitcher.Current;
configure(currentEngineSwitcher);
// Register the current instance of JS engine switcher as a service
services.AddSingleton(currentEngineSwitcher);
return currentEngineSwitcher.EngineFactories;
}
}
}