using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using Autofac; using Autofac.Integration.Mvc; namespace EMIS.Utility { public interface IAutofacContainerProvider { IContainer Container { get; set; } } public class AutofacHelper { private static IContainer _Container; public static IContainer Container { get { if (_Container == null) { if (CreateContainerFun != null) { _Container = CreateContainerFun(); } else { _Container = (HttpContext.Current.ApplicationInstance as IAutofacContainerProvider).Container; } } return _Container; } set { _Container = value; } } public static Func CreateContainerFun { get; set; } public static ILifetimeScope RequestLifetimeScope { get { return Container.BeginLifetimeScope(System.Web.HttpContext.Current != null ? System.Web.HttpContext.Current.Timestamp : DateTime.Now); } } } }