首页 > 解决方案 > Blazor:从服务发送到组件的事件为空

问题描述

我有由 Blazor 组件和 Worker Service 组成的程序。我正在使用 .NET CORE 3.1

我的代码基于Blazor 组件的手册

在我的工人服务方面,我有:

    public event Func<double, Task> Notify;
    public double Data{ get; set; }

    public async Task Update(double data)
    {
        if (Notify != null)
        {
            await Notify.Invoke(data).ConfigureAwait(false);
        }
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
             await Update(Data).ConfigureAwait(false);
             ...

             await Task.Delay(1000, stoppingToken).ConfigureAwait(false);
        }
    }

在我的 Blazor 组件中:

@using MyNamespace
@inject WorkerService MyService
@implements IDisposable

<div>@DataToDisplay</div>

@code {

    private double DataToDisplay{ get; set; }

    protected override void OnInitialized()
    {
        MyService.Notify += OnNotify;
    }

    public async Task OnNotify(double data)
    {
        await InvokeAsync(() =>
        {
            DataToDisplay= data;
            StateHasChanged();
        });
    }

    public void Dispose()
    {
        MyService.Notify -= OnNotify;
    }
}

调试后我通知,Notify 事件在 Blazor 组件中正确连接,在 OnInitialized() 方法中(我也尝试过带有 的版本OnInitializedAsync())但是,每次,当服务调用Update()方法时,在条件检查期间:if (Notify != null),Notify 为 null .

我找不到原因。感谢您的任何建议!

标签: events.net-coreservice-workerblazor

解决方案


public class InjectableService
    {
        public double Value { get; set; }
        public event Func<Task> Notify;
        public double Data { get; set; }

        public  async Task RefreshAsync(double value)
        {
            if (Notify is { })
            {
                Value = value;
                await Notify.Invoke();
            }
        }
public class MyService : BackgroundService
    {
        private readonly InjectableService _injectableService;

        public MyService(InjectableService injectableService)
        {
            _injectableService = injectableService;
        }

        public async Task Update(double value)
        {
            await _injectableService.RefreshAsync(value);
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                await Update(_injectableService.Value + 0.1).ConfigureAwait(false);
                await Task.Delay(1000).ConfigureAwait(false);
            }
        }
    }
@inject InjectableService MyService
@implements IDisposable

<p>@MyService.Value</p>

@code {
    protected override void OnInitialized()
    {
        MyService.Notify += OnNotify;
    }

    public async Task OnNotify()
    {
        await InvokeAsync(() =>
        {
            StateHasChanged();
        });
    }

    public void Dispose()
    {
        MyService.Notify -= OnNotify;
    }
}

在 Startup.cs 中:

services.AddHostedService<MyService>();
services.AddSingleton<InjectableService>();

推荐阅读