Skip to content

Commit d9e7bd1

Browse files
authored
Remove IHostApplicationLifetime from ASP.Net Core 2.1 (#83)
* Remove IHostApplicationLifetime from ASP.Net Core 2.1 * Update * update
1 parent 6e85aa1 commit d9e7bd1

17 files changed

Lines changed: 18 additions & 82 deletions

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -598,13 +598,7 @@ and [this](http://www.breachattack.com/#howitworks) for more details.
598598
You may choose to use the .NET Core 2.1 runtime or a .NET Framework runtime.
599599
This library has been tested with .NET Core 2.1 and .NET Framework 4.8.
600600

601-
One additional requirement is that you must add this code in your `Startup.cs` file:
602-
603-
```csharp
604-
services.AddHostApplicationLifetime();
605-
```
606-
607-
You will also need to reference a serializer package such as `GraphQL.NewtonsoftJson`
601+
You will need to reference a serializer package such as `GraphQL.NewtonsoftJson`
608602
or `GraphQL.SystemTextJson`, as `GraphQL.SystemTextJson` is not included in this case.
609603

610604
Besides that requirement, all features are supported in exactly the same manner as

src/GraphQL.AspNetCore3/Compatibility/HostApplicationLifetime.cs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,17 @@ namespace GraphQL.AspNetCore3;
22

33
#if NETSTANDARD2_0 || NETCOREAPP2_1
44

5-
/// <summary>
6-
/// Provides a signal when the application is shutting down.
7-
/// </summary>
8-
public interface IHostApplicationLifetime
9-
{
10-
/// <inheritdoc cref="IHostApplicationLifetime"/>
11-
CancellationToken ApplicationStopping { get; }
12-
}
13-
14-
/// <inheritdoc cref="IHostApplicationLifetime"/>
15-
public class HostApplicationLifetime : IHostApplicationLifetime, IHostedService
16-
{
17-
private readonly CancellationTokenSource _cts = new();
18-
19-
/// <inheritdoc/>
20-
public CancellationToken ApplicationStopping => _cts.Token;
21-
22-
/// <inheritdoc/>
23-
public Task StartAsync(CancellationToken cancellationToken)
24-
=> Task.CompletedTask;
25-
26-
/// <inheritdoc/>
27-
public Task StopAsync(CancellationToken cancellationToken)
28-
{
29-
_cts.Cancel();
30-
return Task.CompletedTask;
31-
}
32-
}
33-
345
/// <summary>
356
/// Extension methods for <see cref="IServiceCollection"/>.
367
/// </summary>
378
public static partial class ServiceCollectionExtensions
389
{
3910
/// <summary>
40-
/// Registers <see cref="IHostApplicationLifetime"/> within the dependency injection framework.
11+
/// Performs no operation.
4112
/// </summary>
13+
[Obsolete("This method has no functionality and will be removed in future versions of this library.")]
4214
public static void AddHostApplicationLifetime(this IServiceCollection services)
4315
{
44-
services.AddSingleton<IHostApplicationLifetime, HostApplicationLifetime>();
45-
services.AddSingleton<IHostedService>(provider => (HostApplicationLifetime)provider.GetRequiredService<IHostApplicationLifetime>());
4616
}
4717
}
4818

src/GraphQL.AspNetCore3/GraphQLHttpMiddleware.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
using System.Security.Claims;
66
using Microsoft.AspNetCore.Authentication;
77
using Microsoft.AspNetCore.Authorization;
8+
#if NETSTANDARD2_0 || NETCOREAPP2_1
9+
using IHostApplicationLifetime = Microsoft.Extensions.Hosting.IApplicationLifetime;
10+
#endif
811

912
namespace GraphQL.AspNetCore3;
1013

src/Samples/Net48Sample/Startup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public void ConfigureServices(IServiceCollection services)
2525
.WithMutation<Chat.Schema.Mutation>()
2626
.WithSubscription<Chat.Schema.Subscription>())
2727
.AddNewtonsoftJson());
28-
services.AddHostApplicationLifetime();
2928
}
3029

3130
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

src/Tests/AuthorizationTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,6 @@ public async Task EndToEnd(bool authenticated)
728728
.AddAuthorizationRule());
729729
services.AddAuthentication();
730730
services.AddAuthorization();
731-
#if NETCOREAPP2_1 || NET48
732-
services.AddHostApplicationLifetime();
733-
#endif
734731
});
735732
hostBuilder.Configure(app => {
736733
app.UseWebSockets();

src/Tests/BuilderMethodTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ public BuilderMethodTests()
1515
.WithSubscription<Chat.Schema.Subscription>())
1616
.AddSchema<Schema2>()
1717
.AddSystemTextJson());
18-
#if NETCOREAPP2_1 || NET48
19-
services.AddHostApplicationLifetime();
20-
#endif
2118
});
2219
}
2320

src/Tests/ChatTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ private IWebHostBuilder ConfigureBuilder()
2222
.WithMutation<Chat.Schema.Mutation>()
2323
.WithSubscription<Chat.Schema.Subscription>())
2424
.AddSystemTextJson());
25-
#if NETCOREAPP2_1 || NET48
26-
services.AddHostApplicationLifetime();
27-
#endif
2825
});
2926
hostBuilder.Configure(app => {
3027
app.UseWebSockets();

src/Tests/JwtBearer/JwtWebSocketAuthenticationServiceTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,6 @@ private TestServer CreateTestServer(bool defaultScheme = true, bool customScheme
255255
.AddSystemTextJson()
256256
.AddJwtBearerAuthentication()
257257
);
258-
#if NET48 || NETCOREAPP2_1
259-
services.AddHostApplicationLifetime();
260-
#endif
261258
})
262259
.Configure(app => {
263260
app.UseWebSockets();

src/Tests/Middleware/AuthorizationTests.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using Microsoft.AspNetCore.Authentication.Cookies;
77
using Microsoft.AspNetCore.Authentication.JwtBearer;
88
using Microsoft.Extensions.Hosting;
9+
#if NET48 || NETCOREAPP2_1
10+
using IHostApplicationLifetime = Microsoft.Extensions.Hosting.IApplicationLifetime;
11+
#endif
912

1013
namespace Tests.Middleware;
1114

@@ -46,9 +49,6 @@ private TestServer CreateServer(Action<IServiceCollection>? configureServices =
4649
policyConfig.RequireRole("FailingRole");
4750
});
4851
});
49-
#if NETCOREAPP2_1 || NET48
50-
services.AddHostApplicationLifetime();
51-
#endif
5252
configureServices?.Invoke(services);
5353
});
5454
hostBuilder.Configure(app => {
@@ -127,9 +127,6 @@ public async Task WebSocket_NotAuthorized()
127127
services.AddGraphQL(b => b
128128
.AddAutoSchema<Chat.Schema.Query>()
129129
.AddSystemTextJson());
130-
#if NETCOREAPP2_1 || NET48
131-
services.AddHostApplicationLifetime();
132-
#endif
133130
});
134131
hostBuilder.Configure(app => {
135132
app.UseWebSockets();

src/Tests/Middleware/BatchTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ public BatchTests()
1919
.AddSchema<Schema2>()
2020
.AddSystemTextJson()
2121
.ConfigureExecutionOptions(o => _configureExecution(o)));
22-
#if NETCOREAPP2_1 || NET48
23-
services.AddHostApplicationLifetime();
24-
#endif
2522
});
2623
hostBuilder.Configure(app => {
2724
app.UseWebSockets();

0 commit comments

Comments
 (0)