Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.net core 3.0 preview 5+ autofac support #78

Closed
ghost opened this issue Sep 17, 2019 · 4 comments
Closed

.net core 3.0 preview 5+ autofac support #78

ghost opened this issue Sep 17, 2019 · 4 comments

Comments

@ghost
Copy link

ghost commented Sep 17, 2019

老何,
麻烦更新下Util支持下preview版3.0
现在不能用返回IServiceProvider方法,官方建议在program.cs加入

builder.UseServiceProviderFactory(new AutofacServiceProviderFactory(
                x =>
                {
                  
                }));

然后startup.cs
加入

  public void ConfigureContainer(ContainerBuilder builder)
        {
       
        }

麻烦融入下,谢谢!

@dancky
Copy link

dancky commented Sep 18, 2019

是的.我也发现这个问题.一开始直接在StartUp的ConfigureServices里面加services.AddUtil();没加return
Controller里面就直接解析不到 IService了

@ghost
Copy link
Author

ghost commented Sep 18, 2019

是的.我也发现这个问题.一开始直接在StartUp的ConfigureServices里面加services.AddUtil();没加return
Controller里面就直接解析不到 IService了

新版不能用return IServiceProvider了,他是自动build的

@dancky
Copy link

dancky commented Sep 18, 2019

AutofacServiceProviderFactory加了可以用吗?
我最终解决办法是修改Util.Dependency.Bootstrapper里面的注入

    /// <summary>
    /// 注册作用域依赖
    /// </summary>
    private void RegisterScopeDependency()
    {
        _services.BatchRegisterService(GetTypes<IScopeDependency>(), ServiceLifetime.Scoped);
        _builder.RegisterTypes( GetTypes<IScopeDependency>() ).AsImplementedInterfaces().PropertiesAutowired().InstancePerLifetimeScope();
    }

    /// <summary>
    /// 注册瞬态依赖
    /// </summary>
    private void RegisterTransientDependency()
    {
        _services.BatchRegisterService(GetTypes<ITransientDependency>(), ServiceLifetime.Transient);
        _builder.RegisterTypes( GetTypes<ITransientDependency>() ).AsImplementedInterfaces().PropertiesAutowired().InstancePerDependency();
    }

    /// <summary>
    /// 解析依赖注册器
    /// </summary>
    private void ResolveDependencyRegistrar() {
        var types = GetTypes<IDependencyRegistrar>();
        types.Select( type => Reflection.CreateInstance<IDependencyRegistrar>( type ) ).ToList().ForEach( t => t.Register( _services ) );
    }


public static class ServiceCollectionExtenion
{
    /// <summary>
    /// 批量注册服务
    /// </summary>
    /// <param name="services">DI服务</param>
    /// <param name="typeList">需要批量注册的类型集合</param>
    /// <param name="serviceLifetime">服务生命周期</param>
    /// <returns></returns>
    public static IServiceCollection BatchRegisterService(this IServiceCollection services, Type[] typeList, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
    {
        var typeDic = new Dictionary<Type, Type[]>(); //待注册集合
        foreach (var type in typeList)
        {
            var interfaces = type.GetInterfaces();   //获取接口
            typeDic.Add(type, interfaces);
        }
        if (typeDic.Keys.Count() > 0)
        {
            foreach (var instanceType in typeDic.Keys)
            {
                foreach (var interfaceType in typeDic[instanceType])
                {
                    //根据指定的生命周期进行注册
                    switch (serviceLifetime)
                    {
                        case ServiceLifetime.Scoped:
                            services.AddScoped(interfaceType, instanceType);
                            break;
                        case ServiceLifetime.Singleton:
                            services.AddSingleton(interfaceType, instanceType);
                            break;
                        case ServiceLifetime.Transient:
                            services.AddTransient(interfaceType, instanceType);
                            break;
                    }
                }
            }
        }
        return services;
    }
}

@UtilCore
Copy link
Contributor

等.net core 3.0正式版发了就升上去

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants