From 4aa7318dfacc2a505f313145d2e6ec789fa201e3 Mon Sep 17 00:00:00 2001 From: Lukas Gerbenis Date: Mon, 21 Sep 2015 15:48:40 +0300 Subject: [PATCH] #3 Moving Core.Web project --- .../Host/DefaultWebApplicationAutoHost.cs | 22 ++-- ...erModulesWebServiceCollectionExtensions.cs | 112 ++++++++++++++++++ .../Caching/HttpRuntimeCacheService.cs | 14 ++- .../Web/DefaultHttpContextAccessor.cs | 2 +- ...tterModulesApplicationBuilderExtensions.cs | 2 +- ...etterModulesServiceCollectionExtensions.cs | 18 +-- .../src/BetterModules.Mvc6.Sandbox/Startup.cs | 4 +- 7 files changed, 149 insertions(+), 25 deletions(-) create mode 100644 vNext/src/BetterModules.Core.Web/Extensions/BetterModulesWebServiceCollectionExtensions.cs diff --git a/vNext/src/BetterModules.Core.Web/Environment/Host/DefaultWebApplicationAutoHost.cs b/vNext/src/BetterModules.Core.Web/Environment/Host/DefaultWebApplicationAutoHost.cs index cf4e590..a95297f 100644 --- a/vNext/src/BetterModules.Core.Web/Environment/Host/DefaultWebApplicationAutoHost.cs +++ b/vNext/src/BetterModules.Core.Web/Environment/Host/DefaultWebApplicationAutoHost.cs @@ -4,17 +4,16 @@ using System.Threading; using BetterModules.Core.Exceptions; using BetterModules.Core.Web.Exceptions.Host; +using Microsoft.Framework.Logging; namespace BetterModules.Core.Web.Environment.Host { public abstract class DefaultWebApplicationAutoHost : IWebApplicationAutoHost { - private static readonly ILog _logger = LogManager.GetCurrentClassLogger(); + private readonly ILogger logger; private static object _lock = new object(); - protected ILog Logger { get { return _logger; } } - private string _hostName = "Web application host"; public string HostName @@ -53,6 +52,11 @@ protected int InitializedCount private HttpApplication _application; + protected DefaultWebApplicationAutoHost(ILoggerFactory loggerFactory) + { + this.logger = loggerFactory.CreateLogger(typeof(DefaultWebApplicationAutoHost).FullName); + } + protected HttpApplication Application { get @@ -113,23 +117,23 @@ public virtual void OnApplicationStart(HttpApplication application, bool validat { try { - Logger.InfoFormat("{0} starting...", HostName); + logger.LogInformation("{0} starting...", HostName); if (validateViewEngines && !ViewEngines.Engines.Any(engine => engine is CompositePrecompiledMvcEngine)) { throw new CoreException("ViewEngines.Engines collection doesn't contain precompiled composite MVC view engine. Application modules use precompiled MVC views for rendering. Please check if Engines list is not cleared manualy in global.asax.cx"); } - Logger.InfoFormat("{0} started.", HostName); + logger.LogInformation("{0} started.", HostName); } catch (Exception ex) { - Logger.Fatal("Failed to start host application.", ex); + logger.LogCritical("Failed to start host application.", ex); } } public virtual void OnApplicationEnd(HttpApplication application) { - Logger.InfoFormat("{0} stopped.", HostName); + logger.LogInformation("{0} stopped.", HostName); } public virtual void OnApplicationError(HttpApplication application) @@ -209,7 +213,7 @@ private bool TryTouchBinRestartMarker() } catch (Exception ex) { - Logger.Warn("Failed to touch web host application \bin folder.", ex); + logger.LogWarning("Failed to touch web host application \bin folder.", ex); return false; } } @@ -237,7 +241,7 @@ private bool TryTouchWebConfig() } catch (Exception ex) { - Logger.Warn("Failed to touch web host application web.config file.", ex); + logger.LogWarning("Failed to touch web host application web.config file.", ex); return false; } } diff --git a/vNext/src/BetterModules.Core.Web/Extensions/BetterModulesWebServiceCollectionExtensions.cs b/vNext/src/BetterModules.Core.Web/Extensions/BetterModulesWebServiceCollectionExtensions.cs new file mode 100644 index 0000000..ec8588a --- /dev/null +++ b/vNext/src/BetterModules.Core.Web/Extensions/BetterModulesWebServiceCollectionExtensions.cs @@ -0,0 +1,112 @@ +using System.Collections.Generic; +using System.Linq; +using BetterModules.Core.Environment.Assemblies; +using BetterModules.Core.Exceptions; +using BetterModules.Core.Extensions; +using BetterModules.Core.Modules.Registration; +using BetterModules.Core.Security; +using BetterModules.Core.Web.Configuration; +using BetterModules.Core.Web.Environment.Assemblies; +using BetterModules.Core.Web.Modules; +using BetterModules.Core.Web.Modules.Registration; +using BetterModules.Core.Web.Mvc; +using BetterModules.Core.Web.Mvc.Commands; +using BetterModules.Core.Web.Mvc.Extensions; +using BetterModules.Core.Web.Security; +using BetterModules.Core.Web.Web; +using BetterModules.Core.Web.Web.EmbeddedResources; +using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.Framework.Configuration; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.OptionsModel; + +namespace BetterModules.Core.Web.Extensions +{ + public static class BetterModulesWebServiceCollectionExtensions + { + public static IServiceCollection AddBetterModules(this IServiceCollection services, IConfiguration configuration) + { + services.LoadWebConfiguration(configuration); + services.ConfigureDefaultWebServices(); + services.LoadWebAssemblies(); + + return services; + } + + private static void ConfigureDefaultWebServices(this IServiceCollection services) + { + services.ConfigureDefaultServices(); + + services.AddSingleton(); + services.AddSingleton(); + + services.AddSingleton(); + + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + } + + private static void LoadWebConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.Configure(configuration); + var provider = services.BuildServiceProvider(); + var config = provider.GetService>().Options; + if (config?.Database != null) + { + config.Database.ConnectionString = configuration[config.Database.ConnectionStringName]; + } + services.AddInstance(config); + services.AddInstance(config); + } + + private static void LoadWebAssemblies(this IServiceCollection services) + { + services.LoadAssemblies(); + + //TODO: find out how to register EmbeddedResourcesVirtualPathProvider + //if (HostingEnvironment.IsHosted) + //{ + // HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedResourcesVirtualPathProvider(container.Resolve())); + //} + //else + //{ + // if (!IsTestMode) + // { + // throw new CoreException("Failed to register EmbeddedResourcesVirtualPathProvider as a virtual path provider."); + // } + //} + + //TODO: find out how to add precompiled views for assemblies + // Register precompiled views for all the assemblies + //var precompiledAssemblies = new List(); + + //var provider = services.BuildServiceProvider(); + //var moduleRegistration = provider.GetService(); + //moduleRegistration.GetModules().Select(m => m.ModuleDescriptor).Distinct().ToList().ForEach( + // descriptor => + // { + // var webDescriptor = descriptor as WebModuleDescriptor; + // if (webDescriptor != null) + // { + // var precompiledAssembly = new PrecompiledViewAssembly(descriptor.GetType().Assembly, + // $"~/Areas/{webDescriptor.AreaName}/") + // { + // UsePhysicalViewsIfNewer = false + // }; + // precompiledAssemblies.Add(precompiledAssembly); + // } + // }); + + //var engine = new CompositePrecompiledMvcEngine(precompiledAssemblies.ToArray()); + //services.Configure(options => + //{ + // options.ViewEngines.Add(engine); + //}); + } + } +} \ No newline at end of file diff --git a/vNext/src/BetterModules.Core.Web/Services/Caching/HttpRuntimeCacheService.cs b/vNext/src/BetterModules.Core.Web/Services/Caching/HttpRuntimeCacheService.cs index 8c1958c..d9f9c1b 100644 --- a/vNext/src/BetterModules.Core.Web/Services/Caching/HttpRuntimeCacheService.cs +++ b/vNext/src/BetterModules.Core.Web/Services/Caching/HttpRuntimeCacheService.cs @@ -1,4 +1,7 @@ using System; +using FluentNHibernate.Conventions.Helpers; +using Microsoft.Framework.Caching.Memory; +using Microsoft.Framework.Logging; namespace BetterModules.Core.Web.Services.Caching { @@ -10,7 +13,12 @@ public class HttpRuntimeCacheService : ICacheService /// /// Current class logger. /// - private static readonly ILog Logger = LogManager.GetCurrentClassLogger(); + private readonly ILogger logger; + + public HttpRuntimeCacheService(ILoggerFactory loggerFactory) + { + this.logger = loggerFactory.CreateLogger(typeof(HttpRuntimeCacheService).FullName); + } /// /// Sets object in cache with a specified key for specific time. @@ -40,7 +48,7 @@ public T Get(string key) } catch (Exception ex) { - Logger.WarnFormat("Failed to retrieve cache item {0}.", ex, key); + logger.LogWarning("Failed to retrieve cache item {0}.", ex, key); obj = null; } @@ -56,7 +64,7 @@ public T Get(string key) } catch (Exception ex) { - Logger.WarnFormat("Failed to convert cache item {0} of type {1} to type {2}.", ex, key, obj.GetType().FullName, typeof(T).FullName); + logger.LogWarning("Failed to convert cache item {0} of type {1} to type {2}.", ex, key, obj.GetType().FullName, typeof(T).FullName); converted = default(T); } diff --git a/vNext/src/BetterModules.Core.Web/Web/DefaultHttpContextAccessor.cs b/vNext/src/BetterModules.Core.Web/Web/DefaultHttpContextAccessor.cs index 340568b..56be016 100644 --- a/vNext/src/BetterModules.Core.Web/Web/DefaultHttpContextAccessor.cs +++ b/vNext/src/BetterModules.Core.Web/Web/DefaultHttpContextAccessor.cs @@ -80,7 +80,7 @@ public string ResolveActionUrl(System.Linq.Expressions.Expression configureLoggers) + public static IServiceCollection AddBetterModulesCore(this IServiceCollection services, IConfiguration configuration, Action configureLoggers) { var provider = services.BuildServiceProvider(); var loggerFactory = provider.GetService(); configureLoggers(loggerFactory); - return services.AddBetterModules(configuration); + return services.AddBetterModulesCore(configuration); } - private static void ConfigureDefaultServices(IServiceCollection services) + public static void ConfigureDefaultServices(this IServiceCollection services) { services.AddSingleton(); services.AddSingleton(); @@ -54,7 +54,7 @@ private static void ConfigureDefaultServices(IServiceCollection services) services.AddSingleton(); } - private static void LoadConfiguration(IServiceCollection services, IConfiguration configuration) + public static void LoadConfiguration(this IServiceCollection services, IConfiguration configuration) { services.Configure(configuration); var provider = services.BuildServiceProvider(); @@ -66,7 +66,7 @@ private static void LoadConfiguration(IServiceCollection services, IConfiguratio services.AddInstance(config); } - private static void LoadAssemblies(IServiceCollection services) + public static void LoadAssemblies(this IServiceCollection services) { var provider = services.BuildServiceProvider(); diff --git a/vNext/src/BetterModules.Mvc6.Sandbox/Startup.cs b/vNext/src/BetterModules.Mvc6.Sandbox/Startup.cs index 8985cd3..a09b8b5 100644 --- a/vNext/src/BetterModules.Mvc6.Sandbox/Startup.cs +++ b/vNext/src/BetterModules.Mvc6.Sandbox/Startup.cs @@ -41,7 +41,7 @@ public void ConfigureServices(IServiceCollection services) services.AddMvc(); services.AddOptions(); - services.AddBetterModules(Configuration, loggerFactory => + services.AddBetterModulesCore(Configuration, loggerFactory => { loggerFactory.MinimumLevel = LogLevel.Verbose; loggerFactory.AddEventLog(LogLevel.Verbose); @@ -78,7 +78,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) template: "{controller=Home}/{action=Index}/{id?}"); }); - app.UseBetterModules(); + app.UseBetterModulesCore(); } } }