/// <summary> /// Gets the existing builder or creates an empty one /// </summary> /// <param name="ctx"></param> /// <returns></returns> public static ContainerBuilder GetContainerBuilder(this StartupContext ctx) => ctx.ContextData.GetValueOrCreate(BuilderKey, () => { var builder = new ContainerBuilder(); builder.RegisterInstance(ctx).As(ctx.GetType()).SingleInstance(); return(builder); });
/// <summary> /// Configure autofac /// </summary> /// <param name="ctx"></param> /// <param name="cfg"></param> public static void ConfigureContainer(this StartupContext ctx, Action <ContainerBuilder> cfg) { ctx.ContextData.Remove(Key);// delete the built and cached container var cb = GetContainerBuilder(ctx); cfg(cb); }
/// <summary> /// Gets the built container /// </summary> /// <param name="ctx"></param> /// <returns></returns> public static ILifetimeScope Container(this StartupContext ctx) => ctx.ContextData.GetValueOrCreate(Key, () => ctx.GetContainerBuilder().Build());