Reputation: 123
I made Console Hub Application using SignalR for a central hub in visual studio 4.5 framework.
By this Hub we can send notification from web application to desktop application. we can call hub from jquery, but my task is to connect console hub from asp.net MVC Contoller class. so here is my code
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));
}
}
}
and also Create Startup.cs class
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);
});
}
}
}
and then i made new project in asp.net MVC web app to integrate above "myHub". in that web app i want to invoke "myHub" inside Controller class.
is there any way ??
Upvotes: 2
Views: 4993
Reputation: 3425
Sure, you just use the SignalR .NET Client library from your MVC project, and from inside your controller you invoke the hub's methods more or less the same way you would from a Javascript client with the proxyless approach. Something like:
//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");
Check here.
Then, is your design the best solution in your case? Not sure, not enough info to decide it. Also, your MVC app will count as a client like any other JS page connected to your console app, so you might need to distinguish them, but again, not sure what you are trying to achieve.
Upvotes: 4