【问题标题】:Inject Dependency into Core Module of a ASP Boilerplate project将依赖注入到 ASP 样板项目的核心模块中
【发布时间】: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


【解决方案1】:

我不确定你想用“接口注入”做什么,但如果我正确理解你想要做什么,你可以试试这个:

核心模块

[...]
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
    }
}

有帮助吗?

【讨论】:

  • 感谢您的建议!它让我通过了阶段,现在我看到了不同的错误消息:Could not load file or assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
猜你喜欢
  • 2019-06-01
  • 1970-01-01
  • 2018-12-24
  • 2015-11-07
  • 1970-01-01
  • 2018-12-14
  • 2023-03-12
  • 1970-01-01
  • 2018-01-03
相关资源
最近更新 更多