From 39502aeb17f4187b81344f75d225b3d80667331d Mon Sep 17 00:00:00 2001 From: Shane32 Date: Sun, 1 Dec 2024 14:57:53 -0500 Subject: [PATCH 1/3] Remove IHostApplicationLifetime from ASP.Net Core 2.1 --- README.md | 8 +-- .../Compatibility/HostApplicationLifetime.cs | 49 ------------------- .../GraphQLHttpMiddleware.cs | 3 ++ src/Samples/Net48Sample/Startup.cs | 1 - src/Tests/AuthorizationTests.cs | 3 -- src/Tests/BuilderMethodTests.cs | 3 -- src/Tests/ChatTests.cs | 3 -- src/Tests/Middleware/AuthorizationTests.cs | 9 ++-- src/Tests/Middleware/BatchTests.cs | 3 -- src/Tests/Middleware/Cors/MiddlewareTests.cs | 3 -- src/Tests/Middleware/FileUploadTests.cs | 6 +-- src/Tests/Middleware/GetTests.cs | 6 --- src/Tests/Middleware/MiscTests.cs | 3 ++ src/Tests/Middleware/PostTests.cs | 3 -- src/Tests/Middleware/WebSocketTests.cs | 6 +-- src/Tests/UserContextBuilderTests.cs | 3 -- 16 files changed, 16 insertions(+), 96 deletions(-) delete mode 100644 src/GraphQL.AspNetCore3/Compatibility/HostApplicationLifetime.cs diff --git a/README.md b/README.md index 4ec3f36..5a5c479 100644 --- a/README.md +++ b/README.md @@ -566,13 +566,7 @@ and [this](http://www.breachattack.com/#howitworks) for more details. You may choose to use the .NET Core 2.1 runtime or a .NET Framework runtime. This library has been tested with .NET Core 2.1 and .NET Framework 4.8. -One additional requirement is that you must add this code in your `Startup.cs` file: - -```csharp -services.AddHostApplicationLifetime(); -``` - -You will also need to reference a serializer package such as `GraphQL.NewtonsoftJson` +You will need to reference a serializer package such as `GraphQL.NewtonsoftJson` or `GraphQL.SystemTextJson`, as `GraphQL.SystemTextJson` is not included in this case. Besides that requirement, all features are supported in exactly the same manner as diff --git a/src/GraphQL.AspNetCore3/Compatibility/HostApplicationLifetime.cs b/src/GraphQL.AspNetCore3/Compatibility/HostApplicationLifetime.cs deleted file mode 100644 index beeebee..0000000 --- a/src/GraphQL.AspNetCore3/Compatibility/HostApplicationLifetime.cs +++ /dev/null @@ -1,49 +0,0 @@ -namespace GraphQL.AspNetCore3; - -#if NETSTANDARD2_0 || NETCOREAPP2_1 - -/// -/// Provides a signal when the application is shutting down. -/// -public interface IHostApplicationLifetime -{ - /// - CancellationToken ApplicationStopping { get; } -} - -/// -public class HostApplicationLifetime : IHostApplicationLifetime, IHostedService -{ - private readonly CancellationTokenSource _cts = new(); - - /// - public CancellationToken ApplicationStopping => _cts.Token; - - /// - public Task StartAsync(CancellationToken cancellationToken) - => Task.CompletedTask; - - /// - public Task StopAsync(CancellationToken cancellationToken) - { - _cts.Cancel(); - return Task.CompletedTask; - } -} - -/// -/// Extension methods for . -/// -public static partial class ServiceCollectionExtensions -{ - /// - /// Registers within the dependency injection framework. - /// - public static void AddHostApplicationLifetime(this IServiceCollection services) - { - services.AddSingleton(); - services.AddSingleton(provider => (HostApplicationLifetime)provider.GetRequiredService()); - } -} - -#endif diff --git a/src/GraphQL.AspNetCore3/GraphQLHttpMiddleware.cs b/src/GraphQL.AspNetCore3/GraphQLHttpMiddleware.cs index 35b6747..aff4e2c 100644 --- a/src/GraphQL.AspNetCore3/GraphQLHttpMiddleware.cs +++ b/src/GraphQL.AspNetCore3/GraphQLHttpMiddleware.cs @@ -6,6 +6,9 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using static System.Net.Mime.MediaTypeNames; +#if NETSTANDARD2_0 || NETCOREAPP2_1 +using IHostApplicationLifetime = Microsoft.Extensions.Hosting.IApplicationLifetime; +#endif namespace GraphQL.AspNetCore3; diff --git a/src/Samples/Net48Sample/Startup.cs b/src/Samples/Net48Sample/Startup.cs index 0117d6c..873d8ac 100644 --- a/src/Samples/Net48Sample/Startup.cs +++ b/src/Samples/Net48Sample/Startup.cs @@ -25,7 +25,6 @@ public void ConfigureServices(IServiceCollection services) .WithMutation() .WithSubscription()) .AddNewtonsoftJson()); - services.AddHostApplicationLifetime(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/Tests/AuthorizationTests.cs b/src/Tests/AuthorizationTests.cs index 33b7326..068e265 100644 --- a/src/Tests/AuthorizationTests.cs +++ b/src/Tests/AuthorizationTests.cs @@ -728,9 +728,6 @@ public async Task EndToEnd(bool authenticated) .AddAuthorizationRule()); services.AddAuthentication(); services.AddAuthorization(); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); hostBuilder.Configure(app => { app.UseWebSockets(); diff --git a/src/Tests/BuilderMethodTests.cs b/src/Tests/BuilderMethodTests.cs index c0cd92b..2427819 100644 --- a/src/Tests/BuilderMethodTests.cs +++ b/src/Tests/BuilderMethodTests.cs @@ -15,9 +15,6 @@ public BuilderMethodTests() .WithSubscription()) .AddSchema() .AddSystemTextJson()); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); } diff --git a/src/Tests/ChatTests.cs b/src/Tests/ChatTests.cs index 3cfcfc6..e2a08b4 100644 --- a/src/Tests/ChatTests.cs +++ b/src/Tests/ChatTests.cs @@ -22,9 +22,6 @@ private IWebHostBuilder ConfigureBuilder() .WithMutation() .WithSubscription()) .AddSystemTextJson()); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); hostBuilder.Configure(app => { app.UseWebSockets(); diff --git a/src/Tests/Middleware/AuthorizationTests.cs b/src/Tests/Middleware/AuthorizationTests.cs index d94036e..0f7f9dd 100644 --- a/src/Tests/Middleware/AuthorizationTests.cs +++ b/src/Tests/Middleware/AuthorizationTests.cs @@ -6,6 +6,9 @@ using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.Hosting; +#if NET48 || NETCOREAPP2_1 +using IHostApplicationLifetime = Microsoft.Extensions.Hosting.IApplicationLifetime; +#endif namespace Tests.Middleware; @@ -46,9 +49,6 @@ private TestServer CreateServer(Action? configureServices = policyConfig.RequireRole("FailingRole"); }); }); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif configureServices?.Invoke(services); }); hostBuilder.Configure(app => { @@ -127,9 +127,6 @@ public async Task WebSocket_NotAuthorized() services.AddGraphQL(b => b .AddAutoSchema() .AddSystemTextJson()); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); hostBuilder.Configure(app => { app.UseWebSockets(); diff --git a/src/Tests/Middleware/BatchTests.cs b/src/Tests/Middleware/BatchTests.cs index 1fb3a2c..0a02584 100644 --- a/src/Tests/Middleware/BatchTests.cs +++ b/src/Tests/Middleware/BatchTests.cs @@ -19,9 +19,6 @@ public BatchTests() .AddSchema() .AddSystemTextJson() .ConfigureExecutionOptions(o => _configureExecution(o))); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); hostBuilder.Configure(app => { app.UseWebSockets(); diff --git a/src/Tests/Middleware/Cors/MiddlewareTests.cs b/src/Tests/Middleware/Cors/MiddlewareTests.cs index e31b882..7330770 100644 --- a/src/Tests/Middleware/Cors/MiddlewareTests.cs +++ b/src/Tests/Middleware/Cors/MiddlewareTests.cs @@ -18,9 +18,6 @@ public async Task ExecuteMiddleware( .AddAutoSchema() .AddSystemTextJson()); services.AddCors(); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); hostBuilder.Configure(app => { if (configureCorsPolicy != null) diff --git a/src/Tests/Middleware/FileUploadTests.cs b/src/Tests/Middleware/FileUploadTests.cs index 51941c9..fbc1e96 100644 --- a/src/Tests/Middleware/FileUploadTests.cs +++ b/src/Tests/Middleware/FileUploadTests.cs @@ -1,5 +1,8 @@ using GraphQLParser.AST; using Microsoft.Extensions.Hosting; +#if NET48 || NETCOREAPP2_1 +using IHostApplicationLifetime = Microsoft.Extensions.Hosting.IApplicationLifetime; +#endif namespace Tests.Middleware; @@ -15,9 +18,6 @@ public FileUploadTests() services.AddGraphQL(b => b .AddSchema() .AddSystemTextJson()); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); hostBuilder.Configure(app => { app.UseWebSockets(); diff --git a/src/Tests/Middleware/GetTests.cs b/src/Tests/Middleware/GetTests.cs index d5aa2fe..e387663 100644 --- a/src/Tests/Middleware/GetTests.cs +++ b/src/Tests/Middleware/GetTests.cs @@ -41,9 +41,6 @@ public GetTests() }); }); services.AddSingleton(); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); hostBuilder.Configure(app => { app.UseWebSockets(); @@ -142,9 +139,6 @@ public async Task NoUseWebSockets() services.AddGraphQL(b => b .AddAutoSchema() .AddSystemTextJson()); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); hostBuilder.Configure(app => app.UseGraphQL()); using var server = new TestServer(hostBuilder); diff --git a/src/Tests/Middleware/MiscTests.cs b/src/Tests/Middleware/MiscTests.cs index c6c50ca..02867cb 100644 --- a/src/Tests/Middleware/MiscTests.cs +++ b/src/Tests/Middleware/MiscTests.cs @@ -2,6 +2,9 @@ using GraphQL.AspNetCore3.Errors; using GraphQL.Execution; using Microsoft.Extensions.Hosting; +#if NET48 || NETCOREAPP2_1 +using IHostApplicationLifetime = Microsoft.Extensions.Hosting.IApplicationLifetime; +#endif namespace Tests.Middleware; diff --git a/src/Tests/Middleware/PostTests.cs b/src/Tests/Middleware/PostTests.cs index 8d78cc0..909508a 100644 --- a/src/Tests/Middleware/PostTests.cs +++ b/src/Tests/Middleware/PostTests.cs @@ -43,9 +43,6 @@ public PostTests() }); }); services.AddSingleton(); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); hostBuilder.Configure(app => { app.UseWebSockets(); diff --git a/src/Tests/Middleware/WebSocketTests.cs b/src/Tests/Middleware/WebSocketTests.cs index 90123e0..a57b795 100644 --- a/src/Tests/Middleware/WebSocketTests.cs +++ b/src/Tests/Middleware/WebSocketTests.cs @@ -1,5 +1,8 @@ using System.Net; using Microsoft.Extensions.Hosting; +#if NET48 || NETCOREAPP2_1 +using IHostApplicationLifetime = Microsoft.Extensions.Hosting.IApplicationLifetime; +#endif namespace Tests.Middleware; @@ -21,9 +24,6 @@ private void Configure(Action? configureOptions = .WithSubscription()) .AddSchema() .AddSystemTextJson()); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif configureServices(services); }); hostBuilder.Configure(app => { diff --git a/src/Tests/UserContextBuilderTests.cs b/src/Tests/UserContextBuilderTests.cs index 756d910..1dca235 100644 --- a/src/Tests/UserContextBuilderTests.cs +++ b/src/Tests/UserContextBuilderTests.cs @@ -16,9 +16,6 @@ private void Configure(Action configureBuilder) b.AddSystemTextJson(); }); services.AddHttpContextAccessor(); -#if NETCOREAPP2_1 || NET48 - services.AddHostApplicationLifetime(); -#endif }); hostBuilder.Configure(app => { app.UseWebSockets(); From 40d9a6fb17d5f88e0ce4945c2bf9835a17a7ef8e Mon Sep 17 00:00:00 2001 From: Shane32 Date: Sun, 1 Dec 2024 16:07:33 -0500 Subject: [PATCH 2/3] Update --- .../Compatibility/HostApplicationLifetime.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/GraphQL.AspNetCore3/Compatibility/HostApplicationLifetime.cs diff --git a/src/GraphQL.AspNetCore3/Compatibility/HostApplicationLifetime.cs b/src/GraphQL.AspNetCore3/Compatibility/HostApplicationLifetime.cs new file mode 100644 index 0000000..0659eef --- /dev/null +++ b/src/GraphQL.AspNetCore3/Compatibility/HostApplicationLifetime.cs @@ -0,0 +1,19 @@ +namespace GraphQL.AspNetCore3; + +#if NETSTANDARD2_0 || NETCOREAPP2_1 + +/// +/// Extension methods for . +/// +public static partial class ServiceCollectionExtensions +{ + /// + /// Performs no operation. + /// + [Obsolete("This method has no functionality and will be removed in future versions of this library.")] + public static void AddHostApplicationLifetime(this IServiceCollection services) + { + } +} + +#endif From 9f3361c3931a1d9d6f5053e9dcbd80a54f7d883d Mon Sep 17 00:00:00 2001 From: Shane32 Date: Sun, 1 Dec 2024 16:11:11 -0500 Subject: [PATCH 3/3] update --- src/Tests/JwtBearer/JwtWebSocketAuthenticationServiceTests.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Tests/JwtBearer/JwtWebSocketAuthenticationServiceTests.cs b/src/Tests/JwtBearer/JwtWebSocketAuthenticationServiceTests.cs index 8832450..f71f1be 100644 --- a/src/Tests/JwtBearer/JwtWebSocketAuthenticationServiceTests.cs +++ b/src/Tests/JwtBearer/JwtWebSocketAuthenticationServiceTests.cs @@ -255,9 +255,6 @@ private TestServer CreateTestServer(bool defaultScheme = true, bool customScheme .AddSystemTextJson() .AddJwtBearerAuthentication() ); -#if NET48 || NETCOREAPP2_1 - services.AddHostApplicationLifetime(); -#endif }) .Configure(app => { app.UseWebSockets();