SignalR 在添加自定义对话服务时引发空引用异常。

huangapple go评论65阅读模式
英文:

SignalR causing null reference exception while adding custom dialog services

问题

我们正在尝试使用SignalR添加自定义服务,但在调用自定义服务时会抛出空异常。如果我们在mainLayout.razor中指定组件标记为<BlazorApp3.DialogForms.Test/>,则项目无法运行。

示例链接: https://uploadnow.io/f/Y59l8d9

SignalR 在添加自定义对话服务时引发空引用异常。
SignalR 在添加自定义对话服务时引发空引用异常。

输入图像描述
输入图像描述

附带示例中是否缺少任何配置?

需要在没有任何异常的情况下运行具有自定义对话服务的SignalR示例。

英文:

We are trying to add custom services with SignalR, but it throws a null exception when we invoke the custom services. If we specify the component tag in mainLayout.razor like <BlazorApp3.DialogForms.Test/>, it is unable to run the project.

Sample: https://uploadnow.io/f/Y59l8d9

SignalR 在添加自定义对话服务时引发空引用异常。
SignalR 在添加自定义对话服务时引发空引用异常。

enter image description here
enter image description here

Are we missing any configuration in the attached sample?

Need to run the signalR with custom dialog service sample without any exception

答案1

得分: 1

将您的 DialogService 类更改如下,问题可能会得到解决。

using System;
using Microsoft.AspNetCore.Components;

namespace BlazorApp3.DialogForms
{
    public class DialogService
    {
        private event Action<DialogOptions> dialogInstance;

        public event Action<DialogOptions> DialogInstance
        {
            add => dialogInstance += value;
            remove => dialogInstance -= value;
        }
        public async Task Open(DialogOptions options)
        {
            // 调用 Test 以更新并显示具有选项的对话框
            this.dialogInstance?.Invoke(options);
        }
    }
}
英文:

Change your DialogService class like below and the issue could be fixed.

using System;
using Microsoft.AspNetCore.Components;

namespace BlazorApp3.DialogForms
{
    public class DialogService
    {
        private event Action&lt;DialogOptions&gt; dialogInstance;

        public event Action&lt;DialogOptions&gt; DialogInstance
        {
            add =&gt; dialogInstance += value;
            remove =&gt; dialogInstance -= value;
        }
        public async Task Open(DialogOptions options)
        {
            // Invoke Test to update and show the dialog with options
            this.dialogInstance?.Invoke(options);
        }
    }
}

huangapple
  • 本文由 发表于 2023年7月10日 15:13:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定