Skip to content

Instantly share code, notes, and snippets.

@droyad
Created July 28, 2015 06:00
Show Gist options
  • Select an option

  • Save droyad/578e39aa3f2d25f24643 to your computer and use it in GitHub Desktop.

Select an option

Save droyad/578e39aa3f2d25f24643 to your computer and use it in GitHub Desktop.
AutofacNancyBootstrapper
public class NancyBootstrapper : AutofacNancyBootstrapper
{
private readonly ILifetimeScope _container;
public NancyBootstrapper(ILifetimeScope container)
{
_container = container;
StaticConfiguration.DisableErrorTraces = _container.Resolve<EnvironmentNameSetting>().IsProduction;
ApplicationPipelines.OnError.AddItemToStartOfPipeline((context, ex) =>
{
Log.Error(ex, "Exception occurred processing request {url}", context.Request.Url);
return null;
});
}
protected override void ApplicationStartup(ILifetimeScope container, IPipelines pipelines)
{
Log.Debug("Website Started");
base.ApplicationStartup(container, pipelines);
}
protected override void RequestStartup(ILifetimeScope container, IPipelines pipelines, NancyContext context)
{
pipelines.AfterRequest.AddItemToEndOfPipeline((c, ct) => CommitUnitOfWork(container, c));
base.RequestStartup(container, pipelines, context);
}
private static async Task CommitUnitOfWork(ILifetimeScope container, NancyContext context)
{
if (context.Request.Method != "GET" && context.Response.StatusCode == HttpStatusCode.OK)
await container.Resolve<IUnitOfWork>().Commit();
}
protected override ILifetimeScope GetApplicationContainer()
{
return _container;
}
protected override void ConfigureConventions(NancyConventions conventions)
{
base.ConfigureConventions(conventions);
conventions.StaticContentsConventions.AddDirectory("build", null, new[] { ".js", ".ts", ".map", ".css", ".html" });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment