Trying to set up a SignalR server in a WPF Windows application using .NET Core 3.0. All the examples I can find assume one has a startup.cs with a Configure() method where I can call UseSignalR. But I don't. I have an "App" class, derived from Application. (I may be missing something fundamental about WPF/Core/Win that is unrelated to SignalR)
Code extract:
public partial class App : Application
{
public IServiceProvider ServiceProvider { get; private set; }
public IConfiguration Configuration { get; private set; }
protected override void OnStartup(StartupEventArgs e)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Configuration = builder.Build();
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
ServiceProvider = serviceCollection.BuildServiceProvider();
var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();
}
private void ConfigureServices(IServiceCollection services)
{
services.AddTransient(typeof(MainWindow));
services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
services.AddSignalR().AddHubOptions<TestHub>(options =>
{
options.EnableDetailedErrors = true;
});
}
So far so good, in that it at least compiles and runs, if not do anything tangible yet.
I believe I now need to actually configure the hub:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSignalR((configure) =>
{
var desiredTransports =
HttpTransportType.WebSockets |
HttpTransportType.LongPolling;
configure.MapHub<TestHub>("/testhub", (options) =>
{
options.Transports = desiredTransports;
});
});
}
But this method never gets called - I don't believe it should from Application, but would from StartUp.
Finally, my hub class:
class TestHub : Hub
{
public Task SendMessage(string user, string message)
{
return Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
Compiles ok but I don't think the hub is actually running so it does nothing, of course.
Is there something different I should be doing to get the hub started and configured correctly in this type of setup?
FWIW I have used SignalR in the standard .NET framework (not Core) with WinForms apps without issue, so I'm vaguely familiar with the concepts, just not familiar with the Core/WPF/Win way.
Many thanks.