Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,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
Expand Down
34 changes: 2 additions & 32 deletions src/GraphQL.AspNetCore3/Compatibility/HostApplicationLifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,17 @@ namespace GraphQL.AspNetCore3;

#if NETSTANDARD2_0 || NETCOREAPP2_1

/// <summary>
/// Provides a signal when the application is shutting down.
/// </summary>
public interface IHostApplicationLifetime
{
/// <inheritdoc cref="IHostApplicationLifetime"/>
CancellationToken ApplicationStopping { get; }
}

/// <inheritdoc cref="IHostApplicationLifetime"/>
public class HostApplicationLifetime : IHostApplicationLifetime, IHostedService
{
private readonly CancellationTokenSource _cts = new();

/// <inheritdoc/>
public CancellationToken ApplicationStopping => _cts.Token;

/// <inheritdoc/>
public Task StartAsync(CancellationToken cancellationToken)
=> Task.CompletedTask;

/// <inheritdoc/>
public Task StopAsync(CancellationToken cancellationToken)
{
_cts.Cancel();
return Task.CompletedTask;
}
}

/// <summary>
/// Extension methods for <see cref="IServiceCollection"/>.
/// </summary>
public static partial class ServiceCollectionExtensions
{
/// <summary>
/// Registers <see cref="IHostApplicationLifetime"/> within the dependency injection framework.
/// Performs no operation.
/// </summary>
[Obsolete("This method has no functionality and will be removed in future versions of this library.")]
public static void AddHostApplicationLifetime(this IServiceCollection services)
{
services.AddSingleton<IHostApplicationLifetime, HostApplicationLifetime>();
services.AddSingleton<IHostedService>(provider => (HostApplicationLifetime)provider.GetRequiredService<IHostApplicationLifetime>());
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/GraphQL.AspNetCore3/GraphQLHttpMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
#if NETSTANDARD2_0 || NETCOREAPP2_1
using IHostApplicationLifetime = Microsoft.Extensions.Hosting.IApplicationLifetime;
#endif

namespace GraphQL.AspNetCore3;

Expand Down
1 change: 0 additions & 1 deletion src/Samples/Net48Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public void ConfigureServices(IServiceCollection services)
.WithMutation<Chat.Schema.Mutation>()
.WithSubscription<Chat.Schema.Subscription>())
.AddNewtonsoftJson());
services.AddHostApplicationLifetime();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
3 changes: 0 additions & 3 deletions src/Tests/AuthorizationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions src/Tests/BuilderMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ public BuilderMethodTests()
.WithSubscription<Chat.Schema.Subscription>())
.AddSchema<Schema2>()
.AddSystemTextJson());
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
});
}

Expand Down
3 changes: 0 additions & 3 deletions src/Tests/ChatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ private IWebHostBuilder ConfigureBuilder()
.WithMutation<Chat.Schema.Mutation>()
.WithSubscription<Chat.Schema.Subscription>())
.AddSystemTextJson());
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
});
hostBuilder.Configure(app => {
app.UseWebSockets();
Expand Down
3 changes: 0 additions & 3 deletions src/Tests/JwtBearer/JwtWebSocketAuthenticationServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 3 additions & 6 deletions src/Tests/Middleware/AuthorizationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -46,9 +49,6 @@ private TestServer CreateServer(Action<IServiceCollection>? configureServices =
policyConfig.RequireRole("FailingRole");
});
});
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
configureServices?.Invoke(services);
});
hostBuilder.Configure(app => {
Expand Down Expand Up @@ -127,9 +127,6 @@ public async Task WebSocket_NotAuthorized()
services.AddGraphQL(b => b
.AddAutoSchema<Chat.Schema.Query>()
.AddSystemTextJson());
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
});
hostBuilder.Configure(app => {
app.UseWebSockets();
Expand Down
3 changes: 0 additions & 3 deletions src/Tests/Middleware/BatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public BatchTests()
.AddSchema<Schema2>()
.AddSystemTextJson()
.ConfigureExecutionOptions(o => _configureExecution(o)));
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
});
hostBuilder.Configure(app => {
app.UseWebSockets();
Expand Down
3 changes: 0 additions & 3 deletions src/Tests/Middleware/Cors/MiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ public async Task<CorsResponse> ExecuteMiddleware(
.AddAutoSchema<Query>()
.AddSystemTextJson());
services.AddCors();
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
});
hostBuilder.Configure(app => {
if (configureCorsPolicy != null)
Expand Down
6 changes: 3 additions & 3 deletions src/Tests/Middleware/FileUploadTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -15,9 +18,6 @@ public FileUploadTests()
services.AddGraphQL(b => b
.AddSchema<MySchema>()
.AddSystemTextJson());
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
});
hostBuilder.Configure(app => {
app.UseWebSockets();
Expand Down
6 changes: 0 additions & 6 deletions src/Tests/Middleware/GetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ public GetTests()
});
});
services.AddSingleton<PersistedDocumentHandler>();
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
});
hostBuilder.Configure(app => {
app.UseWebSockets();
Expand Down Expand Up @@ -142,9 +139,6 @@ public async Task NoUseWebSockets()
services.AddGraphQL(b => b
.AddAutoSchema<Chat.Schema.Query>()
.AddSystemTextJson());
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
});
hostBuilder.Configure(app => app.UseGraphQL());
using var server = new TestServer(hostBuilder);
Expand Down
3 changes: 3 additions & 0 deletions src/Tests/Middleware/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 0 additions & 3 deletions src/Tests/Middleware/PostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ public PostTests()
});
});
services.AddSingleton<PersistedDocumentHandler>();
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
});
hostBuilder.Configure(app => {
app.UseWebSockets();
Expand Down
6 changes: 3 additions & 3 deletions src/Tests/Middleware/WebSocketTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -21,9 +24,6 @@ private void Configure(Action<GraphQLHttpMiddlewareOptions>? configureOptions =
.WithSubscription<Chat.Schema.Subscription>())
.AddSchema<Schema2>()
.AddSystemTextJson());
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
configureServices(services);
});
hostBuilder.Configure(app => {
Expand Down
3 changes: 0 additions & 3 deletions src/Tests/UserContextBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ private void Configure(Action<IGraphQLBuilder> configureBuilder)
b.AddSystemTextJson();
});
services.AddHttpContextAccessor();
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
});
hostBuilder.Configure(app => {
app.UseWebSockets();
Expand Down