mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-20 10:37:55 +08:00
转移.net core 3.1,为.NET 5做准备
This commit is contained in:
@@ -12,12 +12,21 @@
|
||||
// <summary>IOC扩展</summary>
|
||||
// ***********************************************************************
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Web.Http;
|
||||
using System.Web.Mvc;
|
||||
using System.Runtime.Loader;
|
||||
using Autofac;
|
||||
using Autofac.Integration.Mvc;
|
||||
using Autofac.Integration.WebApi;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Autofac.Extras.Quartz;
|
||||
using Infrastructure.Cache;
|
||||
using Infrastructure.Extensions.AutofacManager;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
using OpenAuth.App.Interface;
|
||||
using OpenAuth.App.SSO;
|
||||
using OpenAuth.Repository;
|
||||
using OpenAuth.Repository.Interface;
|
||||
using IContainer = Autofac.IContainer;
|
||||
@@ -27,54 +36,95 @@ namespace OpenAuth.App
|
||||
public static class AutofacExt
|
||||
{
|
||||
private static IContainer _container;
|
||||
|
||||
public static void InitAutofac()
|
||||
public static IContainer InitForTest(IServiceCollection services)
|
||||
{
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
|
||||
//注册数据库基础操作和工作单元
|
||||
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>)).PropertiesAutowired();
|
||||
builder.RegisterType(typeof(UnitWork)).As(typeof(IUnitWork)).PropertiesAutowired();
|
||||
services.AddScoped(typeof(IRepository<>), typeof(BaseRepository<>));
|
||||
services.AddScoped(typeof(IUnitWork), typeof(UnitWork));
|
||||
|
||||
//注入授权
|
||||
builder.RegisterType(typeof(LocalAuth)).As(typeof(IAuth));
|
||||
|
||||
//注册app层
|
||||
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();
|
||||
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
|
||||
|
||||
// 注册controller,使用属性注入
|
||||
builder.RegisterControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();
|
||||
//防止单元测试时已经注入
|
||||
if (services.All(u => u.ServiceType != typeof(ICacheContext)))
|
||||
{
|
||||
services.AddScoped(typeof(ICacheContext), typeof(CacheContext));
|
||||
}
|
||||
|
||||
//注册所有的ApiControllers
|
||||
builder.RegisterApiControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();
|
||||
if (services.All(u => u.ServiceType != typeof(IHttpContextAccessor)))
|
||||
{
|
||||
services.AddScoped(typeof(IHttpContextAccessor), typeof(HttpContextAccessor));
|
||||
}
|
||||
|
||||
InitDependency(builder);
|
||||
|
||||
builder.RegisterModule(new QuartzAutofacFactoryModule());
|
||||
|
||||
builder.RegisterModelBinders(Assembly.GetCallingAssembly());
|
||||
builder.RegisterModelBinderProvider();
|
||||
builder.Populate(services);
|
||||
|
||||
// OPTIONAL: Register web abstractions like HttpContextBase.
|
||||
//builder.RegisterModule<AutofacWebTypesModule>();
|
||||
|
||||
// OPTIONAL: Enable property injection in view pages.
|
||||
builder.RegisterSource(new ViewRegistrationSource());
|
||||
|
||||
// 注册所有的Attribute
|
||||
builder.RegisterFilterProvider();
|
||||
|
||||
// Set the dependency resolver to be Autofac.
|
||||
_container = builder.Build();
|
||||
return _container;
|
||||
|
||||
//Set the MVC DependencyResolver
|
||||
DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
|
||||
|
||||
//Set the WebApi DependencyResolver
|
||||
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)_container);
|
||||
}
|
||||
|
||||
|
||||
public static void InitAutofac(ContainerBuilder builder)
|
||||
{
|
||||
|
||||
//注册数据库基础操作和工作单元
|
||||
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>));
|
||||
builder.RegisterType(typeof(UnitWork)).As(typeof(IUnitWork));
|
||||
//注入授权
|
||||
builder.RegisterType(typeof(LocalAuth)).As(typeof(IAuth));
|
||||
|
||||
//注册app层
|
||||
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
|
||||
|
||||
builder.RegisterType(typeof(CacheContext)).As(typeof(ICacheContext));
|
||||
builder.RegisterType(typeof(HttpContextAccessor)).As(typeof(IHttpContextAccessor));
|
||||
|
||||
InitDependency(builder);
|
||||
|
||||
builder.RegisterModule(new QuartzAutofacFactoryModule());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 从容器中获取对象
|
||||
/// 注入所有继承了IDependency接口
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static T GetFromFac<T>()
|
||||
/// <param name="builder"></param>
|
||||
private static void InitDependency(ContainerBuilder builder)
|
||||
{
|
||||
return _container.Resolve<T>();
|
||||
// return (T)DependencyResolver.Current.GetService(typeof(T));
|
||||
Type baseType = typeof(IDependency);
|
||||
var compilationLibrary = DependencyContext.Default
|
||||
.CompileLibraries
|
||||
.Where(x => !x.Serviceable
|
||||
&& x.Type == "project")
|
||||
.ToList();
|
||||
var count1 = compilationLibrary.Count;
|
||||
List<Assembly> assemblyList = new List<Assembly>();
|
||||
|
||||
foreach (var _compilation in compilationLibrary)
|
||||
{
|
||||
try
|
||||
{
|
||||
assemblyList.Add(AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(_compilation.Name)));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(_compilation.Name + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
builder.RegisterAssemblyTypes(assemblyList.ToArray())
|
||||
.Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract)
|
||||
.AsSelf().AsImplementedInterfaces()
|
||||
.InstancePerLifetimeScope();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user