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 System;
10 : using System.Web.Http;
11 : using System.Web.Routing;
12 : using Cqrs.Bus;
13 : using Cqrs.Configuration;
14 : using Cqrs.Events;
15 : using Cqrs.Hosts;
16 : using Cqrs.WebApi.Configuration;
17 : using Cqrs.WebApi.Events.Handlers;
18 :
19 : namespace Cqrs.WebApi
20 : {
21 : /// <summary>
22 : /// A <see cref="CqrsHttpApplication"/> that allows you to specify how <see cref="IEvent{TAuthenticationToken}">events</see> are sent to SignalR.
23 : /// </summary>
24 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
25 : /// <typeparam name="TEventToHubProxy">The <see cref="Type"/> of the proxy class that specifies how <see cref="IEvent{TAuthenticationToken}">events</see> are sent to SignalR.</typeparam>
26 : public abstract class CqrsHttpApplicationWithSignalR<TAuthenticationToken, TEventToHubProxy>
27 : : CqrsHttpApplication<TAuthenticationToken>
28 : where TEventToHubProxy : EventToHubProxy<TAuthenticationToken>
29 : {
30 : /// <summary>
31 : /// Calls <see cref="CqrsHttpApplication.SetBuses"/>, <see cref="RegisterDefaultRoutes"/>, <see cref="CqrsHttpApplication.RegisterServiceParameterResolver"/>,
32 : /// <see cref="ConfigureMvcOrWebApi"/>, <see cref="CqrsHttpApplication.RegisterCommandAndEventHandlers"/>, <see cref="RegisterSignalR"/>,
33 : /// <see cref="CqrsHttpApplication.StartBuses"/> and finally <see cref="CqrsHttpApplication.LogApplicationStarted"/>.
34 : /// </summary>
35 2 : protected override void Application_Start(object sender, EventArgs e)
36 : {
37 : SetBuses();
38 :
39 : RegisterDefaultRoutes();
40 : RegisterServiceParameterResolver();
41 :
42 : ConfigureMvcOrWebApi();
43 :
44 : BusRegistrar registrar = RegisterCommandAndEventHandlers();
45 : RegisterSignalR(registrar);
46 :
47 : StartBuses();
48 :
49 : LogApplicationStarted();
50 : }
51 :
52 : /// <summary>
53 : /// Register SignalR to the path /signalr
54 : /// </summary>
55 2 : protected virtual void RegisterSignalR(BusRegistrar registrar)
56 : {
57 : RouteTable.Routes.MapOwinPath("/signalr");
58 : registrar.Register(typeof(TEventToHubProxy));
59 : }
60 :
61 : /// <summary>
62 : /// Register default offered routes and controllers such as the Java-script Client
63 : /// </summary>
64 2 : protected virtual void RegisterDefaultRoutes()
65 : {
66 : GlobalConfiguration.Configure(WebApiConfig.Register);
67 : }
68 :
69 : /// <summary>
70 : /// Override to configure MVC or WebAPI components such as AreaRegistration.RegisterAllAreas();
71 : /// </summary>
72 2 : protected virtual void ConfigureMvcOrWebApi()
73 : {
74 : }
75 : }
76 :
77 : /// <summary>
78 : /// A <see cref="CqrsHttpApplication"/> that uses the <see cref="GlobalEventToHubProxy{TAuthenticationToken}"/> to automatically proxy all <see cref="IEvent{TAuthenticationToken}">events</see> to SignalR
79 : /// </summary>
80 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
81 : public abstract class CqrsHttpApplicationWithSignalR<TAuthenticationToken>
82 : : CqrsHttpApplicationWithSignalR<TAuthenticationToken, GlobalEventToHubProxy<TAuthenticationToken>>
83 2 : {
84 : #region Overrides of CqrsHttpApplicationWithSignalR<TAuthenticationToken, TEventToHubProxy>
85 :
86 : /// <summary>
87 : /// Register SignalR and auto wire-up <see cref="GlobalEventToHubProxy{TAuthenticationToken}"/> to automatically proxy all <see cref="IEvent{TAuthenticationToken}">events</see> to SignalR.
88 : /// </summary>
89 2 : protected override void RegisterSignalR(BusRegistrar registrar)
90 : {
91 : base.RegisterSignalR(registrar);
92 :
93 : var eventHandlerRegistrar = DependencyResolver.Current.Resolve<IEventHandlerRegistrar>();
94 : var proxy = DependencyResolver.Current.Resolve<GlobalEventToHubProxy<TAuthenticationToken>>();
95 : eventHandlerRegistrar.RegisterGlobalEventHandler<IEvent<TAuthenticationToken>>(proxy.Handle, false);
96 : }
97 :
98 : #endregion
99 : }
100 : }
|