标签:依赖注入 .net core inter interface var pes set har 示例
使用 TopShelf+Autofac+AutoMapper+Quartz+NLog 完成现有项目定时调度任务
public static class AutoMapperExtensions
{
public static ContainerBuilder ConfigureAutoMapper(this ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.AsClosedTypesOf(typeof(ITypeConverter<,>))
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(AutoMapperExtensions).Assembly)
.AssignableTo<Profile>().As<Profile>();
builder.Register(c => {
var profiles = c.Resolve<IEnumerable<Profile>>();
var context = c.Resolve<IComponentContext>();
return new MapperConfiguration(x => {
foreach (var profile in profiles) x.AddProfile(profile);
x.ConstructServicesUsing(context.Resolve);
});
}).SingleInstance().AsSelf();
builder.Register(c => {
var context = c.Resolve<IComponentContext>();
var config = context.Resolve<MapperConfiguration>();
return config.CreateMapper();
}).As<IMapper>();
return builder;
}
}
public static class ServicesExtensions
{
public static ContainerBuilder ConfigureSelf(this ContainerBuilder builder)
{
var services = new ServiceCollection();
// register appsettings.json
services.Configure<User>("UserConfig", Settings.Instance.Configuration.GetSection("User"));
builder.Populate(services);
// register services repositories
builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(UserService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces();
return builder;
}
}
public static ContainerBuilder ConfigureQuartz(this ContainerBuilder builder)
{
// 1) Register IScheduler
builder.RegisterModule(new QuartzAutofacFactoryModule());
// 2) Register jobs
builder.RegisterModule(new QuartzAutofacJobsModule(typeof(MyJob1).Assembly));
return builder;
}
cd /d %~dp0
Sample.Topshelf.exe install
pause
cd /d %~dp0
Sample.Topshelf.exe uninstall
pause
标签:依赖注入 .net core inter interface var pes set har 示例
原文地址:https://www.cnblogs.com/gaotang/p/10850564.html