1

I have Web Api which serves to CRUD Posts from Web App, Android App and Desktop.

I want to add SignalR to the Web Api, every time when Action Create in Controller gets called I want to notify all users that Post is created.

Problem is, I can't find any implementation in only Web Api, all implementations are in Web App with Web Api or something like that. I read all MSDN documentation about it. I'm strugling for 3-4 days now.

I managed to get to the point where I implemented SignalR, and my server isn't created any signalr/hubs file that I need to call from Web App script. It's only created when I run app locally - if I publish it on Azure that file isn't created.

Anyone have concrete steps for Implementation only in Web Api?

I tried this blog, it has Web Api stuff but have js in project and local html added. It's not standalone REST api.

This is not about not creating signalr/hubs file. It's about creating standalone Web Api with SignalR.

I have startup:

public void Configuration(IAppBuilder app) {
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888

            app.MapSignalR("/signalr", new Microsoft.AspNet.SignalR.HubConfiguration());
        }

My Hub:

public class ServiceStatusHub : Hub {
        private static IHubContext hubContext =
            GlobalHost.ConnectionManager.GetHubContext<ServiceStatusHub>();

        public static void SetStatus(string message) {
            hubContext.Clients.All.acknowledgeMessage(message);
        }

    }

And in my Api Controler I call:

ServiceStatusHub.SetStatus("Status changed!");

I made console application to test Api, added Signal R client and class:

class SignalRMasterClient {

        public string Url { get; set; }
        public HubConnection Connection { get; set; }
        public IHubProxy Hub { get; set; }

        public SignalRMasterClient(string url) {
            Url = url;
            Connection = new HubConnection(url, useDefaultUrl: false);
            Hub = Connection.CreateHubProxy("ServiceStatusHub");
            Connection.Start().Wait();

            Hub.On<string>("acknowledgeMessage", (message) =>
            {
                Console.WriteLine("Message received: " + message);

                /// TODO: Check status of the LDAP
                /// and update status to Web API.
            });
        }

        public void SayHello(string message) {
            Hub.Invoke("hello", message);
            Console.WriteLine("hello method is called!");
        }

        public void Stop() {
            Connection.Stop();
        }

    }

program.cs:

 var client = new SignalRMasterClient("myUrl.com/signalr");

            // Send message to server.
            client.SayHello("Message from client to Server!");

I getting 500 Internal Server Error.

How can I test is my Web Api signalR works for sure?

Boris Dabetic
  • 113
  • 2
  • 10

1 Answers1

0

I see some problems:

1) You do not need the hubContext field in your hub. You inherit from Hub. This class contains allready a "Clients" property .

public class ServiceStatusHub : Hub {          
        public static void SetStatus(string message) {
            Clients.All.acknowledgeMessage(message);
        }
    }

2) Log errors at starting of server.

public SignalRMasterClient(string url) {
            Url = url;
            Connection = new HubConnection(url, useDefaultUrl: false);
            Hub = Connection.CreateHubProxy("ServiceStatusHub");
            Connection.Start().ContinueWith(task => { if (task.IsFaulted) {
                    Console.WriteLine("There was an error opening the connection:{0}",
                                      task.Exception.GetBaseException());
                } else {
                    Console.WriteLine("Connected");
                }
            });

            Hub.On<string>("acknowledgeMessage", (message) =>
            {
                Console.WriteLine("Message received: " + message);

                /// TODO: Check status of the LDAP
                /// and update status to Web API.
            }).Wait();
        }

3) Check Client url. You need no signalr there in your path at creation of your client. just:

var client = new SignalRMasterClient("myUrl.com/");

Here you will find a running sample which does all you need: SignalR Console app example

Stephu
  • 3,236
  • 4
  • 21
  • 33