// *********************************************************************** // <summary>IOC扩展</summary> // *********************************************************************** using Autofac; using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using IContainer = Autofac.IContainer; namespace Hh.Mes.Service { public static class AutofacExt { private static IContainer _container; public static IContainer InitAutofac(IServiceCollection services) { var builder = new ContainerBuilder(); //缓存注入 services.AddScoped(typeof(IHttpContextAccessor), typeof(HttpContextAccessor)); builder.Populate(services); _container = builder.Build(); return _container; } /// <summary> /// 从容器中获取对象 /// </summary> /// <typeparam name="T"></typeparam> public static T GetFromFac<T>() { return _container.Resolve<T>(); } } }