Skip to content

Commit 8cf5845

Browse files
authored
Enable nullable on Diagnostics.EntityFrameworkCore (#28814)
* Enable nullable on Diagnostics.EntityFrameworkCore
1 parent 6ef0492 commit 8cf5845

10 files changed

+43
-45
lines changed

src/Middleware/Diagnostics.EntityFrameworkCore/src/DatabaseDeveloperPageExceptionFilter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Microsoft.Extensions.Logging;
1414
using Microsoft.Extensions.Options;
1515

16-
#nullable enable
1716
namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
1817
{
1918
/// <summary>

src/Middleware/Diagnostics.EntityFrameworkCore/src/DatabaseDeveloperPageExceptionFilterServiceExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
77
using Microsoft.Extensions.DependencyInjection.Extensions;
88

9-
#nullable enable
109
namespace Microsoft.Extensions.DependencyInjection
1110
{
1211
/// <summary>

src/Middleware/Diagnostics.EntityFrameworkCore/src/DatabaseErrorPageMiddleware.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
2121
/// Captures synchronous and asynchronous database related exceptions from the pipeline that may be resolved using Entity Framework
2222
/// migrations. When these exceptions occur an HTML response with details of possible actions to resolve the issue is generated.
2323
/// </summary>
24-
public class DatabaseErrorPageMiddleware : IObserver<DiagnosticListener>, IObserver<KeyValuePair<string, object>>
24+
public class DatabaseErrorPageMiddleware : IObserver<DiagnosticListener>, IObserver<KeyValuePair<string, object?>>
2525
{
2626
private static readonly AsyncLocal<DiagnosticHolder> _localDiagnostic = new AsyncLocal<DiagnosticHolder>();
2727

@@ -33,8 +33,8 @@ public void Hold(Exception exception, Type contextType)
3333
ContextType = contextType;
3434
}
3535

36-
public Exception Exception { get; private set; }
37-
public Type ContextType { get; private set; }
36+
public Exception? Exception { get; private set; }
37+
public Type? ContextType { get; private set; }
3838
}
3939

4040
private readonly RequestDelegate _next;
@@ -110,8 +110,8 @@ public virtual async Task Invoke(HttpContext httpContext)
110110
{
111111
if (ShouldDisplayErrorPage(exception))
112112
{
113-
var contextType = _localDiagnostic.Value.ContextType;
114-
var details = await httpContext.GetContextDetailsAsync(contextType, _logger);
113+
var contextType = _localDiagnostic.Value!.ContextType;
114+
var details = await httpContext.GetContextDetailsAsync(contextType!, _logger);
115115

116116
if (details != null && (details.PendingModelChanges || details.PendingMigrations.Count() > 0))
117117
{
@@ -139,7 +139,7 @@ private bool ShouldDisplayErrorPage(Exception exception)
139139
{
140140
_logger.AttemptingToMatchException(exception.GetType());
141141

142-
var lastRecordedException = _localDiagnostic.Value.Exception;
142+
var lastRecordedException = _localDiagnostic.Value!.Exception;
143143

144144
if (lastRecordedException == null)
145145
{
@@ -175,7 +175,7 @@ void IObserver<DiagnosticListener>.OnNext(DiagnosticListener diagnosticListener)
175175
}
176176
}
177177

178-
void IObserver<KeyValuePair<string, object>>.OnNext(KeyValuePair<string, object> keyValuePair)
178+
void IObserver<KeyValuePair<string, object?>>.OnNext(KeyValuePair<string, object?> keyValuePair)
179179
{
180180
switch (keyValuePair.Value)
181181
{
@@ -204,11 +204,11 @@ void IObserver<DiagnosticListener>.OnError(Exception error)
204204
{
205205
}
206206

207-
void IObserver<KeyValuePair<string, object>>.OnCompleted()
207+
void IObserver<KeyValuePair<string, object?>>.OnCompleted()
208208
{
209209
}
210210

211-
void IObserver<KeyValuePair<string, object>>.OnError(Exception error)
211+
void IObserver<KeyValuePair<string, object?>>.OnError(Exception error)
212212
{
213213
}
214214
}

src/Middleware/Diagnostics.EntityFrameworkCore/src/DiagnosticsEntityFrameworkCoreLoggerExtensions.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
99
internal static class DiagnosticsEntityFrameworkCoreLoggerExtensions
1010
{
1111
// MigrationsEndPointMiddleware
12-
private static readonly Action<ILogger, Exception> _noContextType = LoggerMessage.Define(
12+
private static readonly Action<ILogger, Exception?> _noContextType = LoggerMessage.Define(
1313
LogLevel.Error,
1414
new EventId(1, "NoContextType"),
1515
"No context type was specified. Ensure the form data from the request includes a 'context' value, specifying the context type name to apply migrations for.");
1616

17-
private static readonly Action<ILogger, string, Exception> _contextNotRegistered = LoggerMessage.Define<string>(
17+
private static readonly Action<ILogger, string, Exception?> _contextNotRegistered = LoggerMessage.Define<string>(
1818
LogLevel.Error,
1919
new EventId(3, "ContextNotRegistered"),
2020
"The context type '{ContextTypeName}' was not found in services. This usually means the context was not registered in services during startup. You probably want to call AddScoped<>() inside the UseServices(...) call in your application startup code.");
2121

22-
private static readonly Action<ILogger, string, Exception> _requestPathMatched = LoggerMessage.Define<string>(
22+
private static readonly Action<ILogger, string, Exception?> _requestPathMatched = LoggerMessage.Define<string>(
2323
LogLevel.Debug,
2424
new EventId(4, "RequestPathMatched"),
2525
"Request path matched the path configured for this migrations endpoint({RequestPath}). Attempting to process the migrations request.");
2626

27-
private static readonly Action<ILogger, string, Exception> _applyingMigrations = LoggerMessage.Define<string>(
27+
private static readonly Action<ILogger, string, Exception?> _applyingMigrations = LoggerMessage.Define<string>(
2828
LogLevel.Debug,
2929
new EventId(5, "ApplyingMigrations"),
3030
"Request is valid, applying migrations for context '{ContextTypeName}'");
3131

32-
private static readonly Action<ILogger, string, Exception> _migrationsApplied = LoggerMessage.Define<string>(
32+
private static readonly Action<ILogger, string, Exception?> _migrationsApplied = LoggerMessage.Define<string>(
3333
LogLevel.Debug,
3434
new EventId(6, "MigrationsApplied"),
3535
"Migrations successfully applied for context '{ContextTypeName}'.");
@@ -40,32 +40,32 @@ internal static class DiagnosticsEntityFrameworkCoreLoggerExtensions
4040
"An error occurred while applying the migrations for '{ContextTypeName}'. See InnerException for details:");
4141

4242
// DatabaseErrorPageMiddleware
43-
private static readonly Action<ILogger, Type, Exception> _attemptingToMatchException = LoggerMessage.Define<Type>(
43+
private static readonly Action<ILogger, Type, Exception?> _attemptingToMatchException = LoggerMessage.Define<Type>(
4444
LogLevel.Debug,
4545
new EventId(1, "AttemptingToMatchException"),
4646
"{ExceptionType} occurred, checking if Entity Framework recorded this exception as resulting from a failed database operation.");
4747

48-
private static readonly Action<ILogger, Exception> _noRecordedException = LoggerMessage.Define(
48+
private static readonly Action<ILogger, Exception?> _noRecordedException = LoggerMessage.Define(
4949
LogLevel.Debug,
5050
new EventId(2, "NoRecordedException"),
5151
"Entity Framework did not record any exceptions due to failed database operations. This means the current exception is not a failed Entity Framework database operation, or the current exception occurred from a DbContext that was not obtained from request services.");
5252

53-
private static readonly Action<ILogger, Exception> _noMatch = LoggerMessage.Define(
53+
private static readonly Action<ILogger, Exception?> _noMatch = LoggerMessage.Define(
5454
LogLevel.Debug,
5555
new EventId(3, "NoMatchFound"),
5656
"The current exception (and its inner exceptions) do not match the last exception Entity Framework recorded due to a failed database operation. This means the database operation exception was handled and another exception occurred later in the request.");
5757

58-
private static readonly Action<ILogger, Exception> _matched = LoggerMessage.Define(
58+
private static readonly Action<ILogger, Exception?> _matched = LoggerMessage.Define(
5959
LogLevel.Debug,
6060
new EventId(4, "MatchFound"),
6161
"Entity Framework recorded that the current exception was due to a failed database operation. Attempting to show database error page.");
6262

63-
private static readonly Action<ILogger, string, Exception> _contextNotRegisteredDatabaseErrorPageMiddleware = LoggerMessage.Define<string>(
63+
private static readonly Action<ILogger, string, Exception?> _contextNotRegisteredDatabaseErrorPageMiddleware = LoggerMessage.Define<string>(
6464
LogLevel.Error,
6565
new EventId(5, "ContextNotRegistered"),
6666
"The context type '{ContextTypeName}' was not found in services. This usually means the context was not registered in services during startup. You probably want to call AddScoped<>() inside the UseServices(...) call in your application startup code. Skipping display of the database error page.");
6767

68-
private static readonly Action<ILogger, Exception> _notRelationalDatabase = LoggerMessage.Define(
68+
private static readonly Action<ILogger, Exception?> _notRelationalDatabase = LoggerMessage.Define(
6969
LogLevel.Debug,
7070
new EventId(6, "NotRelationalDatabase"),
7171
"The target data store is not a relational database. Skipping the database error page.");

src/Middleware/Diagnostics.EntityFrameworkCore/src/HttpContextDatabaseContextDetailsExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using Microsoft.Extensions.DependencyInjection;
1616
using Microsoft.Extensions.Logging;
1717

18-
#nullable enable
1918
namespace Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
2019
{
2120
internal static class HttpContextDatabaseContextDetailsExtensions
@@ -26,7 +25,7 @@ internal static class HttpContextDatabaseContextDetailsExtensions
2625

2726
if (context == null)
2827
{
29-
logger.ContextNotRegisteredDatabaseErrorPageMiddleware(dbcontextType.FullName);
28+
logger.ContextNotRegisteredDatabaseErrorPageMiddleware(dbcontextType.FullName!);
3029
return null;
3130
}
3231

src/Middleware/Diagnostics.EntityFrameworkCore/src/Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
<PackageTags>aspnetcore;diagnostics;entityframeworkcore</PackageTags>
8+
<Nullable>enable</Nullable>
89
</PropertyGroup>
910

1011
<ItemGroup>

src/Middleware/Diagnostics.EntityFrameworkCore/src/MigrationsEndPointMiddleware.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public virtual async Task Invoke(HttpContext context)
7474

7575
if (db != null)
7676
{
77-
var dbName = db.GetType().FullName;
77+
var dbName = db.GetType().FullName!;
7878
try
7979
{
8080
_logger.ApplyingMigrations(dbName);
@@ -103,7 +103,7 @@ public virtual async Task Invoke(HttpContext context)
103103
}
104104
}
105105

106-
private static async Task<DbContext> GetDbContext(HttpContext context, ILogger logger)
106+
private static async Task<DbContext?> GetDbContext(HttpContext context, ILogger logger)
107107
{
108108
var form = await context.Request.ReadFormAsync();
109109
var contextTypeName = form["context"];
@@ -132,9 +132,9 @@ private static async Task<DbContext> GetDbContext(HttpContext context, ILogger l
132132
return null;
133133
}
134134

135-
var contextType = Type.GetType(contextTypeName);
135+
var contextType = Type.GetType(contextTypeName)!;
136136

137-
var db = (DbContext)context.RequestServices.GetService(contextType);
137+
var db = (DbContext?)context.RequestServices.GetService(contextType);
138138

139139
return db;
140140
}

src/Middleware/Diagnostics.EntityFrameworkCore/src/PublicAPI.Shipped.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ virtual Microsoft.AspNetCore.Builder.DatabaseErrorPageOptions.MigrationsEndPoint
1717
virtual Microsoft.AspNetCore.Builder.MigrationsEndPointOptions.Path.get -> Microsoft.AspNetCore.Http.PathString
1818
virtual Microsoft.AspNetCore.Builder.MigrationsEndPointOptions.Path.set -> void
1919
~Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseDeveloperPageExceptionFilter.DatabaseDeveloperPageExceptionFilter(Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseDeveloperPageExceptionFilter!>! logger, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.DatabaseErrorPageOptions!>! options) -> void
20-
~Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.DatabaseErrorPageMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.DatabaseErrorPageOptions> options) -> void
21-
~Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.MigrationsEndPointMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware> logger, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.MigrationsEndPointOptions> options) -> void
22-
~static Microsoft.AspNetCore.Builder.DatabaseErrorPageExtensions.UseDatabaseErrorPage(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
23-
~static Microsoft.AspNetCore.Builder.DatabaseErrorPageExtensions.UseDatabaseErrorPage(this Microsoft.AspNetCore.Builder.IApplicationBuilder app, Microsoft.AspNetCore.Builder.DatabaseErrorPageOptions options) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
24-
~static Microsoft.AspNetCore.Builder.MigrationsEndPointExtensions.UseMigrationsEndPoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
25-
~static Microsoft.AspNetCore.Builder.MigrationsEndPointExtensions.UseMigrationsEndPoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder app, Microsoft.AspNetCore.Builder.MigrationsEndPointOptions options) -> Microsoft.AspNetCore.Builder.IApplicationBuilder
26-
~virtual Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext) -> System.Threading.Tasks.Task
27-
~virtual Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext context) -> System.Threading.Tasks.Task
20+
~Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.DatabaseErrorPageMiddleware(Microsoft.AspNetCore.Http.RequestDelegate! next, Microsoft.Extensions.Logging.ILoggerFactory! loggerFactory, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.DatabaseErrorPageOptions!>! options) -> void
21+
~Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.MigrationsEndPointMiddleware(Microsoft.AspNetCore.Http.RequestDelegate! next, Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware!>! logger, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.MigrationsEndPointOptions!>! options) -> void
22+
static Microsoft.AspNetCore.Builder.DatabaseErrorPageExtensions.UseDatabaseErrorPage(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
23+
static Microsoft.AspNetCore.Builder.DatabaseErrorPageExtensions.UseDatabaseErrorPage(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, Microsoft.AspNetCore.Builder.DatabaseErrorPageOptions! options) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
24+
static Microsoft.AspNetCore.Builder.MigrationsEndPointExtensions.UseMigrationsEndPoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
25+
static Microsoft.AspNetCore.Builder.MigrationsEndPointExtensions.UseMigrationsEndPoint(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, Microsoft.AspNetCore.Builder.MigrationsEndPointOptions! options) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
26+
virtual Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext! httpContext) -> System.Threading.Tasks.Task!
27+
virtual Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext! context) -> System.Threading.Tasks.Task!

src/Middleware/Diagnostics.EntityFrameworkCore/src/Views/DatabaseErrorPage.Designer.cs

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Middleware/Diagnostics.EntityFrameworkCore/src/Views/DatabaseErrorPage.cshtml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
@functions
1111
{
12-
public DatabaseErrorPageModel Model { get; set; }
12+
public DatabaseErrorPageModel Model { get; set; } = default!;
1313

1414
public string UrlEncode(string content)
1515
{
@@ -34,7 +34,7 @@
3434
<body>
3535
<h1>@Strings.DatabaseErrorPage_Title</h1>
3636
<p>
37-
@for (Exception ex = Model.Exception; ex != null; ex = ex.InnerException)
37+
@for (Exception? ex = Model.Exception; ex != null; ex = ex.InnerException)
3838
{
3939
<span>@ex.GetType().Name: @ex.Message</span>
4040
<br />
@@ -87,7 +87,7 @@
8787
</ul>
8888
8989
<p>
90-
<button id="applyMigrations" onclick="ApplyMigrations()" data-assemblyname="@JavaScriptEncode(context.Type.AssemblyQualifiedName)">@Strings.DatabaseErrorPage_ApplyMigrationsButton</button>
90+
<button id="applyMigrations" onclick="ApplyMigrations()" data-assemblyname="@JavaScriptEncode(context.Type.AssemblyQualifiedName!)">@Strings.DatabaseErrorPage_ApplyMigrationsButton</button>
9191
<span id="applyMigrationsError" class="error"></span>
9292
<span id="applyMigrationsSuccess"></span>
9393
</p>
@@ -115,7 +115,7 @@
115115
};
116116
117117
var formBody = "context=" + encodeURIComponent(document.getElementById('applyMigrations').getAttribute('data-assemblyname'));
118-
req.open("POST", "@JavaScriptEncode(Model.PathBase.Add(Model.Options.MigrationsEndPointPath).Value)", true);
118+
req.open("POST", "@JavaScriptEncode(Model.PathBase.Add(Model.Options.MigrationsEndPointPath).Value!)", true);
119119
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
120120
req.send(formBody);
121121
}

0 commit comments

Comments
 (0)