Skip to content

Commit 9c6d2ef

Browse files
committed
refactor: convert DemoServer to Program.cs style (remove Startup.cs)
1 parent f3a9eb6 commit 9c6d2ef

File tree

3 files changed

+60
-119
lines changed

3 files changed

+60
-119
lines changed

demo/DemoServer/Program.cs

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,63 @@
1+
using DemoServer;
12
using IntelliTect.AspNetCore.SignalR.SqlServer;
2-
using Microsoft.AspNetCore.Hosting;
3-
using Microsoft.Extensions.Configuration;
4-
using Microsoft.Extensions.Hosting;
5-
using Microsoft.Extensions.Logging;
6-
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
9-
using System.Threading.Tasks;
10-
11-
namespace DemoServer
3+
using Microsoft.AspNetCore.SignalR;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
// Configure app configuration
8+
builder.Configuration
9+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
10+
.AddJsonFile("appsettings.localhost.json", optional: true, reloadOnChange: true)
11+
.AddEnvironmentVariables();
12+
13+
// Configure logging
14+
builder.Logging.AddSimpleConsole(options =>
1215
{
13-
public static class Program
16+
options.TimestampFormat = "hh:mm:ss ";
17+
options.SingleLine = false;
18+
});
19+
20+
// Configure services
21+
string connectionString = builder.Configuration.GetConnectionString("Default")!;
22+
23+
builder.Services.AddSingleton<IUserIdProvider, UserIdProvider>();
24+
builder.Services.AddRazorPages();
25+
builder.Services.AddSignalR()
26+
.AddSqlServer(o =>
1427
{
15-
public static void Main(string[] args)
16-
{
17-
CreateHostBuilder(args).Build().Run();
18-
}
19-
20-
public static IHostBuilder CreateHostBuilder(string[] args) =>
21-
Host.CreateDefaultBuilder(args)
22-
.ConfigureWebHostDefaults(webBuilder =>
23-
{
24-
webBuilder.UseStartup<Startup>()
25-
.ConfigureAppConfiguration((builder, config) => config
26-
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
27-
.AddJsonFile("appsettings.localhost.json", optional: true, reloadOnChange: true)
28-
.AddEnvironmentVariables()
29-
)
30-
.ConfigureLogging(builder =>
31-
{
32-
builder.AddSimpleConsole(options =>
33-
{
34-
options.TimestampFormat = "hh:mm:ss ";
35-
options.SingleLine = false;
36-
});
37-
});
38-
});
39-
}
28+
o.ConnectionString = connectionString;
29+
o.AutoEnableServiceBroker = true;
30+
o.TableSlugGenerator = hubType => hubType.Name;
31+
o.TableCount = 1;
32+
o.SchemaName = "SignalRCore";
33+
});
34+
35+
builder.Services.AddOptions<SqlServerOptions>().Configure<IConfiguration>((o, config) =>
36+
{
37+
o.ConnectionString = connectionString;
38+
});
39+
40+
// Build the app
41+
var app = builder.Build();
42+
43+
// Configure middleware
44+
if (app.Environment.IsDevelopment())
45+
{
46+
app.UseDeveloperExceptionPage();
47+
}
48+
else
49+
{
50+
app.UseExceptionHandler("/Error");
51+
app.UseHsts();
4052
}
53+
54+
app.UseHttpsRedirection();
55+
app.UseStaticFiles();
56+
app.UseRouting();
57+
app.UseAuthorization();
58+
59+
app.MapRazorPages();
60+
app.MapHub<ChatHubA>("/chatHubA");
61+
app.MapHub<ChatHubB>("/chatHubB");
62+
63+
app.Run();

demo/DemoServer/Startup.cs

Lines changed: 0 additions & 82 deletions
This file was deleted.

demo/DemoServer/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
},
1010
"AllowedHosts": "*",
1111
"ConnectionStrings": {
12-
"Default": "Server=localhost;Database=SignalRTestDb;Trusted_Connection=True;MultipleActiveResultSets=True;"
12+
"Default": "Server=localhost;Database=SignalRTestDb;Trusted_Connection=True;MultipleActiveResultSets=True;TrustServerCertificate=True"
1313
}
1414
}

0 commit comments

Comments
 (0)