如何处理 .NET Core 控制台应用程序中注入 类 之间的事件调用

How to handle event invocation between injected classes in .NET Core Console App

我有一个 .Net Core(3.1) 控制台应用程序,它有 2 个服务 类,一个有一个事件,另一个使用该事件的处理程序侦听它。我已设置获取 DI 容器,但事件字段始终为空,因此无法调用其 Invoke()。关于在涉及事件处理的 ConfigureServices() 中设置服务时我缺少什么的任何指示。下面是完整的测试代码:

public class RefreshEventArgs : EventArgs
{
    public string RefreshEventData { get; set; }
}
public interface INotifierService
{
    event EventHandler<RefreshEventArgs> RefreshEventHandler;
}

public class NotifierService : INotifierService
{
    public event EventHandler<RefreshEventArgs> RefreshEventHandler;
    public RefreshEventArgs RefreshEventData { get; set; }

    // GeneralAppSettings is a POCO class to read all appsettings.json key values.
    private readonly IOptions<GeneralAppSettings> myAppSettings;
    public NotifierService(IOptions<GeneralAppSettings> appSettings)
    {
        myAppSettings = appSettings;
    }
    
    public void RunInvokingRefreshEvent()
    {
        RefreshEventData = new RefreshEventArgs();
        RefreshEventData.RefreshEventData = "somedata";
        
        // Main problem! In the below line, RefreshEventHandler is null all the time           
        RefreshEventHandler?.Invoke(this, RefreshEventData);            
    }
    
    public void SomeBackgroundThreadMonitorsExternalEvents()
    {
        // Some external events triggers below method
        RunInvokingRefreshEvent();
    }       
}

刷新服务

public interface IRefreshService
{
    void Refresh(RefreshEventArgs eventData = null);
}
public class RefresherService : IRefreshService
{
    private readonly IOptions<GeneralAppSettings> myAppSettings;
    private readonly INotifierService notify;

    public RefresherService(IOptions<GeneralAppSettings> _appSettings, INotifierService _notifyService)
    {
        myAppSettings = _appSettings;
        notify = _notifyService;
        notify.RefreshEventHandler += _notify_RefreshEventHandler;
    }

    private void _notify_RefreshEventHandler(object sender, RefreshEventArgs e)
    {
        // Call Refresh() based say based on a config value from myAppSettings
        Refresh(e);
    }
    public void Refresh(RefreshEventArgs eventData = null)
    {
        // final business logic processing based on eventData
    }
}
public class GeneralAppSettings // POCO
{
    public string SomeConfigKeyInAppSettingsJson { get; set; }      
}  

计划

class Program
{
    public static IConfiguration Configuration { get; set; }
    static void Main(string[] args)
    {
        // read appsettings
        var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
           .AddEnvironmentVariables();
        Configuration = builder.Build();

        // Host builder, setting up container 
        var host = Host.CreateDefaultBuilder()
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddConfiguration(Configuration);
            })
            .ConfigureServices((context, services) =>
            {
                services.Configure<GeneralAppSettings>(Configuration.GetSection("GeneralAppSettings"));
                services.AddSingleton<INotifierService, NotifierService>();
                services.AddSingleton<IRefreshService, RefresherService>();
            })
            .Build();
        
        // Need to get NotifierService instance to run some initial logic, so using ActivatorUtilities
        var svc = ActivatorUtilities.GetServiceOrCreateInstance<NotifierService>(host.Services);
        svc.SomeBackgroundThreadMonitorsExternalEvents();      

        // Need to get RefresherService instance to have initial Refresh logic so using ActivatorUtilities
        var refresh = ActivatorUtilities.GetServiceOrCreateInstance<RefresherService>(host.Services);
        refresh.Refresh(null);
        
        // need to keep this main thread alive 
        Thread.Sleep(Timeout.Infinite);
    }
}

当您从 DI 容器中请求某些东西时,您必须 请求“服务”类型(接口或 first/only 通用参数)。如果您请求一个尚未注册的类型并使用 ActivatorUtilities,当且仅当构造它所需的所有类型都可用时,它才会创建一个实例。你正在发生的事情是你得到了两个不同的对象(一个注册为接口,一个伪注册为具体类型)!您的 class 实现接口并在注册中将其用作“实现”类型并不重要。 DI 是 always 基于服务类型并且你没有注册任何类型的服务 NotifierService directly.

你的问题是你的 classes 和你想在 NotifierService 上调用的方法实际上不是接口的一部分。通常的技巧是只注册并请求具体类型作为服务类型:

services.AddSingleton<NotiferService>();
//...
var notifier = services.GetService<NotifierService>();

那行得通,只是现在您还没有注册 INotifierService 以注入 RefresherService

别担心,我们有变通办法。将具体类型注册为单例,然后使用工厂注册接口:

// register the concrete type directly 
services.AddSingleton<NotifierService>();
// use a factory to register the interface 
services.AddSingleton<INotifierService>(sp => sp.GetRequiredService<NotifierService>());

现在无论是请求接口还是具体类型,都会返回同一个实例。您也不再需要使用 ActivatorUtilities(事实上您 不应该 )——您现在可以直接使用主机的服务:

var notifier = host.Services.GetRequiredService<NotifierService>();
notifier.SomeBackgroundThreadMonitorsExternalEvents();

综上所述,您的项目是 IHostedService/BackgroundService 的完美人选。您可以稍微重组它(将 NotifierService 分成两个 classes:一个只包含事件,另一个用于后台服务)这样您就只需要处理接口 您实际上可以调用 Host.Run() ,后者将依次等待关闭。这是此类事情的标准模式,而不是仅仅为了 DI 容器滥用主机并包括奇怪的 Thread.Sleep.