-
Notifications
You must be signed in to change notification settings - Fork 1
/
HostContainerFactory.cs
127 lines (106 loc) · 6.31 KB
/
HostContainerFactory.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
using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Builder;
using Lombiq.OrchardAppHost.Configuration;
using Lombiq.OrchardAppHost.Environment;
using Orchard.Caching;
using Orchard.Environment;
using Orchard.Environment.Extensions.Folders;
using Orchard.Environment.Extensions.Loaders;
using Orchard.FileSystems.AppData;
using Orchard.FileSystems.VirtualPath;
using Orchard.FileSystems.WebSite;
using Orchard.Localization.Services;
using Orchard.Logging;
namespace Lombiq.OrchardAppHost
{
/// <summary>
/// Builds the Autofac container with all the default registrations.
/// </summary>
internal static class HostContainerFactory
{
public static IContainer CreateHostContainer(IOrchardAppHost appHost, AppHostSettings settings, AppHostRegistrations registrations)
{
return OrchardStarter.CreateHostContainer(builder =>
{
builder.RegisterType<AppHostEnvironment>().As<IHostEnvironment>().SingleInstance();
builder.RegisterType<ApplicationEnvironment>().As<IApplicationEnvironment>().SingleInstance();
builder.RegisterType<CultureDateTimeFormatProvider>().As<IDateTimeFormatProvider>().SingleInstance();
// Needed also for shells, separately.
RegisterAppDataFolderRoot(builder, settings.AppDataFolderPath).SingleInstance();
RegisterVolatileProvider<AppHostVirtualPathMonitor, IVirtualPathMonitor>(builder);
RegisterVolatileProvider<AppHostVirtualPathProvider, IVirtualPathProvider>(builder);
RegisterVolatileProvider<AppHostWebSiteFolder, IWebSiteFolder>(builder);
var shellRegistrations = new ShellContainerRegistrations
{
Registrations = shellBuilder =>
{
// Despite imported assemblies being handled these registrations are necessary, because they are needed too early.
// Adding them as normal services, even with OrchardSuppressDependency, wouldn't work.
RegisterAppDataFolderRoot(shellBuilder, settings.AppDataFolderPath).InstancePerMatchingLifetimeScope("shell");
RegisterVolatileProviderForShell<AppHostVirtualPathMonitor, IVirtualPathMonitor>(shellBuilder);
RegisterVolatileProviderForShell<AppHostVirtualPathProvider, IVirtualPathProvider>(shellBuilder);
RegisterVolatileProviderForShell<AppHostWebSiteFolder, IWebSiteFolder>(shellBuilder);
registrations.ShellRegistrations?.Invoke(shellBuilder);
}
};
builder.RegisterInstance(shellRegistrations).As<IShellContainerRegistrations>();
// Handling imported assemblies.
if (settings.ImportedExtensions != null && settings.ImportedExtensions.Any())
{
builder.RegisterType<ImportedExtensionsProvider>().As<IExtensionFolders, IExtensionLoader>().SingleInstance()
.WithParameter(new NamedParameter("extensions", settings.ImportedExtensions));
}
// Configuring extension loading.
builder.RegisterType<ExtensionPathsProvider>().As<IExtensionPathsProvider>().SingleInstance()
.WithParameter(new NamedParameter("hostSettings", settings));
builder.RegisterType<AppHostExtensionFolders>().As<IExtensionFolders>().SingleInstance();
builder.RegisterType<AppHostCoreExtensionLoader>().As<IExtensionLoader>().SingleInstance();
builder.RegisterType<AppHostRawThemeExtensionLoader>().As<IExtensionLoader>().SingleInstance();
if (settings.DisableConfiguratonCaches)
{
builder.RegisterModule<ConfigurationCacheDisablingModule>();
}
if (settings.DisableExtensionMonitoring)
{
builder.RegisterModule<ExtensionMonitoringDisablingModule>();
}
// Either we register MVC singletons or we need at least a new IOrchardShell implementation.
builder.Register(ctx => RouteTable.Routes).SingleInstance();
builder.Register(ctx => ModelBinders.Binders).SingleInstance();
builder.Register(ctx => ViewEngines.Engines).SingleInstance();
builder.RegisterType<LoggerService>().As<ILoggerService>().SingleInstance();
builder.RegisterInstance(appHost).As<IOrchardAppHost>().ExternallyOwned();
registrations.HostRegistrations?.Invoke(builder);
});
}
private static IRegistrationBuilder<AppHostAppDataFolderRoot, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterAppDataFolderRoot(ContainerBuilder builder, string appDataFolderPath)
{
var appDataRootRegistration = builder.RegisterType<AppHostAppDataFolderRoot>().As<IAppDataFolderRoot>();
if (!string.IsNullOrEmpty(appDataFolderPath))
{
appDataRootRegistration.WithParameter(new NamedParameter("rootPath", appDataFolderPath));
}
return appDataRootRegistration;
}
private static IRegistrationBuilder<TRegister, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterVolatileProvider<TRegister, TService>(ContainerBuilder builder)
where TService : IVolatileProvider
{
return builder.RegisterType<TRegister>()
.As<TService, IVolatileProvider>()
.SingleInstance();
}
private static IRegistrationBuilder<TRegister, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterVolatileProviderForShell<TRegister, TService>(ContainerBuilder builder)
where TService : IVolatileProvider
{
return RegisterVolatileProvider<TRegister, TService>(builder).InstancePerMatchingLifetimeScope("shell");
}
private class ShellContainerRegistrations : IShellContainerRegistrations
{
public Action<ContainerBuilder> Registrations { get; set; }
}
}
}