2024-09-02 09:31:34 +08:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Bunny.Common.Context;
|
|
|
|
|
using Bunny.Service.IService;
|
|
|
|
|
using Bunny.Service.IService.Service;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace Bunny.WebApi.Configuration;
|
|
|
|
|
|
|
|
|
|
public static class AddAutofacConfig
|
|
|
|
|
{
|
|
|
|
|
public static void BuildContainer(ContainerBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
// 异步方式创建数据库
|
|
|
|
|
new EfCoreContext().Database.EnsureCreatedAsync();
|
|
|
|
|
|
|
|
|
|
// 如果不在在 Controller 层写构造函数可以打开这个,自动完成注入
|
|
|
|
|
var controllerBaseType = typeof(ControllerBase);
|
|
|
|
|
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
|
|
|
|
|
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
|
|
|
|
|
.PropertiesAutowired(); //支持属性注入
|
2024-09-02 14:37:42 +08:00
|
|
|
|
|
2024-09-02 09:31:34 +08:00
|
|
|
|
// 注入EfCore上下文对象
|
|
|
|
|
builder.RegisterType<EfCoreContext>();
|
|
|
|
|
|
|
|
|
|
// 注入Service服务
|
|
|
|
|
builder.RegisterType<BaseService>().As<IBaseService>();
|
|
|
|
|
builder.RegisterType<BlogService>().As<IBlogService>();
|
|
|
|
|
builder.RegisterType<RedisOptionService>().As<IRedisOptionService>();
|
|
|
|
|
builder.RegisterType<JobService>().As<IJobService>();
|
|
|
|
|
builder.RegisterType<MinioService>().As<IMinioService>();
|
|
|
|
|
}
|
|
|
|
|
}
|