将依赖注入ASP Boilerplate项目的核心模块

问题描述 投票:0回答:1

我有NotificationJob类,我的.Net Core应用程序具有与通知功能相关的所有功能。它有一些来自域服务的注入依赖项。我试图将类的INotificationJob接口注入项目的CoreModule时遇到问题。

我最初尝试将接口直接注入CoreModule但是失败了,所以我在同一个文件NotificationModule中创建了另一个模块,我注入了INotificationJob接口。然后我尝试使用CoreModule注释将其与[DependsOn(typeof(oasisCoreModule))]链接。

项目的核心模块

[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);
    }
}

当我运行解决方案时,我会看到一个错误,如图所示:enter image description here

是否还需要采取其他步骤来完成依赖注入流程?或者我描述的步骤有缺陷?

c# dependency-injection asp.net-core-2.0 hangfire aspnetboilerplate
1个回答
0
投票

我不确定你在尝试用“界面注入”做什么,但如果我理解你正在尝试做什么,你可以试试这个:

核心模块

[...]
public override void PostInitialize()
{
    var recurrentJobs = IocManager.Resolve<NotificationJob>();
    RecurringJob.RemoveIfExists("JobName");
    RecurringJob.AddOrUpdate("JobName", () => recurrentJobs.CheckTickets(), Cron.Minutely);
}

你的班

public class NotificationJob : ISingletonDependency
{

    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 CheckTickets()
    {
        //Do something
    }
}

这有帮助吗?

© www.soinside.com 2019 - 2024. All rights reserved.