Problem with invoking method from SignalR Hub. Invoking failed. Promise rejected
Asked Answered
S

1

6

I've been hanging on to the problem for over a week now and can't find a way to fix it because I'm pretty new to the .NET and angular environment. Also, I can't find a solution on the internet that fits my problem.

I have a service that sends me data every second via REST api. I would like to retrieve this data in the client without having to reload the whole page continuously. Therefore I have chosen SignalR.

So far everything works and the connection to the hub is set up. But when I try to call the method from the hub, I get an error message saying:

Invoking 'GetPerformanceSnapshotData' failed.  Rejecting promise... 
Promise rejected.

ErrorObservable {_isScalar: false, error: Error: An error occurred while sending the request.
    at Object.error (http://localhost:4200/scrip......, scheduler: undefined}
error: Error. An error occurred while sending the request: An error occurred while sending the request. at Object.error (http://localhost:4200/...)...

This is my call method on the client:

public GetPerformanceSnapshotData() {
    this.connect().then((connection) => {
      this.invoke("GetPerformanceSnapshotData").then((data: string) => {
        console.log(data);
      }).catch(error => {
        this.dialogService.showError(error);
        return Observable.throw(error);
      });
    }).catch(error => {
      this.dialogService.showError(error);
      return Observable.throw(error);
    });
  }
}

The server side method looks like this:

namespace ...
{
    using System.Threading.Tasks;
    using System.Net.Http;
    using Microsoft.AspNet.SignalR;

    public class PerformanceSnapshotHub : Hub
    {
        public async Task<string> GetPerformanceSnapshotData()
        {
            using (var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }))
            {
                var response = await client.GetAsync("http://localhost:8080/api/PerformanceSnapshot");
                response.EnsureSuccessStatusCode();
                return await response.Content.ReadAsStringAsync();
            }
        }
    }
}

I don't understand what's wrong. Any ideas?

Spermophile answered 24/10, 2018 at 9:0 Comment(1)
is there no error in the response of the chrome network tab?Samala
S
0

I got it done. There is nothing wrong with the code. The problem was that a firewall blocked the request on the service for whatever reason. After it was opened, everything worked.

The Network tab was unfortunately not very revealing, I came across the issue by accident.

Spermophile answered 26/11, 2018 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.