Skip to content

Instantly share code, notes, and snippets.

@TanmayDharmaraj
Created December 20, 2017 06:28
Show Gist options
  • Save TanmayDharmaraj/d31cc3acb97a00f9c0de225638831ace to your computer and use it in GitHub Desktop.
Save TanmayDharmaraj/d31cc3acb97a00f9c0de225638831ace to your computer and use it in GitHub Desktop.
Sample DI setup
var builder = new ContainerBuilder();
var config = GlobalConfiguration.Configuration;
//This provides HTTPContext
builder.RegisterModule(new AutofacWebTypesModule());
//..
//Other registrations
//..
builder.RegisterType<GlobalStore>()
.WithParameter(new TypedParameter(typeof(CorrelationContext), new CorrelationContextFactory().CreateContext(HttpContext.Current.Request)))
.As<IGlobalStore>().InstancePerRequest();
//..
//Other registrations
//..
IContainer container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment