Skip to content

Commit 4f6b1cd

Browse files
author
Bart Koelman
authored
Merge pull request #750 from bart-degreed/use-jsonapi
Simplified signature of app.UseJsonApi()
2 parents ccae41a + 833fb6d commit 4f6b1cd

File tree

8 files changed

+53
-59
lines changed

8 files changed

+53
-59
lines changed

docs/getting-started/step-by-step.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,15 @@ public void ConfigureServices(IServiceCollection services)
9696
}
9797
```
9898

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

102101
```c#
103102
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
104103
public void Configure(IApplicationBuilder app)
105104
{
105+
app.UseRouting();
106106
app.UseJsonApi();
107+
app.UseEndpoints(endpoints => endpoints.MapControllers());
107108
}
108109
```
109110

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

129+
app.UseRouting();
128130
app.UseJsonApi();
131+
app.UseEndpoints(endpoints => endpoints.MapControllers());
129132
}
130133
```
131134

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

docs/usage/extensibility/middleware.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ Add the following to your Startup.ConfigureServices method. Replace AppDbContext
55
services.AddJsonApi<AppDbContext>();
66
```
77

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

1110
```c3
11+
app.UseRouting();
1212
app.UseJsonApi();
13+
app.UseEndpoints(endpoints => endpoints.MapControllers());
1314
```

src/Examples/GettingStarted/Startup.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace GettingStarted
99
{
1010
public sealed class Startup
1111
{
12-
// This method gets called by the runtime. Use this method to add services to the container.
12+
// This method gets called by the runtime. Use this method to add services to the container.
1313
public void ConfigureServices(IServiceCollection services)
1414
{
1515
services.AddDbContext<SampleDbContext>(
@@ -19,14 +19,16 @@ public void ConfigureServices(IServiceCollection services)
1919
options => options.Namespace = "api");
2020
}
2121

22-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
22+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2323
public void Configure(IApplicationBuilder app, SampleDbContext context)
2424
{
2525
context.Database.EnsureDeleted();
2626
context.Database.EnsureCreated();
2727
CreateSampleData(context);
2828

29+
app.UseRouting();
2930
app.UseJsonApi();
31+
app.UseEndpoints(endpoints => endpoints.MapControllers());
3032
}
3133

3234
private static void CreateSampleData(SampleDbContext context)

src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ public void Configure(
7373
AppDbContext context)
7474
{
7575
context.Database.EnsureCreated();
76+
77+
app.UseRouting();
7678
app.UseJsonApi();
79+
app.UseEndpoints(endpoints => endpoints.MapControllers());
7780
}
7881
}
7982
}

src/Examples/NoEntityFrameworkExample/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public void Configure(IApplicationBuilder app, AppDbContext context)
4343
{
4444
context.Database.EnsureCreated();
4545

46+
app.UseRouting();
4647
app.UseJsonApi();
48+
app.UseEndpoints(endpoints => endpoints.MapControllers());
4749
}
4850
}
4951
}

src/JsonApiDotNetCore/Builders/JsonApiApplicationBuilder.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,22 @@ public void ConfigureMvc(Type dbContextType)
6262
{
6363
RegisterJsonApiStartupServices();
6464

65-
var intermediateProvider = _services.BuildServiceProvider();
66-
_resourceGraphBuilder = intermediateProvider.GetRequiredService<IResourceGraphBuilder>();
67-
_serviceDiscoveryFacade = intermediateProvider.GetRequiredService<IServiceDiscoveryFacade>();
68-
_dbContextType = dbContextType;
65+
IJsonApiExceptionFilterProvider exceptionFilterProvider;
66+
IJsonApiTypeMatchFilterProvider typeMatchFilterProvider;
67+
IJsonApiRoutingConvention routingConvention;
6968

70-
AddResourceTypesFromDbContext(intermediateProvider);
69+
using (var intermediateProvider = _services.BuildServiceProvider())
70+
{
71+
_resourceGraphBuilder = intermediateProvider.GetRequiredService<IResourceGraphBuilder>();
72+
_serviceDiscoveryFacade = intermediateProvider.GetRequiredService<IServiceDiscoveryFacade>();
73+
_dbContextType = dbContextType;
74+
75+
AddResourceTypesFromDbContext(intermediateProvider);
7176

72-
var exceptionFilterProvider = intermediateProvider.GetRequiredService<IJsonApiExceptionFilterProvider>();
73-
var typeMatchFilterProvider = intermediateProvider.GetRequiredService<IJsonApiTypeMatchFilterProvider>();
74-
var routingConvention = intermediateProvider.GetRequiredService<IJsonApiRoutingConvention>();
77+
exceptionFilterProvider = intermediateProvider.GetRequiredService<IJsonApiExceptionFilterProvider>();
78+
typeMatchFilterProvider = intermediateProvider.GetRequiredService<IJsonApiTypeMatchFilterProvider>();
79+
routingConvention = intermediateProvider.GetRequiredService<IJsonApiRoutingConvention>();
80+
}
7581

7682
_mvcBuilder.AddMvcOptions(options =>
7783
{
Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,26 @@
1-
using JsonApiDotNetCore.Internal;
21
using JsonApiDotNetCore.Middleware;
32
using Microsoft.AspNetCore.Builder;
4-
using Microsoft.Extensions.DependencyInjection;
53

64
namespace JsonApiDotNetCore
75
{
86
public static class ApplicationBuilderExtensions
97
{
108
/// <summary>
11-
/// Validates the resource graph and optionally registers the JsonApiDotNetCore middleware.
9+
/// Registers the JsonApiDotNetCore middleware.
1210
/// </summary>
13-
/// <remarks>
14-
/// The <paramref name="skipRegisterMiddleware"/> can be used to skip any middleware registration, in which case the developer
15-
/// is responsible for registering required middleware.
16-
/// </remarks>
17-
/// <param name="app"></param>
18-
/// <param name="skipRegisterMiddleware">Indicates to not register any middleware. This enables callers to take full control of middleware registration order.</param>
19-
/// <param name="useAuthentication">Indicates if 'app.UseAuthentication()' should be called. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
20-
/// <param name="useAuthorization">Indicates if 'app.UseAuthorization()' should be called. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
11+
/// <param name="builder">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
2112
/// <example>
22-
/// The next example illustrates how to manually register middleware.
23-
/// <code>
24-
/// app.UseJsonApi(skipRegisterMiddleware: true);
13+
/// The code below is the minimal that is required for proper activation,
14+
/// which should be added to your Startup.Configure method.
15+
/// <code><![CDATA[
2516
/// app.UseRouting();
26-
/// app.UseMiddleware{JsonApiMiddleware}();
17+
/// app.UseJsonApi();
2718
/// app.UseEndpoints(endpoints => endpoints.MapControllers());
28-
/// </code>
19+
/// ]]></code>
2920
/// </example>
30-
public static void UseJsonApi(this IApplicationBuilder app, bool skipRegisterMiddleware = false, bool useAuthentication = false, bool useAuthorization = false)
21+
public static void UseJsonApi(this IApplicationBuilder builder)
3122
{
32-
using (var scope = app.ApplicationServices.CreateScope())
33-
{
34-
var inverseRelationshipResolver = scope.ServiceProvider.GetService<IInverseRelationships>();
35-
inverseRelationshipResolver?.Resolve();
36-
}
37-
38-
if (!skipRegisterMiddleware)
39-
{
40-
// An endpoint is selected and set on the HttpContext if a match is found
41-
app.UseRouting();
42-
43-
if (useAuthentication)
44-
{
45-
app.UseAuthentication();
46-
}
47-
48-
if (useAuthorization)
49-
{
50-
app.UseAuthorization();
51-
}
52-
53-
// middleware to run after routing occurs.
54-
app.UseMiddleware<JsonApiMiddleware>();
55-
56-
// Executes the endpoints that was selected by routing.
57-
app.UseEndpoints(endpoints => endpoints.MapControllers());
58-
}
23+
builder.UseMiddleware<JsonApiMiddleware>();
5924
}
6025
}
6126
}

src/JsonApiDotNetCore/Extensions/ServiceCollectionExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public static IServiceCollection AddJsonApi(this IServiceCollection services,
2626
IMvcCoreBuilder mvcBuilder = null)
2727
{
2828
SetupApplicationBuilder(services, options, discovery, resources, mvcBuilder, null);
29+
ResolveInverseRelationships(services);
30+
2931
return services;
3032
}
3133

@@ -40,6 +42,8 @@ public static IServiceCollection AddJsonApi<TDbContext>(this IServiceCollection
4042
where TDbContext : DbContext
4143
{
4244
SetupApplicationBuilder(services, options, discovery, resources, mvcBuilder, typeof(TDbContext));
45+
ResolveInverseRelationships(services);
46+
4347
return services;
4448
}
4549

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

63+
private static void ResolveInverseRelationships(IServiceCollection services)
64+
{
65+
using var intermediateProvider = services.BuildServiceProvider();
66+
using var scope = intermediateProvider.CreateScope();
67+
68+
var inverseRelationshipResolver = scope.ServiceProvider.GetService<IInverseRelationships>();
69+
inverseRelationshipResolver?.Resolve();
70+
}
71+
5972
/// <summary>
6073
/// Enables client serializers for sending requests and receiving responses
6174
/// in json:api format. Internally only used for testing.

0 commit comments

Comments
 (0)