0

I started to work with SignalR on Visual Studio 2012, so far I got the basic hang of it, I guided myself through this example (You can browse the code in the page). I decided I wanted to add a REST service to said example, so I added a basic service to it and it worked.

The next step I wanted to take was to add a communication between the service and SignalR, so according to what the example showed to me, I only needed to create a HubConnection through the url in my project (in this case, the example uses the url http:localhost:4200). You can check the WorkerRoleHubConfiguration class, it has a method that has the next line:

return RoleEnvironment.GetConfigurationSettingValue("GUI_URL");

Where GUI_URL is http:localhost:4200.

In my service class then I just added a method with the following:

var url = RoleEnvironment.GetConfigurationSettingValue("http://localhost:4200");

try
{
    HubConnection _connection = new HubConnection(url);
    IHubProxy _hub = _connection.CreateProxy("SiteMonitR");
    _hub.Invoke("displayResult");
}
catch (Exception ex)
{
    error = ex.ToString();
}

But that throws an exception, this one.

I don't undertand why I can get the url in the same way the example does, as I'm doing everything as it's done on the Server class.

The goal I'm trying to achieve is that when an endpoint is accesed and something changes in my system, SignalR notifies to the clients connected to it.

I hope anyone can help me understand what's wrong with my work.

EDIT

I'm adding my ServiceConfiguration.Local.cscfg, my ServiceConfiguration.Cloud.cscfg and ServiceDefinition.csdef files as reference here, I think the problem should be around there but to be honest I got no idea as to why this isn't working.

EDIT 2

I'm getting the following exception at this line var url = RoleEnvironment.GetConfigurationSettingValue("http://localhost:4200");

The exception is:

SEHExcetion occurred. External component has thrown an exception.

1 Answer 1

0

The URL is for a GUI - it has to be a web interface for signalr to negotiate the hub connection. In the example, the hub (server) sends updates to connections coming from the configured URL - again a web interface (html page).

The logic to communicate needs to reside in the Server class and be called from the worker role. For example, after making a call to your service in the worker role, then call server.DoSomething("message") to invoke a message to the server. THat code would look something like:

public Class Server 
{ ...
    public void DoSomething(string message)
    {
        _hub.Invoke("doSomething", message);
    }
    ...
}

Then in Server.Run() add:

    // whenever a DoSomething is called
    _hub.On<string>("doSomething", (message) => _hub.Invoke("doSomething", message));

And in SiteMonitRNotificationHub

public class SiteMonitRNotificationHub : Hub
{
    ...
    public void DoSomething(string address)
    {
        Clients.doingSomething(address);
    }
    ...
} 

Finally in the controller script in the web gui:

c.siteMonitorHub
...
    .on('doingSomething', function (message) {
        c.doSomething(message);
    })

and...

this.doSomething= function (message) {
    // do something in your web page with message
};
16
  • so why is it not working if I'm using the same url? Do I have to reference it in a different way? Jan 2, 2013 at 16:28
  • I changed how I get the url like this var url = RoleEnvironment.GetConfigurationSettingValue("GUI_URL"); but I got an exception External component has thrown an exception. I'm really lost as to why I can't create the HubConnection object, or am I misinterpreting something? Jan 2, 2013 at 21:49
  • RoleEnvironment.GetConfigurationSettingValue("name") is an Azure method to get the value from the Azure settings file with the key = "name". You have the URL, just use var url = "localhost:4200". If you aren't running the website in Azure or the Azure emulator, RoleEnvironment.GetConfigurationSettingValue() will always return an exception, likewise if the setting doesn't exist, you will get an error. Jan 3, 2013 at 2:42
  • but I'm running it on the emulator, also I tried to use var url = "localhost:4200"; but I get the SEHExcetion occurred. External component has thrown an exception. Jan 3, 2013 at 4:41
  • Does SiteMonitR.Web start and open in a browser? If so, what is the URL that SiteMonitR.Web runs under when you launch the application? Copy and paste that full URL into the value attribute for setting name="GUI_URL". Jan 3, 2013 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.