Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using Chinchilla.Logging;
10 : using Chinchilla.Logging.Azure.ApplicationInsights;
11 : using Chinchilla.Logging.Azure.Configuration;
12 : using Chinchilla.Logging.Configuration;
13 : using Chinchilla.StateManagement;
14 : using Cqrs.Azure.ConfigurationManager;
15 : using Cqrs.Configuration;
16 : using Ninject;
17 : using Ninject.Modules;
18 : #if NET472
19 : using Ninject.Web.Common;
20 : using System.Web;
21 : #endif
22 : using System;
23 : using Chinchilla.StateManagement.Web;
24 : using Cqrs.Authentication;
25 : using Cqrs.Ninject.Configuration;
26 : using Cqrs.Services;
27 :
28 : namespace Cqrs.Ninject.Azure.Wcf.Configuration
29 : {
30 : /// <summary>
31 : /// The core <see cref="INinjectModule"/> for use defining base level requirements.
32 : /// </summary>
33 : public class WebHostModule : ResolvableModule
34 1 : {
35 : #region Overrides of NinjectModule
36 :
37 : /// <summary>
38 : /// Loads the module into the kernel.
39 : /// </summary>
40 1 : public override void Load()
41 : {
42 : RegisterBasicHelpers();
43 : RegisterAzureConfigurations();
44 : RegisterBasicSerives();
45 : RegisterWebBit();
46 : }
47 :
48 : #endregion
49 :
50 : /// <summary>
51 : /// Register the all Azure configurations
52 : /// </summary>
53 1 : protected virtual void RegisterAzureConfigurations()
54 : {
55 : Bind<ILoggerSettings>()
56 : .To<AzureLoggerSettingsConfiguration>()
57 : .InSingletonScope();
58 :
59 : Bind<IConfigurationManager>()
60 : .To<CloudConfigurationManager>()
61 : .InSingletonScope();
62 : }
63 :
64 : /// <summary>
65 : /// Registers the basic helpers required.
66 : /// </summary>
67 1 : protected virtual void RegisterBasicHelpers()
68 : {
69 : RegisterContextItemCollectionFactory();
70 :
71 : Bind<ITelemetryHelper>()
72 : .To<TelemetryHelper>()
73 : .InSingletonScope();
74 : }
75 : /// <summary>
76 : /// Registers the <see cref="IContextItemCollectionFactory"/> required.
77 : /// </summary>
78 1 : protected virtual void RegisterContextItemCollectionFactory()
79 : {
80 : // We use console state as, even though a webjob runs in an azure website, it's technically loaded via something call the 'WindowsScriptHost', which is not web and IIS based so it's threading model is very different and more console based.
81 : Bind<IContextItemCollectionFactory>()
82 : .To<WebContextItemCollectionFactory>()
83 : .InSingletonScope();
84 : }
85 :
86 : /// <summary>
87 : /// Registers the basic services required.
88 : /// </summary>
89 1 : protected virtual void RegisterBasicSerives()
90 : {
91 : string authenticationType;
92 : if (!Resolve<IConfigurationManager>().TryGetSetting("Cqrs.AuthenticationTokenType", out authenticationType))
93 : authenticationType = "Guid";
94 :
95 : if (authenticationType.ToLowerInvariant() == "int" || authenticationType.ToLowerInvariant() == "integer")
96 : Bind<IUnitOfWorkService>()
97 : .To<UnitOfWorkService<int>>()
98 : .InThreadScope();
99 : else if (authenticationType.ToLowerInvariant() == "guid")
100 : Bind<IUnitOfWorkService>()
101 : .To<UnitOfWorkService<Guid>>()
102 : .InThreadScope();
103 : else if (authenticationType.ToLowerInvariant() == "string" || authenticationType.ToLowerInvariant() == "text")
104 : Bind<IUnitOfWorkService>()
105 : .To<UnitOfWorkService<string>>()
106 : .InThreadScope();
107 :
108 : else if (authenticationType == "SingleSignOnToken")
109 : Bind<IUnitOfWorkService>()
110 : .To<UnitOfWorkService<SingleSignOnToken>>()
111 : .InThreadScope();
112 : else if (authenticationType == "SingleSignOnTokenWithUserRsn")
113 : Bind<IUnitOfWorkService>()
114 : .To<UnitOfWorkService<SingleSignOnTokenWithUserRsn>>()
115 : .InThreadScope();
116 : else if (authenticationType == "SingleSignOnTokenWithCompanyRsn")
117 : Bind<IUnitOfWorkService>()
118 : .To<UnitOfWorkService<SingleSignOnTokenWithCompanyRsn>>()
119 : .InThreadScope();
120 : else if (authenticationType == "SingleSignOnTokenWithUserRsnAndCompanyRsn")
121 : Bind<IUnitOfWorkService>()
122 : .To<UnitOfWorkService<SingleSignOnTokenWithUserRsnAndCompanyRsn>>()
123 : .InThreadScope();
124 :
125 : else if (authenticationType == "ISingleSignOnToken")
126 : Bind<IUnitOfWorkService>()
127 : .To<UnitOfWorkService<ISingleSignOnToken>>()
128 : .InThreadScope();
129 : else if (authenticationType == "ISingleSignOnTokenWithUserRsn")
130 : Bind<IUnitOfWorkService>()
131 : .To<UnitOfWorkService<ISingleSignOnTokenWithUserRsn>>()
132 : .InThreadScope();
133 : else if (authenticationType == "ISingleSignOnTokenWithCompanyRsn")
134 : Bind<IUnitOfWorkService>()
135 : .To<UnitOfWorkService<ISingleSignOnTokenWithCompanyRsn>>()
136 : .InThreadScope();
137 : else if (authenticationType == "ISingleSignOnTokenWithUserRsnAndCompanyRsn")
138 : Bind<IUnitOfWorkService>()
139 : .To<UnitOfWorkService<ISingleSignOnTokenWithUserRsnAndCompanyRsn>>()
140 : .InThreadScope();
141 : }
142 :
143 : /// <summary>
144 : /// Registers some things Ninject likes.
145 : /// </summary>
146 1 : protected virtual void RegisterWebBit()
147 : {
148 : #if NET472
149 : Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
150 : Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
151 : #endif
152 : }
153 : }
154 : }
|