Skip to content

Instantly share code, notes, and snippets.

@alexeyzimarev
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeyzimarev/e41aa376774e68978ed7 to your computer and use it in GitHub Desktop.
Save alexeyzimarev/e41aa376774e68978ed7 to your computer and use it in GitHub Desktop.
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