3
3
using JsonApiDotNetCore . Configuration ;
4
4
using JsonApiDotNetCore . Data ;
5
5
using JsonApiDotNetCore . Formatters ;
6
+ using JsonApiDotNetCore . Graph ;
6
7
using JsonApiDotNetCore . Internal ;
7
8
using JsonApiDotNetCore . Internal . Generics ;
8
9
using JsonApiDotNetCore . Middleware ;
@@ -34,18 +35,18 @@ public static IServiceCollection AddJsonApi<TContext>(this IServiceCollection se
34
35
return AddJsonApi < TContext > ( services , options , mvcBuilder ) ;
35
36
}
36
37
37
- public static IServiceCollection AddJsonApi < TContext > ( this IServiceCollection services ,
38
- Action < JsonApiOptions > options ,
39
- IMvcCoreBuilder mvcBuilder ) where TContext : DbContext
38
+ public static IServiceCollection AddJsonApi < TContext > (
39
+ this IServiceCollection services ,
40
+ Action < JsonApiOptions > options ,
41
+ IMvcCoreBuilder mvcBuilder ) where TContext : DbContext
40
42
{
41
43
var config = new JsonApiOptions ( ) ;
42
44
43
45
options ( config ) ;
44
46
45
47
config . BuildContextGraph ( builder => builder . AddDbContext < TContext > ( ) ) ;
46
48
47
- mvcBuilder
48
- . AddMvcOptions ( opt =>
49
+ mvcBuilder . AddMvcOptions ( opt =>
49
50
{
50
51
opt . Filters . Add ( typeof ( JsonApiExceptionFilter ) ) ;
51
52
opt . SerializeAsJsonApi ( config ) ;
@@ -55,22 +56,28 @@ public static IServiceCollection AddJsonApi<TContext>(this IServiceCollection se
55
56
return services ;
56
57
}
57
58
58
- public static IServiceCollection AddJsonApi ( this IServiceCollection services ,
59
- Action < JsonApiOptions > options ,
60
- IMvcCoreBuilder mvcBuilder )
59
+ public static IServiceCollection AddJsonApi (
60
+ this IServiceCollection services ,
61
+ Action < JsonApiOptions > configureOptions ,
62
+ IMvcCoreBuilder mvcBuilder ,
63
+ Action < ServiceDiscoveryFacade > autoDiscover = null )
61
64
{
62
- var config = new JsonApiOptions ( ) ;
65
+ var options = new JsonApiOptions ( ) ;
66
+ configureOptions ( options ) ;
63
67
64
- options ( config ) ;
68
+ if ( autoDiscover != null )
69
+ {
70
+ var facade = new ServiceDiscoveryFacade ( services , options . ContextGraphBuilder ) ;
71
+ autoDiscover ( facade ) ;
72
+ }
65
73
66
- mvcBuilder
67
- . AddMvcOptions ( opt =>
74
+ mvcBuilder . AddMvcOptions ( opt =>
68
75
{
69
76
opt . Filters . Add ( typeof ( JsonApiExceptionFilter ) ) ;
70
- opt . SerializeAsJsonApi ( config ) ;
77
+ opt . SerializeAsJsonApi ( options ) ;
71
78
} ) ;
72
79
73
- AddJsonApiInternals ( services , config ) ;
80
+ AddJsonApiInternals ( services , options ) ;
74
81
return services ;
75
82
}
76
83
@@ -90,6 +97,9 @@ public static void AddJsonApiInternals(
90
97
this IServiceCollection services ,
91
98
JsonApiOptions jsonApiOptions )
92
99
{
100
+ if ( jsonApiOptions . ContextGraph == null )
101
+ jsonApiOptions . ContextGraph = jsonApiOptions . ContextGraphBuilder . Build ( ) ;
102
+
93
103
if ( jsonApiOptions . ContextGraph . UsesDbContext == false )
94
104
{
95
105
services . AddScoped < DbContext > ( ) ;
0 commit comments