Skip to content

Commit 36717c7

Browse files
committed
Fix #2 - log an error during init if the database cannot be connected to.
1 parent f40949c commit 36717c7

File tree

7 files changed

+39
-31
lines changed

7 files changed

+39
-31
lines changed

demo/DemoServer/DemoServer.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.7" />
10+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.0" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

demo/DemoServer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
3232
builder.AddSimpleConsole(options =>
3333
{
3434
options.TimestampFormat = "hh:mm:ss ";
35-
options.SingleLine = true;
35+
options.SingleLine = false;
3636
});
3737
});
3838
});

src/IntelliTect.AspNetCore.SignalR.SqlServer/IntelliTect.AspNetCore.SignalR.SqlServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
55
<LangVersion>9.0</LangVersion>
66
<Nullable>enable</Nullable>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/SqlInstaller.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,43 @@ public SqlInstaller(SqlServerOptions options, ILogger logger, string messagesTab
2828
public async Task Install()
2929
{
3030
_logger.LogInformation("Start installing SignalR SQL objects");
31-
32-
using var connection = new SqlConnection(_options.ConnectionString);
33-
await connection.OpenAsync();
34-
using var command = connection.CreateCommand();
35-
36-
if (_options.AutoEnableServiceBroker)
31+
try
3732
{
38-
try
39-
{
40-
command.CommandText = GetType().Assembly.StringResource("enable-broker.sql");
41-
await command.ExecuteNonQueryAsync();
42-
}
43-
catch (Exception ex)
33+
using var connection = new SqlConnection(_options.ConnectionString);
34+
await connection.OpenAsync();
35+
using var command = connection.CreateCommand();
36+
37+
if (_options.AutoEnableServiceBroker)
4438
{
45-
_logger.LogError(ex, "Unable to automatically enable SQL Server Service Broker.");
39+
try
40+
{
41+
command.CommandText = GetType().Assembly.StringResource("enable-broker.sql");
42+
await command.ExecuteNonQueryAsync();
43+
}
44+
catch (Exception ex)
45+
{
46+
_logger.LogError(ex, "Unable to automatically enable SQL Server Service Broker.");
47+
}
4648
}
47-
}
4849

49-
var script = GetType().Assembly.StringResource("install.sql");
50+
var script = GetType().Assembly.StringResource("install.sql");
51+
52+
script = script.Replace("SET @SCHEMA_NAME = 'SignalR';", "SET @SCHEMA_NAME = '" + _options.SchemaName + "';");
53+
script = script.Replace("SET @TARGET_SCHEMA_VERSION = 1;", "SET @TARGET_SCHEMA_VERSION = " + SchemaVersion + ";");
54+
script = script.Replace("SET @MESSAGE_TABLE_COUNT = 1;", "SET @MESSAGE_TABLE_COUNT = " + _options.TableCount + ";");
55+
script = script.Replace("SET @MESSAGE_TABLE_NAME = 'Messages';", "SET @MESSAGE_TABLE_NAME = '" + _messagesTableNamePrefix + "';");
5056

51-
script = script.Replace("SET @SCHEMA_NAME = 'SignalR';", "SET @SCHEMA_NAME = '" + _options.SchemaName + "';");
52-
script = script.Replace("SET @TARGET_SCHEMA_VERSION = 1;", "SET @TARGET_SCHEMA_VERSION = " + SchemaVersion + ";");
53-
script = script.Replace("SET @MESSAGE_TABLE_COUNT = 1;", "SET @MESSAGE_TABLE_COUNT = " + _options.TableCount + ";");
54-
script = script.Replace("SET @MESSAGE_TABLE_NAME = 'Messages';", "SET @MESSAGE_TABLE_NAME = '" + _messagesTableNamePrefix + "';");
57+
command.CommandText = script;
58+
await command.ExecuteNonQueryAsync();
5559

56-
command.CommandText = script;
57-
await command.ExecuteNonQueryAsync();
60+
_logger.LogInformation("SignalR SQL objects installed");
61+
}
62+
catch (Exception ex)
63+
{
64+
_logger.LogError(ex, "Unable to install SignalR SQL objects");
65+
throw;
66+
}
5867

59-
_logger.LogInformation("SignalR SQL objects installed");
6068
}
6169
}
6270
}

src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/SqlStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public Task Send(byte[] message)
4444

4545
public void Dispose()
4646
{
47-
_logger.LogInformation("{0}Disposing stream {1}", _tracePrefix, _streamIndex);
47+
_logger.LogTrace("{0}Disposing stream {1}", _tracePrefix, _streamIndex);
4848

4949
_receiver.Dispose();
5050
}

src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServerProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private void WriteInvocationCore(ref MessagePackWriter writer, string methodName
110110
writer.WriteArrayHeader(0);
111111
}
112112

113-
WriteHubMessage(ref writer, new InvocationMessage(methodName, args));
113+
WriteHubMessage(ref writer, new InvocationMessage(methodName, args ?? Array.Empty<object[]>()));
114114
}
115115

116116
public SqlServerInvocation ReadInvocationAll(ReadOnlyMemory<byte> data)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public override Task OnDisconnectedAsync(HubConnectionContext connection)
116116
var tasks = new List<Task>();
117117

118118
var feature = connection.Features.Get<ISqlServerFeature>()!;
119+
if (feature is null) return Task.CompletedTask;
120+
119121
var groupNames = feature.Groups;
120122

121123
if (groupNames != null)
@@ -165,7 +167,7 @@ public override Task SendConnectionAsync(string connectionId, string methodName,
165167
var connection = _connections[connectionId];
166168
if (connection != null)
167169
{
168-
return connection.WriteAsync(new InvocationMessage(methodName, args), cancellationToken).AsTask();
170+
return connection.WriteAsync(new InvocationMessage(methodName, args ?? Array.Empty<object[]>()), cancellationToken).AsTask();
169171
}
170172

171173
var message = _protocol.WriteTargetedInvocation(MessageType.InvocationConnection, connectionId, methodName, args, null);
@@ -382,8 +384,6 @@ private async Task EnsureSqlServerConnection()
382384
{
383385
if (_streams.Count == 0)
384386
{
385-
// NOTE: Called from a ThreadPool thread
386-
387387
var installer = new SqlInstaller(_options, _logger, _tableNamePrefix);
388388
await installer.Install();
389389

0 commit comments

Comments
 (0)