I've been working with ASP.Net for about 3 months now and wanted to have a look at the SignalR streams today. Everything works fine locally, but as soon as I build it via Github Actions with Docker and deploy it to my Ubuntu server (Docker and Portainer are not running anything else) the SignalR stream is no longer accessible. All other endpoints (GET, POST, etc.) are perfectly accessible.
Program.cs
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.ResponseCompression;
using mushroom_backend;
using mushroom_backend.Hubs;
using mushroom_backend.Interfaces;
using mushroom_backend.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.Configure<MushroomStoreDatabaseSettings>(
builder.Configuration.GetSection("MushroomStoreDatabase"));
builder.Services.AddSingleton<ILogsService, LogsService>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddProblemDetails();
builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.Services.AddSignalR();
builder.Services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
["application/octet-stream"]);
});
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
policy.AllowAnyMethod().AllowAnyOrigin().AllowAnyHeader().SetPreflightMaxAge(TimeSpan.FromSeconds(3600))
.SetIsOriginAllowedToAllowWildcardSubdomains());
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
app.MapHub<LogHub>("/api/hub/logHub");
app.UseResponseCompression();
app.Run();
Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
ENV ASPNETCORE_URLS=http://0.0.0.0:5070
WORKDIR /app
EXPOSE 5070
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["mushroom_backend.csproj", "./"]
RUN dotnet restore "mushroom_backend.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "mushroom_backend.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "mushroom_backend.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV ASPNETCORE_ENVIRONMENT=Production
ENTRYPOINT ["dotnet", "mushroom_backend.dll", "--server.urls", "http://+:5070"]
appsettings.json
{
"MushroomStoreDatabase": {
"ConnectionString": "mongodb://felix:<password>@<url>:7070/",
"DatabaseName": "MushroomStore",
"FavoritesCollectionName": "Favorites"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
LogHub.cs
using Microsoft.AspNetCore.SignalR;
namespace mushroom_backend.Hubs;
public class LogHub(ILogger<LogHub> logger) : Hub
{
private readonly ILogger<LogHub> _logger = logger;
public override Task OnConnectedAsync()
{
_logger.LogInformation("New Connection: [{ConnectionId}]", Context.ConnectionId);
return base.OnConnectedAsync();
}
}
Even after a long search I have not found the problem. I have already restarted the project several times from 0, restarted the server where everything is running, adjusted configs and so on. Maybe someone here can help me. Attached is my program.cs, appsettings.json, dockerfile and the hub file which doesn't do much yet.
Thank you in advance
after the tip from jason (turn on logging) I realised that I had a "/" in the path.
I had
app.MapHub<LogHub>("/api/hub/logHub");
but it belongs to
app.MapHub<LogHub>("api/hub/logHub");
Thanks again to jason