/// <summary> Ioc注册 </summary> protected override void IocRegisters() { Builder = Builder ?? new ContainerBuilder(); //注入程序集查找器 var finder = new DefaultAssemblyFinder(); Builder.RegisterInstance(finder).As <IAssemblyFinder>().SingleInstance(); var assemblies = finder.FindAll().ToArray(); Builder.RegisterAssemblyTypes(assemblies) .Where(type => typeof(IScopedDependency).IsAssignableFrom(type) && !type.IsAbstract) .AsSelf() //自身服务,用于没有接口的类 .AsImplementedInterfaces() //接口服务 .PropertiesAutowired() //属性注入 .InstancePerLifetimeScope(); //保证生命周期基于请求 Builder.RegisterAssemblyTypes(assemblies) .Where(type => typeof(IDependency).IsAssignableFrom(type) && !type.IsAbstract) .AsSelf() //自身服务,用于没有接口的类 .AsImplementedInterfaces() //接口服务 .PropertiesAutowired(); //属性注入 Builder.RegisterAssemblyTypes(assemblies) .Where(type => typeof(ISingleDependency).IsAssignableFrom(type) && !type.IsAbstract) .AsSelf() //自身服务,用于没有接口的类 .AsImplementedInterfaces() //接口服务 .PropertiesAutowired() //属性注入 .SingleInstance(); //保证单例注入 IocManager = new IocManager(this); CurrentIocManager.SetIocManager(IocManager); Builder.RegisterInstance(IocManager).AsSelf().As <IIocManager>().SingleInstance(); }
/// <summary> 开启微服务 </summary> /// <param name="provider"></param> /// <param name="addressAction"></param> /// <returns></returns> public static void UseMicroService(this IServiceProvider provider, Action <ServiceAddress> addressAction = null) { if (CurrentIocManager.IocManager == null) { CurrentIocManager.SetIocManager(new DefaultIocManager(provider)); } var address = SpearConfig.GetConfig().Service; addressAction?.Invoke(address); var host = provider.GetService <IMicroHost>(); Task.Factory.StartNew(async() => await host.Start(address)); }