I have an Angular (Angular 9) application as client and a ASP.NET WebApi as server. The server has an additional Signalr2 interface. First thing is I don't know if the server where my application will be installed supports websockets. If not, my angular application would create an http request to the WEB API and get the response asynchrounus, so normal REST. If the server supports websockets, I would like to do a websocket request from my client side and get the response from signalR. The client side is clear, I would do some kind of HTTPClient abstraction to generate one or the other and wait for the response. The other thing is, how can I call a Web APi method from my SignalRHub without using the .Net HttpClient? And if I have to use the HttpClient, is the overhead so big, that doing a normal http request would result the same?
This could be my SignalR data:
public WebApiRequest{
public string HttpMethod{ get; set;}
public string Route{ get; set;}
public OrderedDictionary<string, object> Parameters{ get; set;}
public string Content{ get; set;}
}
And this the SignalR response data:
public WebApiResponse{
public int StatusCode{ get; set;}
public object Data{ get; set;}
}
This the Test Web Api Controller:
[RoutePrefix("api/test")]
[Authorize]
public class TestController : ApiController
{
[HttpPost]
[Route("calculate")]
public async Task<IHttpActionResult> Calculate(int id, Calculatedata calc)
{
return Ok(CalculateMyTestData(id, calc));
}
}
Now with the data from the signalr request I would like to call the 'Calculate' method of the 'TestController' and give the response as WebApiResponse back.
An idea, this is inside my Hub:
public void DoWebApirequest(WebApiRequest data)
{
var test = new TestController()
{
var method = GetHttpMethod(data.HttpMethod);
Request = new HttpRequestMessage(method , this.Context.Request.Url)
{
Content = new StringContent(data.Content, Encoding.UTF8, "application/json")
}
};
foreach (var contextHeader in this.Context.Headers)
{
test.Request.Headers.Add(contextHeader.Key, contextHeader.Value);
}
// Now fill the parameters and call the method you like by reflection...
test.Calculate(...);
}
Could this work?