Last active
August 29, 2015 14:22
-
-
Save alexeyzimarev/e41aa376774e68978ed7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Reflection; | |
using Autofac; | |
using Autofac.Core.Lifetime; | |
using Funq; | |
using Serilog; | |
using ServiceStack.Api.Postman; | |
using ServiceStack.Api.Swagger; | |
using ServiceStack.Logging; | |
using ServiceStack.Logging.Serilog; | |
using ServiceStack.ServiceInterface.Cors; | |
using ServiceStack.ServiceInterface.Validation; | |
using ServiceStack.WebHost.Endpoints; | |
using ServiceStack.WebHost.Endpoints.Formats; | |
namespace Services.API | |
{ | |
public class AppHost : AppHostHttpListenerBase | |
{ | |
private readonly IContainer _container; | |
public static Assembly[] IncludedAssemblies { get; set; } | |
private ILifetimeScope _scope; | |
public AppHost(IContainer container) : base("API", IncludedAssemblies) | |
{ | |
_container = container; | |
} | |
public override void Configure(Container container) | |
{ | |
LogManager.LogFactory = new SerilogFactory(Log.Logger); | |
var adapter = new AutofacIocAdapter(_container); | |
container.Adapter = adapter; | |
Plugins.Add(new SwaggerFeature()); | |
Plugins.Add(new ValidationFeature()); | |
Plugins.Add(new PostmanFeature { LocalOnly = false }); | |
Plugins.Add(new CorsFeature()); | |
container.RegisterValidators(IncludedAssemblies); | |
HtmlFormat.HtmlTitleFormat = "Snapshot of <i>{0}</i> generated on <b>{1}</b>"; | |
RequestFilters.Add((req, resp, dto) => | |
{ | |
_scope = _container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag); | |
CurrentContext.Scope = _scope; | |
}); | |
ResponseFilters.Add((req, resp, dto) => | |
{ | |
_scope.Dispose(); | |
CurrentContext.Scope = null; | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment