OpenAuth.Net/OpenAuth.App/AutofacExt.cs

130 lines
4.7 KiB
C#
Raw Normal View History

// ***********************************************************************
2015-10-26 21:58:12 +08:00
// Assembly : OpenAuth.Mvc
// Author : yubaolee
// Created : 10-26-2015
//
// Last Modified By : yubaolee
// Last Modified On : 10-26-2015
// ***********************************************************************
// <copyright file="AutofacExt.cs" company="www.cnblogs.com/yubaolee">
// Copyright (c) www.cnblogs.com/yubaolee. All rights reserved.
// </copyright>
2016-01-05 10:03:35 +08:00
// <summary>IOC扩展</summary>
2015-10-26 21:58:12 +08:00
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
2015-11-19 21:49:39 +08:00
using System.Reflection;
using System.Runtime.Loader;
2017-12-08 05:49:00 +08:00
using Autofac;
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;
2015-12-02 16:28:01 +08:00
using OpenAuth.Repository;
2017-11-29 20:49:14 +08:00
using OpenAuth.Repository.Interface;
2017-12-08 05:49:00 +08:00
using IContainer = Autofac.IContainer;
2015-10-26 21:58:12 +08:00
2017-12-08 05:49:00 +08:00
namespace OpenAuth.App
2015-10-26 21:58:12 +08:00
{
2016-09-22 15:38:43 +08:00
public static class AutofacExt
2015-10-26 21:58:12 +08:00
{
2016-09-22 15:38:43 +08:00
private static IContainer _container;
public static IContainer InitForTest(IServiceCollection services)
2015-10-26 21:58:12 +08:00
{
var builder = new ContainerBuilder();
//注册数据库基础操作和工作单元
services.AddScoped(typeof(IRepository<,>), typeof(BaseRepository<,>));
services.AddScoped(typeof(IUnitWork<>), typeof(UnitWork<>));
2015-10-26 21:58:12 +08:00
//注入授权
builder.RegisterType(typeof(LocalAuth)).As(typeof(IAuth));
2017-12-08 05:49:00 +08:00
//注册app层
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
2015-10-26 21:58:12 +08:00
//防止单元测试时已经注入
if (services.All(u => u.ServiceType != typeof(ICacheContext)))
{
services.AddScoped(typeof(ICacheContext), typeof(CacheContext));
}
2015-10-26 21:58:12 +08:00
if (services.All(u => u.ServiceType != typeof(IHttpContextAccessor)))
{
services.AddScoped(typeof(IHttpContextAccessor), typeof(HttpContextAccessor));
}
InitDependency(builder);
builder.RegisterModule(new QuartzAutofacFactoryModule());
2015-10-26 21:58:12 +08:00
builder.Populate(services);
2015-10-26 21:58:12 +08:00
2016-09-22 15:38:43 +08:00
_container = builder.Build();
return _container;
2017-12-08 05:49:00 +08:00
2015-10-26 21:58:12 +08:00
}
public static void InitAutofac(ContainerBuilder builder)
{
//注册数据库基础操作和工作单元
builder.RegisterGeneric(typeof(BaseRepository<,>)).As(typeof(IRepository<,>));
builder.RegisterGeneric(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>
/// <param name="builder"></param>
private static void InitDependency(ContainerBuilder builder)
{
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();
}
2015-10-26 21:58:12 +08:00
}
}