简体   繁体   English

Signalr 从服务器向客户端发送消息 c# 执行请求时发生未处理的异常

[英]Signalr Sending messages from server to client c# An unhandled exception has occurred while executing the request

My error:我的错误:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'AgencyApi.Controllers.LicenseInfoController'. There should only be one applicable constructor.
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.TryFindMatchingConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable`1[]& parameterMap)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable`1[]& parameterMap)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactory(Type instanceType, Type[] argumentTypes)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.CreateActivator(ControllerActionDescriptor descriptor)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.CreateControllerFactory(ControllerActionDescriptor descriptor)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerCache.GetCachedResult(ControllerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerProvider.OnProvidersExecuting(ActionInvokerProviderContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionInvokerFactory.CreateInvoker(ActionContext actionContext)
   at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointFactory.<>c__DisplayClass7_0.<CreateRequestDelegate>b__0(HttpContext context)"

My SignalRhub.cs我的 SignalRhub.cs

using Microsoft.AspNetCore.SignalR;
using System;
using System.Threading.Tasks;

namespace AgencyApi
{
    public class SignalRHub:Hub
    {
        public async Task SendMessage(string user, string message)
            {
                await Clients.All.SendAsync("ReceiveMessage", user, message);
            }
        
    }
    
}

My LicenseInfoController.cs我的 LicenseInfoController.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using AgencyApi.Data.Entities;
using Microsoft.AspNetCore.Mvc;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using Microsoft.AspNetCore.SignalR;
using AgencyApi;

namespace AgencyApi.Controllers
{
    [Route("api/[controller]")]
    public class LicenseInfoController : BaseController
    {
        private readonly ILicenseInfoService _licenseService;

        [HttpPost("ConfirmLink")]
        public async Task<JObject> ConfirmLink(LicenseInfo value)
        {
            JObject res = new JObject();
            try
            {
                var menuService = (IMenuService)this.HttpContext.RequestServices.GetService(typeof(IMenuService));
                var model = await _licenseService.GetById(value.id);
                model.Status = value.Status;
                model.Reason = value.Reason;
                var result = await _licenseService.Update(model);
                res.Add("ok", true);
                res.Add("data", JObject.FromObject(result));
            }
            catch (Exception ex)
            {
                res.Add("error", ex.Message);
            }   
            SendToAll();
            return res;
        }
        private readonly IHubContext<SignalRHub> _hubContext;

        public LicenseInfoController(IHubContext<SignalRHub> hubContext)
        {
            _hubContext = hubContext;
        }

        public void SendToAll()
        {
            _hubContext.Clients.All.SendAsync("Send", "message");
        }
    }
}

I think you are copying some bad example.我认为你正在复制一些不好的例子。 I would suggest a different approach我会建议一种不同的方法

Make you SignalRinterface as a service, not a derivative:让您将 SignalRinterface 作为服务,而不是衍生产品:

public class SignalRHub
{
    public Task SendMessage(string user, string message) =>
        _hubContext.Clients.All.SendAsync("ReceiveMessage", user, message);

    public Task SendToAll(string message) =>
        _hubContext.Clients.All.SendAsync("Send", message);

    private readonly IHubContext<Hub> _hubContext;

    public SignalRHub(IHubContext<Hub> hubContext) =>
        _hubContext = hubContext;
}

Register in Startup在启动中注册

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();

    services.AddSignalR();
    services.AddSingleton<SignalRHub>();
}

Then add to the controller:然后添加到 controller:

public class LicenseInfoController : Controller
{
    [HttpPost]
    public async Task<JObject> ConfirmLink(string value)
    {
        JObject res = new JObject();
        await _signalRHub.SendToAll("message");
        return res;
    }

    private readonly SignalRHub _signalRHub;

    public LicenseInfoController(SignalRHub signalRHub)
    {
        _signalRHub = signalRHub;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 执行请求时发生未处理的异常 - An unhandled exception has occurred while executing the request C# Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常 - C# Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request 从服务器发送消息到客户端c# - Sending messages from server to client c# 使用SignalR从服务器向客户端发送消息 - Sending Messages from the Server to the Client with SignalR Mongodb C#驱动程序向服务器发送消息时发生异常 - Mongodb C# driver An exception occurred while sending a message to the server 在OpenIddict中处理请求时发生未处理的异常 - An unhandled exception occurred while processing the request in OpenIddict C# HttpClient.SendAsync 在测试某些 URL 时抛出“发送请求时发生错误”异常 - C# HttpClient.SendAsync throw "An error occurred while sending the request" exception when testing some URLs C# Signalr - 发送请求时出错 - C# Signalr - An error occured while sending the request C# TMDbClient HttpRequestException: 发送请求时发生错误 - C# TMDbClient HttpRequestException: An error occurred while sending the request 尝试从C#代码执行存储过程时,mscorlib.dll中发生了类型为&#39;System.StackOverflowException&#39;的未处理异常 - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll while trying to execute stored procedure from c# code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM