/// <summary> /// Includes dotNetify in the application request pipeline. /// </summary> /// <param name="appBuilder">Application builder.</param> /// <param name="config">Delegate to provide custom configuration.</param> /// <returns></returns> public static IApplicationBuilder UseDotNetify(this IApplicationBuilder appBuilder, Action <IDotNetifyConfiguration> config = null) { var provider = appBuilder.ApplicationServices; var vmControllerFactory = provider.GetService <IVMControllerFactory>(); if (vmControllerFactory == null) { throw new InvalidOperationException("Please call 'IServiceCollection.AddDotNetify()' inside the ConfigureServices() of the startup class."); } var scopedServiceProvider = provider.GetService <IHubServiceProvider>(); // Use ASP.NET Core DI to provide view model instances by default. Func <Type, object[], object> factoryMethod = (type, args) => { try { return(ActivatorUtilities.CreateInstance(scopedServiceProvider.ServiceProvider ?? provider, type, args ?? new object[] { })); } catch (Exception ex) { Trace.Fail(ex.Message); throw ex; } }; var dotNetifyConfig = new DotNetifyConfiguration(); dotNetifyConfig.SetFactoryMethod(factoryMethod); config?.Invoke(dotNetifyConfig); // If no view model assembly has been registered, default to the entry assembly. if (!dotNetifyConfig.HasAssembly) { dotNetifyConfig.RegisterEntryAssembly(); } // Sets how long to keep a view model controller in memory after it hasn't been accessed for a while. if (dotNetifyConfig.VMControllerCacheExpiration.HasValue) { vmControllerFactory.CacheExpiration = dotNetifyConfig.VMControllerCacheExpiration; } // Add middleware to extract headers from incoming requests. _middlewareTypes.Insert(0, Tuple.Create(typeof(ExtractHeadersMiddleware), new object[] { })); // Add middleware factories to the hub. var middlewareFactories = provider.GetService <IList <Tuple <Type, Func <IMiddlewarePipeline> > > >(); _middlewareTypes.ForEach(t => middlewareFactories?.Add(Tuple.Create <Type, Func <IMiddlewarePipeline> >(t.Item1, () => (IMiddlewarePipeline)factoryMethod(t.Item1, t.Item2)))); // Add filter factories to the hub. var filterFactories = provider.GetService <IDictionary <Type, Func <IVMFilter> > >(); _filterTypes.ForEach(t => filterFactories?.Add(t.Item1, () => (IVMFilter)factoryMethod(t.Item1, t.Item2))); return(appBuilder); }
public static IApplicationBuilder UseDotNetify(this IApplicationBuilder appBuilder, Action <IDotNetifyConfiguration> config = null) { var provider = appBuilder.ApplicationServices; // Make sure all the required services are there. if (provider.GetService <IMemoryCache>() == null) { throw new InvalidOperationException("No service of type IMemoryCache has been registered. Please add the service by calling 'IServiceCollection.AddMemoryCache()' in the startup class."); } var vmControllerFactory = provider.GetService <IVMControllerFactory>(); if (vmControllerFactory == null) { throw new InvalidOperationException("Please call 'IServiceCollection.AddDotNetify()' inside the ConfigureServices() of the startup class."); } // Use ASP.NET Core DI to provide view model instances by default. Func <Type, object[], object> factoryMethod = (type, args) => { try { return(ActivatorUtilities.CreateInstance(provider, type, args ?? new object[] { })); } catch (Exception ex) { Trace.Fail(ex.Message); throw ex; } }; var dotNetifyConfig = new DotNetifyConfiguration(); dotNetifyConfig.SetFactoryMethod(factoryMethod); config?.Invoke(dotNetifyConfig); // If no view model assembly has been registered, default to the entry assembly. if (!dotNetifyConfig.HasAssembly) { dotNetifyConfig.RegisterEntryAssembly(); } // Sets how long to keep a view model controller in memory after it hasn't been accessed for a while. if (dotNetifyConfig.VMControllerCacheExpiration.HasValue) { vmControllerFactory.CacheExpiration = dotNetifyConfig.VMControllerCacheExpiration; } // Add middleware factories to the hub. var middlewareFactories = provider.GetService <IList <Func <IMiddleware> > >(); _middlewareTypes.ForEach(t => middlewareFactories?.Add(() => (IMiddleware)factoryMethod(t, null))); return(appBuilder); }
public static IAppBuilder UseDotNetify(this IAppBuilder appBuilder, Action <IDotNetifyConfiguration> config = null) { var dotNetifyConfig = new DotNetifyConfiguration(); config?.Invoke(dotNetifyConfig); // If no view model assembly has been registered, default to the entry assembly. if (!dotNetifyConfig.HasAssembly) { dotNetifyConfig.RegisterEntryAssembly(); } return(appBuilder); }
public static IApplicationBuilder UseDotNetify(this IApplicationBuilder appBuilder, Action <IDotNetifyConfiguration> config = null) { var provider = appBuilder.ApplicationServices; // Make sure all the required services are there. if (provider.GetService <IMemoryCache>() == null) { throw new InvalidOperationException("No service of type IMemoryCache has been registered. Please add the service by calling 'IServiceCollection.AddMemoryCache()' in the startup class."); } if (provider.GetService <IVMControllerFactory>() == null) { throw new InvalidOperationException("Please call 'IServiceCollection.AddDotNetify()' inside the ConfigureServices() of the startup class."); } // Use ASP.NET Core DI to provide view model instances by default. var dotNetifyConfig = new DotNetifyConfiguration(); dotNetifyConfig.SetFactoryMethod((type, args) => { try { return(ActivatorUtilities.CreateInstance(provider, type, args ?? new object[] { })); } catch (Exception ex) { Trace.Fail(ex.Message); throw ex; } }); config?.Invoke(dotNetifyConfig); // If no view model assembly has been registered, default to the entry assembly. if (!dotNetifyConfig.HasAssembly) { dotNetifyConfig.RegisterEntryAssembly(); } return(appBuilder); }