C# 从MVC中的控制器调用信号器集线器

C# 从MVC中的控制器调用信号器集线器,c#,asp.net-mvc-4,signalr,C#,Asp.net Mvc 4,Signalr,在VisualStudio4.5框架中,我使用SignalR为中心集线器创建了控制台集线器应用程序 通过这个中心,我们可以将通知从web应用程序发送到桌面应用程序。我们可以从jquery调用中心,但我的任务是从asp.net MVC控件类连接控制台中心。 这是我的代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

在VisualStudio4.5框架中,我使用SignalR为中心集线器创建了控制台集线器应用程序

通过这个中心,我们可以将通知从web应用程序发送到桌面应用程序。我们可以从jquery调用中心,但我的任务是从asp.net MVC控件类连接控制台中心。 这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using AGPWebClient;
using AGPWebClient.Classes;
using AGPWebMVC.Models;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.ComponentModel;
using AGPWebMVC.Controllers;
namespace ConsoleAppHubClass
{
public class MyHub : Hub
{
    public void AddMessage(string name, string message)
    {
        Console.WriteLine("Hub AddMessage {0} {1}\n", name, message);
        Clients.All.addMessage(name, message);
    }

    public void Heartbeat()
    {
        Console.WriteLine("Hub Heartbeat\n");
        Clients.All.heartbeat();
    }

    //public void SendHelloObject(HelloModel hello)
    //{
    //    Console.WriteLine("Hub hello {0} {1}\n", hello.Molly, hello.Age);
    //    Clients.All.sendHelloObject(hello);
    //}

    public override Task OnConnected()
    {
        Console.WriteLine("Hub OnConnected {0}\n", Context.ConnectionId);
        return (base.OnConnected());
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        Console.WriteLine("Hub OnDisconnected {0}\n", Context.ConnectionId);
        return (base.OnDisconnected(stopCalled));
    }

    public override Task OnReconnected()
    {
        Console.WriteLine("Hub OnReconnected {0}\n", Context.ConnectionId);
        return (base.OnDisconnected(false));
    }


    }
  }
并创建Startup.cs类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Cors;
using Owin;

namespace ConsoleAppHubClass
{
public class Startup
{
    public void Configuration(IAppBuilder app)
    {

        app.Map("/signalr/hubs", map =>
        {

            map.UseCors(CorsOptions.AllowAll);

            var hubConfiguration = new HubConfiguration
            {


            };

            hubConfiguration.EnableJavaScriptProxies= true;
            hubConfiguration.EnableJSONP = true;
            hubConfiguration.EnableDetailedErrors = true;
            map.RunSignalR(hubConfiguration);
        });
    }
}
}
然后我在asp.net MVC web应用程序中创建了一个新项目来集成上面的“myHub”。 在那个web应用程序中,我想在控制器类中调用“myHub”

有什么方法吗?

当然,您只需从MVC项目中使用,然后从控制器内部调用hub的方法,或多或少与使用proxyless方法从Javascript客户端调用hub的方法相同。比如:

//somewhere you setup your connection once, you decide where
//variables should probably be members of some class
var hubConnection = new HubConnection("http://your_end_point:port/");
var hubProxy = hubConnection.CreateHubProxy("MyHub");
await hubConnection.Start();
...
//somewhere else in your controller you use the proxy to do your calls
hubProxy.Invoke("AddMessage", "foo", "bar");
检查


那么,您的设计是您的最佳解决方案吗?不确定,没有足够的信息来决定。此外,您的MVC应用程序将与连接到控制台应用程序的任何其他JS页面一样被视为客户端,因此您可能需要区分它们,但同样,不确定您想要实现什么。

谢谢,它可以工作了,现在我可以从MVC的控制台呼叫中心。