1010
1111namespace IntelliTect . AspNetCore . SignalR . SqlServer . Internal
1212{
13- internal class SqlInstaller
13+ internal class SqlInstaller ( SqlServerOptions options , ILogger logger , string messagesTableNamePrefix , string tracePrefix )
1414 {
1515 private const int SchemaVersion = 1 ;
1616
17- private readonly string _messagesTableNamePrefix ;
18- private readonly ILogger _logger ;
19- private readonly SqlServerOptions _options ;
20-
21- public SqlInstaller ( SqlServerOptions options , ILogger logger , string messagesTableNamePrefix )
22- {
23- _logger = logger ;
24- _options = options ;
25- _messagesTableNamePrefix = messagesTableNamePrefix ;
26- }
27-
2817 public async Task Install ( )
2918 {
30- if ( ! _options . AutoEnableServiceBroker && ! _options . AutoInstallSchema )
19+ if ( ! options . AutoEnableServiceBroker && ! options . AutoInstallSchema )
3120 {
32- _logger . LogInformation ( "Skipping install of SignalR SQL objects" ) ;
21+ logger . LogInformation ( "{HubName}: Skipping install of SignalR SQL objects" , tracePrefix ) ;
3322 return ;
3423 }
3524
36- _logger . LogInformation ( "Start installing SignalR SQL objects" ) ;
25+ await options . InstallLock . WaitAsync ( ) ;
26+ logger . LogInformation ( "{HubName}: Start installing SignalR SQL objects" , tracePrefix ) ;
3727 try
3828 {
39- using var connection = new SqlConnection ( _options . ConnectionString ) ;
29+ using var connection = new SqlConnection ( options . ConnectionString ) ;
4030 await connection . OpenAsync ( ) ;
4131 using var command = connection . CreateCommand ( ) ;
4232
43- if ( _options . AutoEnableServiceBroker )
33+ if ( options . AutoEnableServiceBroker )
4434 {
4535 try
4636 {
@@ -49,31 +39,34 @@ public async Task Install()
4939 }
5040 catch ( Exception ex )
5141 {
52- _logger . LogError ( ex , "Unable to automatically enable SQL Server Service Broker." ) ;
42+ logger . LogError ( ex , "Unable to automatically enable SQL Server Service Broker." ) ;
5343 }
5444 }
5545
56- if ( _options . AutoInstallSchema )
46+ if ( options . AutoInstallSchema )
5747 {
5848 var script = GetType ( ) . Assembly . StringResource ( "install.sql" ) ;
5949
60- script = script . Replace ( "SET @SCHEMA_NAME = 'SignalR';" , "SET @SCHEMA_NAME = '" + _options . SchemaName + "';" ) ;
50+ script = script . Replace ( "SET @SCHEMA_NAME = 'SignalR';" , "SET @SCHEMA_NAME = '" + options . SchemaName + "';" ) ;
6151 script = script . Replace ( "SET @TARGET_SCHEMA_VERSION = 1;" , "SET @TARGET_SCHEMA_VERSION = " + SchemaVersion + ";" ) ;
62- script = script . Replace ( "SET @MESSAGE_TABLE_COUNT = 1;" , "SET @MESSAGE_TABLE_COUNT = " + _options . TableCount + ";" ) ;
63- script = script . Replace ( "SET @MESSAGE_TABLE_NAME = 'Messages_YourHubName';" , "SET @MESSAGE_TABLE_NAME = '" + _messagesTableNamePrefix + "';" ) ;
52+ script = script . Replace ( "SET @MESSAGE_TABLE_COUNT = 1;" , "SET @MESSAGE_TABLE_COUNT = " + options . TableCount + ";" ) ;
53+ script = script . Replace ( "SET @MESSAGE_TABLE_NAME = 'Messages_YourHubName';" , "SET @MESSAGE_TABLE_NAME = '" + messagesTableNamePrefix + "';" ) ;
6454
6555 command . CommandText = script ;
6656 await command . ExecuteNonQueryAsync ( ) ;
6757
68- _logger . LogInformation ( "SignalR SQL objects installed" ) ;
58+ logger . LogInformation ( "{HubName}: SignalR SQL objects installed" , messagesTableNamePrefix ) ;
6959 }
7060 }
7161 catch ( Exception ex )
7262 {
73- _logger . LogError ( ex , "Unable to install SignalR SQL objects" ) ;
63+ logger . LogError ( ex , "{HubName}: Unable to install SignalR SQL objects" , messagesTableNamePrefix ) ;
7464 throw ;
7565 }
76-
66+ finally
67+ {
68+ options . InstallLock . Release ( ) ;
69+ }
7770 }
7871 }
7972}
0 commit comments