栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.Net6获取Autofac注入的服务

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.Net6获取Autofac注入的服务

Program.cs 配置:

using Autofac;
using Autofac.Extensions.DependencyInjection;
using System.Reflection;
using WebApplication3;

var builder = WebApplication.CreateBuilder(args);

// 1.将默认ServiceProviderFactory指定为AutofacServiceProviderFactory
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer(builder =>
{
    // 2.自动注册
    Assembly assembly = Assembly.Load("Jasper.Application.Base");
    builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces().InstancePerDependency();
});


// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// 3.获取Autofac注入的实例
IocManager.Instance.Container = app.UseHostFiltering().ApplicationServices.GetAutofacRoot();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();

Autofac帮助类:

using Autofac;
using Autofac.Core;

namespace WebApplication3
{
    /// 
    /// Autofac帮助类
    /// 
    public class IocManager
    {
        public static IocManager Instance = new IocManager();

        public ILifetimeScope Container { get; set; }

        public T GetService()
        {
            return ResolutionExtensions.Resolve((IComponentContext)(object)this.Container);
        }

        public T GetService(string serviceKey)
        {
            return ResolutionExtensions.ResolveKeyed((IComponentContext)(object)this.Container, (object)serviceKey);
        }

        public T GetService(string serviceKey, params Parameter[] parameters)
        {
            return ResolutionExtensions.ResolveKeyed((IComponentContext)(object)this.Container, (object)serviceKey, parameters);
        }

        public object GetService(Type serviceType)
        {
            return ResolutionExtensions.Resolve((IComponentContext)(object)this.Container, serviceType);
        }

        public object GetService(string serviceKey, Type serviceType)
        {
            return ResolutionExtensions.ResolveKeyed((IComponentContext)(object)this.Container, (object)serviceKey, serviceType);
        }

        public bool IsRegistered()
        {
            return ResolutionExtensions.IsRegistered((IComponentContext)(object)this.Container);
        }

        public bool IsRegistered(string serviceKey)
        {
            return ResolutionExtensions.IsRegisteredWithKey((IComponentContext)(object)this.Container, (object)serviceKey);
        }

        public bool IsRegistered(Type serviceType)
        {
            return ResolutionExtensions.IsRegistered((IComponentContext)(object)this.Container, serviceType);
        }

        public bool IsRegisteredWithKey(string serviceKey, Type serviceType)
        {
            return ResolutionExtensions.IsRegisteredWithKey((IComponentContext)(object)this.Container, (object)serviceKey, serviceType);
        }
    }
}

使用:

var testIoc = IocManager.Instance.GetService();

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/908290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号