Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Blazor服务器端:订阅事件_C#_Asp.net Core_Events_Blazor - Fatal编程技术网

C# Blazor服务器端:订阅事件

C# Blazor服务器端:订阅事件,c#,asp.net-core,events,blazor,C#,Asp.net Core,Events,Blazor,我很难理解如何在Blazor页面中订阅活动 我有一个实现Quartz的.NETIJobListener接口的类: public class GlobalJobListener : IJobListener { public event TaskExecution Started; public event TaskExecution Vetoed; public event TaskExecutionComplete Executed;

我很难理解如何在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; }
    }
配置服务中

services.AddScoped<GlobalJobListener>();
OnInitializedAsync()

BeforeStart()
从不触发,但
JobToBeExecuted()
触发OK。我看不出我做错了什么


非常感谢。

这是什么样的“工作”真正开始了?在异步启动方法中连接事件,那么可能是在链接实际发生之前触发的事件吗?请尝试使用“private Task BeforeStart()”而不是“private void BeforeStart()”,并使方法返回Task.CompletedTask
[Inject]
 protected GlobalJobListener listener { get; set; }
 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"));
        }