Simple encapsulation and reconstruction of Autofac Container, autofaccontainer
For ease of use, the simple encapsulation of Autofac container is recorded as follows, which will be used or shared with you later. Welcome to discuss it!
1 using Autofac; 2 using Autofac. core. lifetime; 3 using Autofac. integration. mvc; 4 5 public static class ContainerManager 6 {7 private static IContainer _ container; 8 9 public static void SetContainer (IContainer container) 10 {11 _ container = container; 12} 13 14 public static IContainer Container 15 {16 get {return _ container;} 17} 18 19 public static T Resolve <T> (string key = "", ILifetime Scope scope = null) where T: class 20 {21 if (scope = null) 22 {23 // no scope specified 24 scope = Scope (); 25} 26 if (string. isNullOrEmpty (key) 27 {28 return scope. resolve <T> (); 29} 30 return scope. resolveKeyed <T> (key); 31} 32 33 public static object Resolve (Type type, ilifetimescript scope = null) 34 {35 if (scope = null) 36 {37 // no scope specified 38 scope = Scope (); 39} 40 return Scope. resolve (type); 41} 42 43 public static T [] ResolveAll <T> (string key = "", ilifetimes1_scope = null) 44 {45 if (scope = null) 46 {47 // no scope specified 48 scope = Scope (); 49} 50 if (string. isNullOrEmpty (key) 51 {52 return scope. resolve <IEnumerable <T> (). toArray (); 53} 54 return scope. resolveKeyed <IEnumerable <T> (key ). toArray (); 55} 56 57 public static T ResolveUnregistered <T> (I Lifetimescript scope = null) where T: class 58 {59 return ResolveUnregistered (typeof (T), scope) as T; 60} 61 62 public static object ResolveUnregistered (Type type Type, ilifetimescript scope = null) 63 {64 if (scope = null) 65 {66 // no scope specified 67 scope = Scope (); 68} 69 var constructors = type. getConstructors (); 70 foreach (var constructor in constructors) 71 {72 try 73 {74 var parameter S = constructor. getParameters (); 75 var parameterInstances = new List <object> (); 76 foreach (var parameter in parameters) 77 {78 var service = Resolve (parameter. parameterType, scope); 79 if (service = null) throw new ArgumentException ("Unkown dependency"); 80 parameterInstances. add (service); 81} 82 return Activator. createInstance (type, parameterInstances. toArray (); 83} 84 catch (ArgumentExcep Tion) 85 {86 87} 88} 89 throw new ArgumentException ("no suitable constructor found in all dependencies. "); 90} 91 92 public static bool TryResolve (Type serviceType, ILifetimeScope scope, out object instance) 93 {94 if (scope = null) 95 {96 // no scope specified 97 scope = Scope (); 98} 99 return scope. tryResolve (serviceType, out instance); 100} 101 102 public static bool IsRegistered (Type serviceType, ilifetimes1_scope = null) 103 {104 if (scope = null) 105 {106 // no scope specified107 scope = S Round (); 108} 109 return scope. isRegistered (serviceType); 110} 111 112 public static object ResolveOptional (Type serviceType, ilifetimes1_scope = null) 113 {114 if (scope = null) 115 {116 // no scope specified117 scope = Scope (); 118} 119 return scope. resolveOptional (serviceType); 120} 121 122 public static ilifetimescript Scope () 123 {124 try125 {126 if (HttpContext. current! = Null) 127 return AutofacDependencyResolver. current. requestlifetimescript; 128 129 // when such lifetime scope is returned, you shoshould be sure that it'll be disposed once used (e.g. in schedule tasks) 130 return Container. beginlifetimesags (MatchingScopeLifetimeTags. requestLifetimeScopeTag); 131} 132 catch (Exception) 133 {134 // we can get an exception here if RequestLifetimeScope is already disposed135 // for example, requested in or after "Application_EndRequest" handler136 // but note that usually it shold never happen1_138 // when such lifetime scope is returned, you shoshould be sure that it'll be disposed once used (e.g. in schedule tasks) 139 return Container. beginlifetimesags (MatchingScopeLifetimeTags. requestLifetimeScopeTag); 140} 141} 142}
The usage is as follows:
1 /// <summary> 2 /// IOCConfig 3 /// </summary> 4 public class IOCConfig 5 {6 /// <summary> 7 // RegisterAll 8/ // </summary> 9 public static void RegisterAll () 10 {11 var iocBuilder = new Autofac. containerBuilder (); 12 13 // todo: reference the repository project in the web project to compile to the web directory and facilitate publishing. Do not directly use the 14 iocBuilder in the code. registerModule <RIS. infrastructure. registerModule> (); 15 iocBuilder. registerModule <RIS. biz. registerModule> (); 16 iocBuilder. registerModule <RIS. repository. registerModule> (); 17 18 iocBuilder. register (c, p) => new WebWorkContext (p. named <string> ("userToken") 19. as <RIS. biz. core. IWorkContext> () 20. instanceperlifetimescript (); 21 22 23 iocBuilder. registerControllers (Assembly. getExecutingAssembly (); 24 25 iocBuilder. registerApiControllers (Assembly. getExecutingAssembly (); 26 27 // iocBuilder. registerModelBinde Rs (Assembly. getExecutingAssembly (); 28 // iocBuilder. registerModelBinderProvider (); 29 // iocBuilder. registerModule <AutofacWebTypesModule> (); 30 var config = GlobalConfiguration. configuration; 31 iocBuilder. registerWebApiFilterProvider (config); 32 IContainer iocContainer = iocBuilder. build (); 33 config. dependencyResolver = new AutofacWebApiDependencyResolver (iocContainer); 34 System. web. mvc. dependencyReso Lver. setResolver (new AutofacDependencyResolver (iocContainer); 35 36 37 RIS. biz. core. containerManager. setContainer (iocContainer); 38} 39} 40 41 // Note: Instructions on enabling IIS6 or IIS7 Classic mode, 42 // access the http://go.microsoft.com /? LinkId = 939480143 // <summary> 44 // MvcApplication45 // </summary> 46 public class MvcApplication: System. web. httpApplication47 {48 /// <summary> 49 // program startup entry 50 /// </summary> 51 protected void Application_Start () 52 {53 IOCConfig. registerAll (); 54 55 AreaRegistration. registerAllAreas (); 56 57 WebApiConfig. register (GlobalConfiguration. configuration); 58 FilterConfig. registerGlobalFilters (GlobalFilters. filters); 59 RouteConfig. registerRoutes (RouteTable. routes); 60 BundleConfig. registerBundles (BundleTable. bundles); 61} 62}