[英]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.