Issue
I've created a .net core
controller which, when using postman, I can hit the Get method successfully (validated with breakpoints on the controller constructor and get method) with the URL (https://localhost:44447/api/callsynchronisation/ws)
The code looks like this:
public class CallSynchronisationController : ApiControllerBase
{
private IWebSocketHandler handler;
public CallSynchronisationController(IWebSocketHandler handler)
{
this.handler = handler;
}
[HttpGet("ws")]
public async Task Get()
{
if (HttpContext.WebSockets.IsWebSocketRequest)
{
using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
await handler.Handle(Guid.NewGuid(), webSocket);
}
else
{
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
}
}
}
In my startup.cs
I've added app.UseWebSockets()
. This code does execute as expected.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddApplicationServices();
builder.Services.AddInfrastructureServices(builder.Configuration);
builder.Services.AddWebUIServices();
var app = builder.Build();
app.UseWebSockets();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
// Initialise and seed database
using (var scope = app.Services.CreateScope())
{
var initialiser = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitialiser>();
await initialiser.InitialiseAsync();
await initialiser.SeedAsync();
}
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHealthChecks("/health");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSwaggerUi3(settings =>
{
settings.Path = "/api";
settings.DocumentPath = "/api/specification.json";
});
app.UseRouting();
AddWebUIServices calls onto services.AddControllersWithViews
I've set up websockets in angular with the adress https://wss://localhost:44447/api/callsynchronisation/ws.
When I try to send a message the connection is still in the connecting state
In chrome the initiating ws
request never gets a response from the server. I see is the request with a header "upgrade" with the value websocket
as expected.
The constructor for the controller does not execute (although it does for the previously mentioned https
get request).
What am I doing wrong??
Solution
My application is a .net core with an angular SPA.
The issue was that I was sending web socket requests to the angular app (ng serve) without appropriate proxy configuration (to route it to the .net core app).
In my case, the ws path happened to match a path in the angular proxy settings which were not ws enabled (hence why non ws requests seemed to be working).
I updated the angular proxy configuration (proxy.conf.js) to proxy web socket requests onto the .net core app as ws requests and all is well!
Here is the detail for others:
{
context: ["/ws/*"],
"target": target,
"secure": false,
"ws": true
}
Answered By - iasksillyquestions
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.