Skip to content

Simplified signature of app.UseJsonApi() #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2020
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: 5 additions & 3 deletions docs/getting-started/step-by-step.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,15 @@ public void ConfigureServices(IServiceCollection services)
}
```

Add the middleware to the Startup.Configure method. Note that under the hood,
this will call `app.UseRouting()` and `app.UseEndpoints(...)` so there is no need to add that as well.
Add the middleware to the Startup.Configure method.

```c#
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseJsonApi();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
```

Expand All @@ -125,7 +126,9 @@ public void Configure(IApplicationBuilder app, AppDbContext context)
context.SaveChanges();
}

app.UseRouting();
app.UseJsonApi();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
```

Expand All @@ -135,4 +138,3 @@ public void Configure(IApplicationBuilder app, AppDbContext context)
dotnet run
curl http://localhost:5000/people
```

5 changes: 3 additions & 2 deletions docs/usage/extensibility/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ Add the following to your Startup.ConfigureServices method. Replace AppDbContext
services.AddJsonApi<AppDbContext>();
```

Add the middleware to the Startup.Configure method. Note that under the hood,
this will call `app.UseRouting()` and `app.UseEndpoints(...)` so there is no need to add that as well.
Add the middleware to the Startup.Configure method.

```c3
app.UseRouting();
app.UseJsonApi();
app.UseEndpoints(endpoints => endpoints.MapControllers());
```
6 changes: 4 additions & 2 deletions src/Examples/GettingStarted/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace GettingStarted
{
public sealed class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<SampleDbContext>(
Expand All @@ -19,14 +19,16 @@ public void ConfigureServices(IServiceCollection services)
options => options.Namespace = "api");
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, SampleDbContext context)
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
CreateSampleData(context);

app.UseRouting();
app.UseJsonApi();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}

private static void CreateSampleData(SampleDbContext context)
Expand Down
3 changes: 3 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public void Configure(
AppDbContext context)
{
context.Database.EnsureCreated();

app.UseRouting();
app.UseJsonApi();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
}
}
2 changes: 2 additions & 0 deletions src/Examples/NoEntityFrameworkExample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public void Configure(IApplicationBuilder app, AppDbContext context)
{
context.Database.EnsureCreated();

app.UseRouting();
app.UseJsonApi();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
}
}
22 changes: 14 additions & 8 deletions src/JsonApiDotNetCore/Builders/JsonApiApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,22 @@ public void ConfigureMvc(Type dbContextType)
{
RegisterJsonApiStartupServices();

var intermediateProvider = _services.BuildServiceProvider();
_resourceGraphBuilder = intermediateProvider.GetRequiredService<IResourceGraphBuilder>();
_serviceDiscoveryFacade = intermediateProvider.GetRequiredService<IServiceDiscoveryFacade>();
_dbContextType = dbContextType;
IJsonApiExceptionFilterProvider exceptionFilterProvider;
IJsonApiTypeMatchFilterProvider typeMatchFilterProvider;
IJsonApiRoutingConvention routingConvention;

AddResourceTypesFromDbContext(intermediateProvider);
using (var intermediateProvider = _services.BuildServiceProvider())
{
_resourceGraphBuilder = intermediateProvider.GetRequiredService<IResourceGraphBuilder>();
_serviceDiscoveryFacade = intermediateProvider.GetRequiredService<IServiceDiscoveryFacade>();
_dbContextType = dbContextType;

AddResourceTypesFromDbContext(intermediateProvider);

var exceptionFilterProvider = intermediateProvider.GetRequiredService<IJsonApiExceptionFilterProvider>();
var typeMatchFilterProvider = intermediateProvider.GetRequiredService<IJsonApiTypeMatchFilterProvider>();
var routingConvention = intermediateProvider.GetRequiredService<IJsonApiRoutingConvention>();
exceptionFilterProvider = intermediateProvider.GetRequiredService<IJsonApiExceptionFilterProvider>();
typeMatchFilterProvider = intermediateProvider.GetRequiredService<IJsonApiTypeMatchFilterProvider>();
routingConvention = intermediateProvider.GetRequiredService<IJsonApiRoutingConvention>();
}

_mvcBuilder.AddMvcOptions(options =>
{
Expand Down
53 changes: 9 additions & 44 deletions src/JsonApiDotNetCore/Extensions/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,26 @@
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Middleware;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace JsonApiDotNetCore
{
public static class ApplicationBuilderExtensions
{
/// <summary>
/// Validates the resource graph and optionally registers the JsonApiDotNetCore middleware.
/// Registers the JsonApiDotNetCore middleware.
/// </summary>
/// <remarks>
/// The <paramref name="skipRegisterMiddleware"/> can be used to skip any middleware registration, in which case the developer
/// is responsible for registering required middleware.
/// </remarks>
/// <param name="app"></param>
/// <param name="skipRegisterMiddleware">Indicates to not register any middleware. This enables callers to take full control of middleware registration order.</param>
/// <param name="useAuthentication">Indicates if 'app.UseAuthentication()' should be called. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
/// <param name="useAuthorization">Indicates if 'app.UseAuthorization()' should be called. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
/// <param name="builder">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <example>
/// The next example illustrates how to manually register middleware.
/// <code>
/// app.UseJsonApi(skipRegisterMiddleware: true);
/// The code below is the minimal that is required for proper activation,
/// which should be added to your Startup.Configure method.
/// <code><![CDATA[
/// app.UseRouting();
/// app.UseMiddleware{JsonApiMiddleware}();
/// app.UseJsonApi();
/// app.UseEndpoints(endpoints => endpoints.MapControllers());
/// </code>
/// ]]></code>
/// </example>
public static void UseJsonApi(this IApplicationBuilder app, bool skipRegisterMiddleware = false, bool useAuthentication = false, bool useAuthorization = false)
public static void UseJsonApi(this IApplicationBuilder builder)
{
using (var scope = app.ApplicationServices.CreateScope())
{
var inverseRelationshipResolver = scope.ServiceProvider.GetService<IInverseRelationships>();
inverseRelationshipResolver?.Resolve();
}

if (!skipRegisterMiddleware)
{
// An endpoint is selected and set on the HttpContext if a match is found
app.UseRouting();

if (useAuthentication)
{
app.UseAuthentication();
}

if (useAuthorization)
{
app.UseAuthorization();
}

// middleware to run after routing occurs.
app.UseMiddleware<JsonApiMiddleware>();

// Executes the endpoints that was selected by routing.
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
builder.UseMiddleware<JsonApiMiddleware>();
}
}
}
13 changes: 13 additions & 0 deletions src/JsonApiDotNetCore/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static IServiceCollection AddJsonApi(this IServiceCollection services,
IMvcCoreBuilder mvcBuilder = null)
{
SetupApplicationBuilder(services, options, discovery, resources, mvcBuilder, null);
ResolveInverseRelationships(services);

return services;
}

Expand All @@ -40,6 +42,8 @@ public static IServiceCollection AddJsonApi<TDbContext>(this IServiceCollection
where TDbContext : DbContext
{
SetupApplicationBuilder(services, options, discovery, resources, mvcBuilder, typeof(TDbContext));
ResolveInverseRelationships(services);

return services;
}

Expand All @@ -56,6 +60,15 @@ private static void SetupApplicationBuilder(IServiceCollection services, Action<
applicationBuilder.ConfigureServices();
}

private static void ResolveInverseRelationships(IServiceCollection services)
{
using var intermediateProvider = services.BuildServiceProvider();
using var scope = intermediateProvider.CreateScope();

var inverseRelationshipResolver = scope.ServiceProvider.GetService<IInverseRelationships>();
inverseRelationshipResolver?.Resolve();
}

/// <summary>
/// Enables client serializers for sending requests and receiving responses
/// in json:api format. Internally only used for testing.
Expand Down