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 Cqrs.Configuration;
10 : using Microsoft.AspNet.SignalR;
11 : using Microsoft.Owin;
12 : using Microsoft.Owin.Cors;
13 : using Newtonsoft.Json;
14 : using Owin;
15 :
16 : [assembly: OwinStartup("Cqrs.WebApi.SignalR.Hubs.SignalRStartUp", typeof(Cqrs.WebApi.SignalR.Hubs.SignalRStartUp))]
17 : namespace Cqrs.WebApi.SignalR.Hubs
18 : {
19 : /// <summary>
20 : /// A Start-up class for SignalR. wired in using <see cref="OwinStartupAttribute">OwinStartup</see>.
21 : /// </summary>
22 : public class SignalRStartUp
23 1 : {
24 : /// <summary>
25 : /// Gets or sets the <see cref="IConfigurationManager"/>.
26 : /// </summary>
27 : public IConfigurationManager ConfigurationManager { get; set; }
28 :
29 : /// <summary>
30 : /// Instantiates a new instance of <see cref="SignalRStartUp"/> creating a new instance of <see cref="ConfigurationManager"/>.
31 : /// </summary>
32 1 : public SignalRStartUp()
33 : :this (new ConfigurationManager())
34 : {
35 : }
36 :
37 : /// <summary>
38 : /// Instantiates a new instance of <see cref="SignalRStartUp"/> with the provided <paramref name="configurationManager"/>.
39 : /// </summary>
40 1 : public SignalRStartUp(IConfigurationManager configurationManager)
41 : {
42 : ConfigurationManager = configurationManager;
43 : }
44 :
45 : /// <summary>
46 : /// Configure SignalR to run.
47 : /// </summary>
48 1 : public virtual void Configuration(IAppBuilder app)
49 : {
50 : string url;
51 : if (!ConfigurationManager.TryGetSetting("Cqrs.WebApi.SignalR.EndpointPath", out url) || string.IsNullOrWhiteSpace(url))
52 : url = "/signalr";
53 : // Branch the pipeline here for requests that start with "/signalr"
54 : app.Map(url, map =>
55 : {
56 : // Setup the CORS middleware to run before SignalR.
57 : // By default this will allow all origins. You can
58 : // configure the set of origins and/or http verbs by
59 : // providing a cors options with a different policy.
60 : map.UseCors(CorsOptions.AllowAll);
61 : var hubConfiguration = new HubConfiguration
62 : {
63 : // You can enable JSONP by un-commenting line below.
64 : // JSONP requests are insecure but some older browsers (and some
65 : // versions of IE) require JSONP to work cross domain
66 : // EnableJSONP = true
67 : EnableDetailedErrors = true,
68 : // Resolver = new SignalRResolver()
69 : };
70 : // Run the SignalR pipeline. We're not using MapSignalR
71 : // since this branch already runs under the "/signalr"
72 : // path.
73 : map.RunSignalR(hubConfiguration);
74 :
75 : // Instruct DI to resolve the Hub
76 : GlobalHost.DependencyResolver.Register
77 : (
78 : typeof(INotificationHub),
79 : () => DependencyResolver.Current.Resolve(typeof(INotificationHub))
80 : );
81 : GlobalHost.DependencyResolver.Register
82 : (
83 : typeof(NotificationHub),
84 : () => DependencyResolver.Current.Resolve(typeof(INotificationHub))
85 : );
86 : });
87 :
88 : JsonSerializer serializer = JsonSerializer.Create
89 : (
90 : new JsonSerializerSettings
91 : {
92 : ContractResolver = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.ContractResolver,
93 : StringEscapeHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.StringEscapeHandling,
94 : PreserveReferencesHandling = PreserveReferencesHandling.None,
95 : MissingMemberHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.MissingMemberHandling,
96 : Binder = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.Binder,
97 : CheckAdditionalContent = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.CheckAdditionalContent,
98 : ConstructorHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.ConstructorHandling,
99 : Context = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.Context,
100 : Converters = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.Converters,
101 : Culture = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.Culture,
102 : DateFormatHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.DateFormatHandling,
103 : DateFormatString = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.DateFormatString,
104 : DateParseHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.DateParseHandling,
105 : DateTimeZoneHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.DateTimeZoneHandling,
106 : DefaultValueHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.DefaultValueHandling,
107 : Error = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.Error,
108 : FloatFormatHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.FloatFormatHandling,
109 : FloatParseHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.FloatParseHandling,
110 : Formatting = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.Formatting,
111 : MaxDepth = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.MaxDepth,
112 : MetadataPropertyHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.MetadataPropertyHandling,
113 : NullValueHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.NullValueHandling,
114 : ObjectCreationHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.ObjectCreationHandling,
115 : ReferenceLoopHandling = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.ReferenceLoopHandling,
116 : ReferenceResolver = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.ReferenceResolver,
117 : TraceWriter = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.TraceWriter,
118 : TypeNameAssemblyFormat = Cqrs.Events.DefaultJsonSerializerSettings.DefaultSettings.TypeNameAssemblyFormat,
119 : TypeNameHandling = TypeNameHandling.None
120 : }
121 : );
122 : GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);
123 : }
124 : }
125 : }
|