Line data Source code
1 : using System;
2 : using Cqrs.Authentication;
3 : using Cqrs.Configuration;
4 : using Cqrs.Ninject.Configuration;
5 : using Ninject;
6 : using Ninject.Web.Common;
7 :
8 : [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Cqrs.Ninject.WebApi.Configuration.SimplifiedNinjectWebApi), "Start", Order = 50)]
9 : [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(Cqrs.Ninject.WebApi.Configuration.SimplifiedNinjectWebApi), "Stop", Order = 50)]
10 :
11 : namespace Cqrs.Ninject.WebApi.Configuration
12 : {
13 : using Microsoft.Web.Infrastructure.DynamicModuleHelper;
14 :
15 : public static class SimplifiedNinjectWebApi
16 0 : {
17 : private static readonly Bootstrapper Bootstrapper = new Bootstrapper();
18 :
19 : /// <summary>
20 : /// Starts the application
21 : /// </summary>
22 1 : public static void Start()
23 : {
24 : DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
25 : DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
26 : Bootstrapper.Initialize(CreateKernel);
27 : }
28 :
29 : /// <summary>
30 : /// Stops the application.
31 : /// </summary>
32 1 : public static void Stop()
33 : {
34 : Bootstrapper.ShutDown();
35 : }
36 :
37 : /// <summary>
38 : /// Creates the kernel that will manage your application.
39 : /// </summary>
40 : /// <returns>The created kernel.</returns>
41 : private static IKernel CreateKernel()
42 : {
43 : NinjectDependencyResolver.ModulesToLoad.Insert(0, new WebApiModule());
44 :
45 : string authenticationType;
46 : if (!new ConfigurationManager().TryGetSetting("Cqrs.WebApi.AuthenticationTokenType", out authenticationType))
47 : authenticationType = "Guid";
48 :
49 : if (authenticationType.ToLowerInvariant() == "int" || authenticationType.ToLowerInvariant() == "integer")
50 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<int, DefaultAuthenticationTokenHelper>(true, false));
51 : else if (authenticationType.ToLowerInvariant() == "guid")
52 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<Guid, DefaultAuthenticationTokenHelper>(true, false));
53 : else if (authenticationType.ToLowerInvariant() == "string" || authenticationType.ToLowerInvariant() == "text")
54 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<string, DefaultAuthenticationTokenHelper>(true, false));
55 :
56 : else if (authenticationType == "SingleSignOnToken")
57 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<SingleSignOnToken, DefaultAuthenticationTokenHelper>(true, false));
58 : else if (authenticationType == "SingleSignOnTokenWithUserRsn")
59 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<SingleSignOnTokenWithUserRsn, DefaultAuthenticationTokenHelper>(true, false));
60 : else if (authenticationType == "SingleSignOnTokenWithCompanyRsn")
61 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<SingleSignOnTokenWithCompanyRsn, DefaultAuthenticationTokenHelper>(true, false));
62 : else if (authenticationType == "SingleSignOnTokenWithUserRsnAndCompanyRsn")
63 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<SingleSignOnTokenWithUserRsnAndCompanyRsn, DefaultAuthenticationTokenHelper>(true, false));
64 :
65 : else if (authenticationType == "ISingleSignOnToken")
66 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<ISingleSignOnToken, AuthenticationTokenHelper>(true, false));
67 : else if (authenticationType == "ISingleSignOnTokenWithUserRsn")
68 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<ISingleSignOnTokenWithUserRsn, AuthenticationTokenHelper>(true, false));
69 : else if (authenticationType == "ISingleSignOnTokenWithCompanyRsn")
70 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<ISingleSignOnTokenWithCompanyRsn, AuthenticationTokenHelper>(true, false));
71 : else if (authenticationType == "ISingleSignOnTokenWithUserRsnAndCompanyRsn")
72 : NinjectDependencyResolver.ModulesToLoad.Insert(1, new CqrsModule<ISingleSignOnTokenWithUserRsnAndCompanyRsn, AuthenticationTokenHelper>(true, false));
73 :
74 : NinjectDependencyResolver.ModulesToLoad.Insert(2, new SimplifiedSqlModule<SingleSignOnToken>());
75 :
76 : // NinjectDependencyResolver.Start();
77 : var kernel = new StandardKernel();
78 : // This is only done so the follow Wcf safe method can be called. Otherwise use the commented out line above.
79 : NinjectDependencyResolver.Start(kernel, true);
80 :
81 : return kernel;
82 : }
83 : }
84 : }
|