Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="cdmdotnet Limited">
4 : // // Copyright cdmdotnet Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Collections.Generic;
11 : using System.Security;
12 : using System.Web.Http;
13 : using System.Web.Routing;
14 : using cdmdotnet.Logging;
15 : using Cqrs.Configuration;
16 : using Cqrs.WebApi.Configuration;
17 : using Cqrs.WebApi.Events.Handlers;
18 :
19 : namespace Cqrs.WebApi
20 : {
21 : public abstract class CqrsHttpApplication<TAuthenticationToken, TEventToHubProxy>
22 : : System.Web.HttpApplication
23 : where TEventToHubProxy : EventToHubProxy<TAuthenticationToken>
24 0 : {
25 : protected static IDependencyResolver DependencyResolver { get; set; }
26 :
27 0 : protected virtual void Application_Start(object sender, EventArgs e)
28 : {
29 : ConfigureDefaultDependencyResolver();
30 : RegisterDefaultRoutes();
31 :
32 : ConfigureMvc();
33 :
34 : BusRegistrar registrar = RegisterCommandAndEventHandlers();
35 : RegisterSignalR(registrar);
36 :
37 : LogApplicationStarted();
38 : }
39 :
40 0 : protected abstract void ConfigureDefaultDependencyResolver();
41 :
42 : /// <summary>
43 : /// Register SignalR to the path /signalr
44 : /// </summary>
45 1 : protected virtual void RegisterSignalR(BusRegistrar registrar)
46 : {
47 : RouteTable.Routes.MapOwinPath("/signalr");
48 : registrar.Register(typeof(TEventToHubProxy));
49 : }
50 :
51 : /// <summary>
52 : /// Register default offered routes and controllers such as the Java-script Client
53 : /// </summary>
54 1 : protected virtual void RegisterDefaultRoutes()
55 : {
56 : GlobalConfiguration.Configure(WebApiConfig.Register);
57 : }
58 :
59 : /// <summary>
60 : /// Override to configure MVC components such as AreaRegistration.RegisterAllAreas();
61 : /// </summary>
62 1 : protected virtual void ConfigureMvc()
63 : {
64 : }
65 :
66 0 : protected virtual BusRegistrar RegisterCommandAndEventHandlers()
67 : {
68 : var registrar = new BusRegistrar(DependencyResolver);
69 : return registrar;
70 : }
71 :
72 : /// <summary>
73 : /// Log that the application has started
74 : /// </summary>
75 1 : protected virtual void LogApplicationStarted()
76 : {
77 : try
78 : {
79 : ILogger logger = DependencyResolver.Resolve<ILogger>();
80 :
81 : if (logger != null)
82 : {
83 : DependencyResolver.Resolve<ICorrelationIdHelper>().SetCorrelationId(Guid.Empty);
84 : logger.LogInfo("Application started.");
85 : }
86 : }
87 : catch { /**/ }
88 : }
89 :
90 0 : protected virtual void Application_End(object sender, EventArgs e)
91 : {
92 : try
93 : {
94 : ILogger logger = DependencyResolver.Resolve<ILogger>();
95 :
96 : if (logger != null)
97 : {
98 : DependencyResolver.Resolve<ICorrelationIdHelper>().SetCorrelationId(Guid.Empty);
99 : logger.LogInfo("Application stopped.");
100 : }
101 : }
102 : catch { /**/ }
103 : }
104 :
105 0 : protected virtual void Application_Error(object sender, EventArgs e)
106 : {
107 : try
108 : {
109 : Exception ex = Server.GetLastError();
110 :
111 : ILogger logger = DependencyResolver.Resolve<ILogger>();
112 : Action<string, string, Exception, IDictionary<string, object>, IDictionary<string, object>> loggerFunction = logger.LogError;
113 : if (ex is SecurityException)
114 : loggerFunction = logger.LogWarning;
115 :
116 : loggerFunction("An error occurred.", null, ex, null, null);
117 : }
118 : catch { /**/ }
119 : }
120 :
121 0 : protected virtual void Application_BeginRequest(object sender, EventArgs e)
122 : {
123 : try
124 : {
125 : Guid correlationId = Guid.NewGuid();
126 : DependencyResolver.Resolve<ICorrelationIdHelper>().SetCorrelationId(correlationId);
127 : Response.AddHeader("CorrelationId", correlationId.ToString("N"));
128 : }
129 : catch (NullReferenceException) { }
130 : }
131 :
132 0 : protected virtual void Application_AuthenticateRequest(object sender, EventArgs e)
133 : {
134 : }
135 :
136 0 : protected virtual void Session_Start(object sender, EventArgs e)
137 : {
138 : // This is required otherwise the first call per new session will fail due to a WCF issue. This forces the SessionID to be created now, not after the response has been flushed on the pipeline.
139 : string sessionId = Session.SessionID;
140 : }
141 :
142 0 : protected virtual void Session_End(object sender, EventArgs e)
143 : {
144 : }
145 : }
146 : }
|