【发布时间】:2018-08-09 04:50:46
【问题描述】:
我有 NotificationJob 类,我拥有与 .Net Core 应用程序的通知功能相关的所有功能。它有一些来自域服务的注入依赖项。我在尝试将类的INotificationJob 接口注入项目的CoreModule 时遇到问题。
我最初尝试将接口直接注入CoreModule,但失败了,所以我在同一个文件中创建了另一个模块,称为NotificationModule,我在其中注入INotificationJob 接口。然后我尝试使用[DependsOn(typeof(oasisCoreModule))] 注释将它与CoreModule 链接起来。
项目的核心模块
[DependsOn(
typeof(AbpZeroCoreModule),
typeof(AbpHangfireAspNetCoreModule),
typeof(AbpWebCommonModule)
)]
public class oasisCoreModule : AbpModule
{
public override void PreInitialize()
{
Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
Configuration.BackgroundJobs.UseHangfire();
Configuration.Auditing.IsEnabledForAnonymousUsers = true;
// Declare entity types
Configuration.Modules.Zero().EntityTypes.Tenant = typeof(Tenant);
Configuration.Modules.Zero().EntityTypes.Role = typeof(Role);
Configuration.Modules.Zero().EntityTypes.User = typeof(User);
oasisLocalizationConfigurer.Configure(Configuration.Localization);
// Enable this line to create a multi-tenant application.
Configuration.MultiTenancy.IsEnabled = oasisConsts.MultiTenancyEnabled;
// Configure roles
AppRoleConfig.Configure(Configuration.Modules.Zero().RoleManagement);
Configuration.Settings.Providers.Add<AppSettingProvider>();
}
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(typeof(oasisCoreModule).GetAssembly());
}
public override void PostInitialize()
{
IocManager.Resolve<AppTimes>().StartupTime = Clock.Now;
}
}
// This is the custom module that I created in the same file as the core module.
[DependsOn(typeof(oasisCoreModule))]
public class NotificationModule : AbpModule
{
INotificationJob _job;
public NotificationModule(INotificationJob job)
{
_job = job;
}
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
public override void PostInitialize()
{
_job.Loop();
}
}
INotificationJob 接口我正在注入到NotificationModule
public interface INotificationJob: IDomainService
{
void Loop();
void CheckTickets();
void CheckReminders(string email, string ticket);
}
INotificationJob接口的类实现
public class NotificationJob: DomainService, INotificationJob
{
private readonly ITicketRefManager _ticketRefManager;
private readonly IClientManager _clientManager;
private readonly IEmailManager _emailManager;
public NotificationJob(
ITicketRefManager ticketRefManager,
IClientManager clientManager,
IEmailManager emailManager,
)
{
_ticketRefManager = ticketRefManager;
_clientManager = clientManager;
_emailManager = emailManager;
}
public void Loop()
{
RecurringJob.AddOrUpdate(() => CheckTickets(), Cron.Minutely);
}
}
我还需要采取其他步骤来完成依赖注入过程吗?还是我描述的步骤有缺陷?
【问题讨论】:
-
我认为您必须从源“BoilerPlate”更新项目..
-
您是否尝试在应用层注入 INotificationJob 服务?我看到你有 NotificationModule 依赖于 oasisCoreModule。要注入服务,您可能应该声明 oasisCoreModule 依赖于 NotificationModule
标签: c# dependency-injection asp.net-core-2.0 hangfire aspnetboilerplate