Skip to content

Commit

Permalink
#3 Moving Core.Web project
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Gerbenis committed Sep 21, 2015
1 parent 59cf1a0 commit 4aa7318
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IModulesRegistration, DefaultWebModulesRegistration>();
services.AddSingleton<IWebModulesRegistration, DefaultWebModulesRegistration>();

services.AddSingleton<IControllerFactory, DefaultWebControllerFactory>();

services.AddSingleton<IEmbeddedResourcesProvider, DefaultEmbeddedResourcesProvider>();
services.AddSingleton<IHttpContextAccessor, DefaultHttpContextAccessor>();
services.AddSingleton<IControllerExtensions, DefaultControllerExtensions>();
services.AddSingleton<ICommandResolver, DefaultCommandResolver>();
services.AddSingleton<IPrincipalProvider, DefaultWebPrincipalProvider>();
services.AddSingleton<IAssemblyManager, DefaultWebAssemblyManager>();
}

private static void LoadWebConfiguration(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<DefaultWebConfigurationSection>(configuration);
var provider = services.BuildServiceProvider();
var config = provider.GetService<IOptions<DefaultWebConfigurationSection>>().Options;
if (config?.Database != null)
{
config.Database.ConnectionString = configuration[config.Database.ConnectionStringName];
}
services.AddInstance<IWebConfiguration>(config);
services.AddInstance<Core.Configuration.IConfiguration>(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<IEmbeddedResourcesProvider>()));
//}
//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<PrecompiledViewAssembly>();

//var provider = services.BuildServiceProvider();
//var moduleRegistration = provider.GetService<IModulesRegistration>();
//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<MvcViewOptions>(options =>
//{
// options.ViewEngines.Add(engine);
//});
}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -10,7 +13,12 @@ public class HttpRuntimeCacheService : ICacheService
/// <summary>
/// Current class logger.
/// </summary>
private static readonly ILog Logger = LogManager.GetCurrentClassLogger();
private readonly ILogger logger;

public HttpRuntimeCacheService(ILoggerFactory loggerFactory)
{
this.logger = loggerFactory.CreateLogger(typeof(HttpRuntimeCacheService).FullName);
}

/// <summary>
/// Sets object in cache with a specified key for specific time.
Expand Down Expand Up @@ -40,7 +48,7 @@ public T Get<T>(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;
}

Expand All @@ -56,7 +64,7 @@ public T Get<T>(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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public string ResolveActionUrl<TController>(System.Linq.Expressions.Expression<A
var current = GetCurrent();
if (current != null)
{
string url = new UrlHelper(current.).Action(action, controller, routeValuesFromExpression);
string url = new UrlHelper(null, null).Action(action, controller, routeValuesFromExpression);
if (fullUrl)
{
url = string.Concat(GetServerUrl(current.Request).TrimEnd('/'), url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace BetterModules.Core.Extensions
{
public static class BetterModulesApplicationBuilderExtensions
{
public static IApplicationBuilder UseBetterModules(this IApplicationBuilder builder)
public static IApplicationBuilder UseBetterModulesCore(this IApplicationBuilder builder)
{
RunDatabaseMigrations(builder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ namespace BetterModules.Core.Extensions
{
public static class BetterModulesServiceCollectionExtensions
{
public static IServiceCollection AddBetterModules(this IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddBetterModulesCore(this IServiceCollection services, IConfiguration configuration)
{
LoadConfiguration(services, configuration);
ConfigureDefaultServices(services);
services.LoadConfiguration(configuration);
services.ConfigureDefaultServices();

LoadAssemblies(services);
services.LoadAssemblies();

return services;
}

public static IServiceCollection AddBetterModules(this IServiceCollection services, IConfiguration configuration, Action<ILoggerFactory> configureLoggers)
public static IServiceCollection AddBetterModulesCore(this IServiceCollection services, IConfiguration configuration, Action<ILoggerFactory> configureLoggers)
{
var provider = services.BuildServiceProvider();
var loggerFactory = provider.GetService<ILoggerFactory>();

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<IModulesRegistration, DefaultModulesRegistration>();
services.AddSingleton<ISessionFactoryProvider, DefaultSessionFactoryProvider>();
Expand All @@ -54,7 +54,7 @@ private static void ConfigureDefaultServices(IServiceCollection services)
services.AddSingleton<IMigrationRunner, DefaultMigrationRunner>();
}

private static void LoadConfiguration(IServiceCollection services, IConfiguration configuration)
public static void LoadConfiguration(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<DefaultConfigurationSection>(configuration);
var provider = services.BuildServiceProvider();
Expand All @@ -66,7 +66,7 @@ private static void LoadConfiguration(IServiceCollection services, IConfiguratio
services.AddInstance<Configuration.IConfiguration>(config);
}

private static void LoadAssemblies(IServiceCollection services)
public static void LoadAssemblies(this IServiceCollection services)
{

var provider = services.BuildServiceProvider();
Expand Down
4 changes: 2 additions & 2 deletions vNext/src/BetterModules.Mvc6.Sandbox/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -78,7 +78,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
template: "{controller=Home}/{action=Index}/{id?}");
});

app.UseBetterModules();
app.UseBetterModulesCore();
}
}
}

0 comments on commit 4aa7318

Please sign in to comment.