C# 信号器仅在第一次调用时工作

C# 信号器仅在第一次调用时工作,c#,asp.net-core-mvc,signalr,C#,Asp.net Core Mvc,Signalr,我有一个ASP.NET核心MVC5网站。我使用信号器从模型层向客户机/视图发送“通知” 如果我打开我的索引页面,它会使用SignalR发送一个可用摄像头的列表。然后我再次使用信号器发送相机在不同型号上拍摄的图像。然而,只有第一种方法有效 如果我导航到https://localhost:44303/camera/live/?IP=192.168.50.212它开始发送图像,但不会发现其他摄像头。如果我先导航到发现,发现就可以正常工作 在这两种模型中,行Hub.Clients.All.sendsyn

我有一个ASP.NET核心MVC5网站。我使用信号器从模型层向客户机/视图发送“通知”

如果我打开我的索引页面,它会使用SignalR发送一个可用摄像头的列表。然后我再次使用信号器发送相机在不同型号上拍摄的图像。然而,只有第一种方法有效

如果我导航到
https://localhost:44303/camera/live/?IP=192.168.50.212
它开始发送图像,但不会发现其他摄像头。如果我先导航到发现,发现就可以正常工作

在这两种模型中,行
Hub.Clients.All.sendsync(“方法”,数据)正在执行。在这两种模型中,中心都定义为

public Microsoft.AspNetCore.SignalR.IHubContext<MasterHub> Hub { get; internal set; }`
public Microsoft.AspNetCore.signal.IHubContext中心{get;内部集;}`
每个模型都有一个单独的控制器,用于设置集线器上下文,如下所示:

private readonly IHubContext<MasterHub> _hubContext;

public CameraController(IHubContext<MasterHub> hubContext)
{
    _hubContext = hubContext;
}
private只读IHubContext\u hubContext;
公共摄像机控制器(IHubContext hubContext)
{
_hubContext=hubContext;
}
然而,只有第一个我不愿意工作的

我是否必须在发送消息后关闭信号器连接才能再次使用它?如果是的话,我会怎么做

两个控制器看起来都像:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Thermal_Screening.Hubs;
using Thermal_Screening.Models;


namespace Thermal_Screening.Controllers
{
    public class CameraController : Controller
    {
        public string CameraName;
        private readonly IHubContext<MasterHub> _hubContext;

        public CameraController(IHubContext<MasterHub> hubContext)
        {
            _hubContext = hubContext;
        }

        public IActionResult Live(string IP)
        {
            CameraName = getCameraNameFromIP(IP); // doin it this way causes a 2s delay, should get ip in model

            return View(new CameraViewModel(IP) { Hub = _hubContext, IP = IP, CameraName = CameraName });
        }

        public IActionResult Settings(string IP)
        {
            CameraName = getCameraNameFromIP(IP);

            return View(new CameraViewModel(IP) { Hub = _hubContext, IP = IP, CameraName = CameraName });

        }

        public IActionResult Log(string IP)
        {
            CameraName = getCameraNameFromIP(IP);

            return View(new CameraViewModel(IP) { Hub = _hubContext, IP = IP, CameraName = CameraName });

        }

        private string getCameraNameFromIP(string IP)
        {
            WebClient x = new WebClient();
            string source = x.DownloadString("http://" + IP);
            return Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
        }
    }
}

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Extensions;
using Flir.Atlas.Live.Discovery;
using Flir.Atlas.Live.Device;
using Flir.Atlas.Live;
using System.IO;
using Flir.Atlas.Image;
using System.Drawing;
using Microsoft.AspNetCore.SignalR;
using Thermal_Screening.Hubs;

namespace Thermal_Screening.Models
{
    public class CameraViewModel
    {


        public Microsoft.AspNetCore.SignalR.IHubContext<MasterHub> Hub { get; internal set; }

//removed for berevity

Hub.Clients.All.SendAsync("method", "data");
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.signal;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Text.RegularExpressions;
使用System.Threading.Tasks;
使用热屏蔽。轮毂;
使用热屏蔽模型;
名称空间热屏蔽控制器
{
公共类CameraController:控制器
{
公共字符串CameraName;
私有只读IHubContext\u hubContext;
公共摄像机控制器(IHubContext hubContext)
{
_hubContext=hubContext;
}
公共IActionResult Live(字符串IP)
{
CameraName=getCameraNameFromIP(IP);//这样做会导致2秒延迟,应该在模型中获得IP
返回视图(新的CameraViewModel(IP){Hub=\U hubContext,IP=IP,CameraName=CameraName});
}
公共IActionResult设置(字符串IP)
{
CameraName=getCameraNameFromIP(IP);
返回视图(新的CameraViewModel(IP){Hub=\U hubContext,IP=IP,CameraName=CameraName});
}
公共IActionResult日志(字符串IP)
{
CameraName=getCameraNameFromIP(IP);
返回视图(新的CameraViewModel(IP){Hub=\U hubContext,IP=IP,CameraName=CameraName});
}
私有字符串getCameraNameFromIP(字符串IP)
{
WebClient x=新的WebClient();
string source=x.DownloadString(“http://”+IP);
返回Regex.Match(source,@“\]*\>\s*(?[\s\s]*?)\”,RegexOptions.IgnoreCase).Groups[“Title”].Value;
}
}
}
两种视图模型看起来都像:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Thermal_Screening.Hubs;
using Thermal_Screening.Models;


namespace Thermal_Screening.Controllers
{
    public class CameraController : Controller
    {
        public string CameraName;
        private readonly IHubContext<MasterHub> _hubContext;

        public CameraController(IHubContext<MasterHub> hubContext)
        {
            _hubContext = hubContext;
        }

        public IActionResult Live(string IP)
        {
            CameraName = getCameraNameFromIP(IP); // doin it this way causes a 2s delay, should get ip in model

            return View(new CameraViewModel(IP) { Hub = _hubContext, IP = IP, CameraName = CameraName });
        }

        public IActionResult Settings(string IP)
        {
            CameraName = getCameraNameFromIP(IP);

            return View(new CameraViewModel(IP) { Hub = _hubContext, IP = IP, CameraName = CameraName });

        }

        public IActionResult Log(string IP)
        {
            CameraName = getCameraNameFromIP(IP);

            return View(new CameraViewModel(IP) { Hub = _hubContext, IP = IP, CameraName = CameraName });

        }

        private string getCameraNameFromIP(string IP)
        {
            WebClient x = new WebClient();
            string source = x.DownloadString("http://" + IP);
            return Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
        }
    }
}

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Extensions;
using Flir.Atlas.Live.Discovery;
using Flir.Atlas.Live.Device;
using Flir.Atlas.Live;
using System.IO;
using Flir.Atlas.Image;
using System.Drawing;
using Microsoft.AspNetCore.SignalR;
using Thermal_Screening.Hubs;

namespace Thermal_Screening.Models
{
    public class CameraViewModel
    {


        public Microsoft.AspNetCore.SignalR.IHubContext<MasterHub> Hub { get; internal set; }

//removed for berevity

Hub.Clients.All.SendAsync("method", "data");
使用Microsoft.AspNetCore.Http;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Text.RegularExpressions;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Http.Extensions;
使用Flir.Atlas.Live.Discovery;
使用Flir.Atlas.Live.Device;
使用Flir.Atlas.Live;
使用System.IO;
使用Flir.Atlas.Image;
使用系统图;
使用Microsoft.AspNetCore.signal;
使用热屏蔽。轮毂;
名称空间热屏蔽模型
{
公共类CameraViewModel
{
public Microsoft.AspNetCore.signal.IHubContext中心{get;内部集;}
//因丧礼而被免职
Hub.Clients.All.SendAsync(“方法”、“数据”);

请显示足够的代码来重现问题,而不仅仅是snippets@stuartd我已经编辑过了。