Blazor服务器端:订阅事件

时间:2020-10-30 12:15:53

标签: c# asp.net-core events blazor

我很难理解如何在Blazor页面中订阅活动。

我有一个实现Quartz的.NET IJobListener接口的类:

public class GlobalJobListener : IJobListener
    {
        public event TaskExecution Started;
        public event TaskExecution Vetoed;
        public event TaskExecutionComplete Executed;

        public GlobalJobListener(string name)
        {
            Name = name;
        }
        public GlobalJobListener()
        {
           
        }

        public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
        {
            var task = new Task(() => Started?.Invoke());
            task.Start();
            task.Wait();
            return task;
        }

        public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
        {
            var task = new Task(() => Vetoed?.Invoke());
            task.Start();
            task.Wait();
            return task;
        }

        public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
        {
            var task = new Task(() => Executed?.Invoke(jobException));
            task.Start();
            task.Wait();
            return task;
        }

        public string Name { get; }
    }

ConfigureServices中:

services.AddScoped<GlobalJobListener>();

然后在我的.razor页面中:

[Inject]
 protected GlobalJobListener listener { get; set; }

还有OnInitializedAsync()

 protected override async Task OnInitializedAsync()
        {
            scheduler = quartz.Scheduler;
            if (scheduler != null)
            {                
                listener.Started += BeforeStart;
                listener.Executed += AfterEndAsync;
            }
        }
  private void BeforeStart()
        {           
            Log.Information("\t" + "Started: " + DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss tt"));
        }

并且BeforeStart()不会触发,但是JobToBeExecuted()会触发OK。我看不到自己在做什么错。

非常感谢。

0 个答案:

没有答案