Skip to content

Commit 4614bb2

Browse files
committed
feat: delay attempts to install SQL Server schema until the host has started. This avoids errors in Program.cs-style applications where install would be attempted as soon as MapHub was invoked, before an application would have an opportunity to create its own database.
1 parent 7c9eae1 commit 4614bb2

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

src/IntelliTect.AspNetCore.SignalR.SqlServer/SqlServerHubLifetimeManager.cs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections.Generic;
64
using System.Globalization;
7-
using System.IO;
8-
using System.Linq;
9-
using System.Text;
10-
using System.Threading;
11-
using System.Threading.Tasks;
125
using IntelliTect.AspNetCore.SignalR.SqlServer.Internal.Messages;
136
using Microsoft.AspNetCore.SignalR.Protocol;
147
using IntelliTect.AspNetCore.SignalR.SqlServer.Internal;
158
using Microsoft.Extensions.Logging;
169
using Microsoft.Extensions.Options;
1710
using Microsoft.AspNetCore.SignalR;
1811
using System.Runtime.CompilerServices;
12+
using Microsoft.Extensions.Hosting;
1913

2014
[assembly: InternalsVisibleTo("IntelliTect.AspNetCore.SignalR.SqlServer.Tests")]
2115

@@ -43,19 +37,6 @@ public class SqlServerHubLifetimeManager<THub> : HubLifetimeManager<THub>, IDisp
4337
private int _internalId;
4438
private bool _disposed;
4539

46-
/// <summary>
47-
/// Constructs the <see cref="SqlServerHubLifetimeManager{THub}"/> with types from Dependency Injection.
48-
/// </summary>
49-
/// <param name="logger">The logger to write information about what the class is doing.</param>
50-
/// <param name="options">The <see cref="SqlServerOptions"/> that influence behavior of the SQL Server connection.</param>
51-
/// <param name="hubProtocolResolver">The <see cref="IHubProtocolResolver"/> to get an <see cref="IHubProtocol"/> instance when writing to connections.</param>
52-
public SqlServerHubLifetimeManager(ILogger<SqlServerHubLifetimeManager<THub>> logger,
53-
IOptions<SqlServerOptions> options,
54-
IHubProtocolResolver hubProtocolResolver)
55-
: this(logger, options, hubProtocolResolver, globalHubOptions: null, hubOptions: null)
56-
{
57-
}
58-
5940
/// <summary>
6041
/// Constructs the <see cref="SqlServerHubLifetimeManager{THub}"/> with types from Dependency Injection.
6142
/// </summary>
@@ -64,11 +45,15 @@ public SqlServerHubLifetimeManager(ILogger<SqlServerHubLifetimeManager<THub>> lo
6445
/// <param name="hubProtocolResolver">The <see cref="IHubProtocolResolver"/> to get an <see cref="IHubProtocol"/> instance when writing to connections.</param>
6546
/// <param name="globalHubOptions">The global <see cref="HubOptions"/>.</param>
6647
/// <param name="hubOptions">The <typeparamref name="THub"/> specific options.</param>
67-
public SqlServerHubLifetimeManager(ILogger<SqlServerHubLifetimeManager<THub>> logger,
68-
IOptions<SqlServerOptions> options,
69-
IHubProtocolResolver hubProtocolResolver,
70-
IOptions<HubOptions>? globalHubOptions,
71-
IOptions<HubOptions<THub>>? hubOptions)
48+
/// <param name="lifetime">The host lifetime instance</param>
49+
public SqlServerHubLifetimeManager(
50+
ILogger<SqlServerHubLifetimeManager<THub>> logger,
51+
IOptions<SqlServerOptions> options,
52+
IHubProtocolResolver hubProtocolResolver,
53+
IOptions<HubOptions>? globalHubOptions,
54+
IOptions<HubOptions<THub>>? hubOptions,
55+
IHostApplicationLifetime lifetime
56+
)
7257
{
7358
_logger = logger;
7459
_options = options.Value;
@@ -86,7 +71,9 @@ public SqlServerHubLifetimeManager(ILogger<SqlServerHubLifetimeManager<THub>> lo
8671
_protocol = new SqlServerProtocol(new DefaultHubMessageSerializer(hubProtocolResolver, supportedProtocols, null));
8772
}
8873

89-
_ = EnsureSqlServerConnection();
74+
// Delay until startup so the application can have a chance to create the database
75+
// if the application does that in Program.cs before app.Run().
76+
lifetime.ApplicationStarted.Register(() => _ = EnsureSqlServerConnection());
9077
}
9178

9279
/// <inheritdoc />

0 commit comments

Comments
 (0)