G.Thirunavukkarasu G.Thirunavukkarasu - 15 days ago 5
C# Question

How do i config the WCF Service Running URL as SignalR url?

I am Developing a Real Time Application.I am Created a ASP.NET MVC Client Application and WCF Service.and Again i need to add the signalR concept in WCF and Client side for real time notification.

For this i am created an self hosted signalR and added this solution to WCF Service Solution in visual Studio.

My WCF Service Running URL Address is :http://localhost:63694/Service1.svc

My Requirement is WCF and SignalR work together.and the CLient is ASp.Net MVC4

The WCF and ASP.NET MVC codes are here

SignalR code:-

namespace SelfHostingSignalR
{
class Program
{

static void Main(string[] args)
{
try
{
string url = "http://localhost:63694/Signalr";

using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}

class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();

}
}
public class MyHub : Hub
{
public void Send(string user)
{
Clients.All.addMessage(user);

}
}
}


For signalr url i am hardcoded some value with wcf url address:

string url = "http://localhost:63694/Signalr";

i don't know is it correct or not.Could anyone provide me a solution to solve this?

how do i configure the WCF Service Running Url to signalR url?

Answer

To self-host SignalR on a WCF web service application, you just need to configure /signalr in Startup.cs:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}

with MyHub defined like this:

public class MyHub : Hub
{
    public void Send(string user)
    {
        Clients.All.addMessage(user);
    }
}

in your client MVC .NET application, you can invoke server method and define client method as below:

    [HttpGet]
    public async Task<ActionResult> SignalR()
    {
        dynamic model = new ExpandoObject();
        using(var hubConnection = new HubConnection("http://localhost:63694"))
        {
            var myHub = hubConnection.CreateHubProxy("MyHub");
            myHub.On<string>("addMessage", (user) =>
            {
                model.User = user;
            });
            await hubConnection.Start();
            await myHub.Invoke("Send", "an user");
        }
        return View(model);
    }