33using JsonApiDotNetCore . Configuration ;
44using JsonApiDotNetCore . Data ;
55using JsonApiDotNetCore . Formatters ;
6+ using JsonApiDotNetCore . Graph ;
67using JsonApiDotNetCore . Internal ;
78using JsonApiDotNetCore . Internal . Generics ;
89using JsonApiDotNetCore . Middleware ;
@@ -35,9 +36,10 @@ public static IServiceCollection AddJsonApi<TContext>(this IServiceCollection se
3536 return AddJsonApi < TContext > ( services , options , mvcBuilder ) ;
3637 }
3738
38- public static IServiceCollection AddJsonApi < TContext > ( this IServiceCollection services ,
39- Action < JsonApiOptions > options ,
40- IMvcCoreBuilder mvcBuilder ) where TContext : DbContext
39+ public static IServiceCollection AddJsonApi < TContext > (
40+ this IServiceCollection services ,
41+ Action < JsonApiOptions > options ,
42+ IMvcCoreBuilder mvcBuilder ) where TContext : DbContext
4143 {
4244 var config = new JsonApiOptions ( ) ;
4345
@@ -51,17 +53,24 @@ public static IServiceCollection AddJsonApi<TContext>(this IServiceCollection se
5153 return services ;
5254 }
5355
54- public static IServiceCollection AddJsonApi ( this IServiceCollection services ,
55- Action < JsonApiOptions > options ,
56- IMvcCoreBuilder mvcBuilder )
56+ public static IServiceCollection AddJsonApi (
57+ this IServiceCollection services ,
58+ Action < JsonApiOptions > configureOptions ,
59+ IMvcCoreBuilder mvcBuilder ,
60+ Action < ServiceDiscoveryFacade > autoDiscover = null )
5761 {
58- var config = new JsonApiOptions ( ) ;
62+ var options = new JsonApiOptions ( ) ;
63+ configureOptions ( options ) ;
5964
60- options ( config ) ;
65+ if ( autoDiscover != null )
66+ {
67+ var facade = new ServiceDiscoveryFacade ( services , options . ContextGraphBuilder ) ;
68+ autoDiscover ( facade ) ;
69+ }
6170
6271 mvcBuilder . AddMvcOptions ( opt => AddMvcOptions ( opt , config ) ) ;
6372
64- AddJsonApiInternals ( services , config ) ;
73+ AddJsonApiInternals ( services , options ) ;
6574 return services ;
6675 }
6776
@@ -88,6 +97,9 @@ public static void AddJsonApiInternals(
8897 this IServiceCollection services ,
8998 JsonApiOptions jsonApiOptions )
9099 {
100+ if ( jsonApiOptions . ContextGraph == null )
101+ jsonApiOptions . ContextGraph = jsonApiOptions . ContextGraphBuilder . Build ( ) ;
102+
91103 if ( jsonApiOptions . ContextGraph . UsesDbContext == false )
92104 {
93105 services . AddScoped < DbContext > ( ) ;
0 commit comments