AutofacHelper.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using Autofac;
  7. using Autofac.Integration.Mvc;
  8. namespace EMISOnline.Utility
  9. {
  10. public interface IAutofacContainerProvider
  11. {
  12. IContainer Container { get; set; }
  13. }
  14. public class AutofacHelper
  15. {
  16. private static IContainer _Container;
  17. public static IContainer Container
  18. {
  19. get
  20. {
  21. if (_Container == null)
  22. {
  23. if (CreateContainerFun != null)
  24. {
  25. _Container = CreateContainerFun();
  26. }
  27. else
  28. {
  29. _Container = (HttpContext.Current.ApplicationInstance as IAutofacContainerProvider).Container;
  30. }
  31. }
  32. return _Container;
  33. }
  34. set
  35. {
  36. _Container = value;
  37. }
  38. }
  39. public static Func<IContainer> CreateContainerFun { get; set; }
  40. public static ILifetimeScope RequestLifetimeScope
  41. {
  42. get
  43. {
  44. return
  45. Container.BeginLifetimeScope(System.Web.HttpContext.Current != null
  46. ? System.Web.HttpContext.Current.Timestamp
  47. : DateTime.Now);
  48. }
  49. }
  50. }
  51. }