I have a solution, which contains the SignalR-Tutorial project (SignalR-Chat) and a project which is basically a console application.
In the BlazorSignalRApp I created a static function called 'StartServerAsync' which is starting the web application. When I set the BlazorSignalRApp as Startup project, everything is running smoothly. Port set to 7283, stylesheet loaded and everything is working like in the tutorial (https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-8.0&tabs=visual-studio ).
When I try to start the webapplication with the the same 'StartServerAsync' function, the server starts (Port 5000 this time) but is not reachable and no textbox and no stylesheet is visible.
I know, there is not much missing... I must be related to some settings files not deployed. Are there any examples r documentation? I know, that I need to redirect the wwwroot for the static files. I tried, but it did not change anything.
Could somebody point me the right direction?
Here my implementation of StartServerAsync:
public class BlazorServerStartup
{
public static async Task<WebApplication> StartServerAsync(Serilog.ILogger logger, CancellationToken cancellationToken, WebApplicationOptions? webApplicationOptions = null)
{
WebApplicationBuilder builder;
if (webApplicationOptions is null)
{
builder = WebApplication.CreateBuilder();
}
else
{
builder = WebApplication.CreateBuilder(webApplicationOptions);
}
builder.Host.UseSerilog(logger);
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});
var app = builder.Build();
app.UseResponseCompression();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapHub<ModuleNotificationHub>("/notificationHub");
app.MapFallbackToPage("/_Host");
await app.StartAsync(cancellationToken)
.ConfigureAwait(false);
return app;
}
}
Here is the code from the console application to start the server:
public class Program
{
private static Serilog.ILogger? Logger;
private static CancellationTokenSource? _cancellationTokenSource;
private static CancellationToken _cancellationToken;
private static async Task Main(string[] args)
{
_cancellationTokenSource = new CancellationTokenSource();
_cancellationToken = _cancellationTokenSource.Token;
Console.CancelKeyPress += Console_CancelKeyPress;
Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Debug()
.CreateLogger();
Logger.Information("Starting Server");
try
{
var currentDirectory = Directory.GetCurrentDirectory();
var webRootPath = Path.GetFullPath(Path.Combine(currentDirectory, "..", "..", "..", "..", "BlazorSignalRApp", "wwwroot"));
var webAppOptions = new WebApplicationOptions
{
ContentRootPath = currentDirectory,
WebRootPath = webRootPath,
};
var server = await BlazorServerStartup.StartServerAsync(Logger, _cancellationToken, webAppOptions).ConfigureAwait(false);
while (!_cancellationToken.IsCancellationRequested)
{
await Task.Delay(100).ConfigureAwait(false);
}
await server.WaitForShutdownAsync(_cancellationToken).ConfigureAwait(false);
Logger.Information("Stop Server");
}
catch (Exception ex)
{
Logger.Error(ex, "{ErrorMessage:l}", ex.Message);
}
}