Skip to content

Commit 86cabe9

Browse files
committed
introduce ScopedServiceProvider and fix tests
1 parent a1624ff commit 86cabe9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+354
-228
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
[![Build status](https://ci.appveyor.com/api/projects/status/9fvgeoxdikwkom10?svg=true)](https://ci.appveyor.com/project/jaredcnance/jsonapidotnetcore)
88
[![Travis](https://travis-ci.org/json-api-dotnet/JsonApiDotNetCore.svg?branch=master)](https://travis-ci.org/json-api-dotnet/JsonApiDotNetCore)
99
[![NuGet](https://img.shields.io/nuget/v/JsonApiDotNetCore.svg)](https://www.nuget.org/packages/JsonApiDotNetCore/)
10-
[![MyGet CI](https://img.shields.io/myget/research-institute/vpre/JsonApiDotNetCore.svg)](https://www.myget.org/feed/research-institute/package/nuget/JsonApiDotNetCore)
1110
[![Join the chat at https://gitter.im/json-api-dotnet-core/Lobby](https://badges.gitter.im/json-api-dotnet-core/Lobby.svg)](https://gitter.im/json-api-dotnet-core/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
1211
[![FIRST-TIMERS](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](http://www.firsttimersonly.com/)
1312

src/Examples/NoEntityFrameworkExample/Startup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Extensions.Logging;
1010
using NoEntityFrameworkExample.Services;
1111
using Microsoft.EntityFrameworkCore;
12+
using System;
1213

1314
namespace NoEntityFrameworkExample
1415
{
@@ -27,7 +28,7 @@ public Startup(IHostingEnvironment env)
2728
public IConfigurationRoot Configuration { get; }
2829

2930
// This method gets called by the runtime. Use this method to add services to the container.
30-
public void ConfigureServices(IServiceCollection services)
31+
public virtual IServiceProvider ConfigureServices(IServiceCollection services)
3132
{
3233
// Add framework services.
3334
var mvcBuilder = services.AddMvc();
@@ -46,6 +47,8 @@ public void ConfigureServices(IServiceCollection services)
4647
services.AddSingleton<IConfiguration>(Configuration);
4748
services.AddSingleton<DbContextOptions<AppDbContext>>(optionsBuilder.Options);
4849
services.AddScoped<AppDbContext>();
50+
51+
return services.BuildServiceProvider();
4952
}
5053

5154
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

src/Examples/OperationsExample/Startup.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ public virtual IServiceProvider ConfigureServices(IServiceCollection services)
3232

3333
services.AddSingleton<ILoggerFactory>(loggerFactory);
3434

35-
services.AddDbContext<AppDbContext>(options =>
36-
{
37-
options.UseNpgsql(GetDbConnectionString());
38-
}, ServiceLifetime.Transient);
35+
services.AddDbContext<AppDbContext>(options => options.UseNpgsql(GetDbConnectionString()), ServiceLifetime.Scoped);
3936

4037
services.AddJsonApi<AppDbContext>(opt => opt.EnableOperations = true);
4138

src/Examples/ReportsExample/Startup.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
51
using JsonApiDotNetCore.Extensions;
62
using JsonApiDotNetCore.Services;
73
using Microsoft.AspNetCore.Builder;
84
using Microsoft.AspNetCore.Hosting;
95
using Microsoft.Extensions.Configuration;
106
using Microsoft.Extensions.DependencyInjection;
117
using Microsoft.Extensions.Logging;
12-
using Microsoft.Extensions.Options;
138

149
namespace ReportsExample
1510
{
@@ -28,12 +23,13 @@ public Startup(IHostingEnvironment env)
2823
Config = builder.Build();
2924
}
3025

31-
public void ConfigureServices(IServiceCollection services)
26+
public virtual void ConfigureServices(IServiceCollection services)
3227
{
3328
var mvcBuilder = services.AddMvc();
3429
services.AddJsonApi(opt =>
3530
{
36-
opt.BuildContextGraph(builder => {
31+
opt.BuildContextGraph(builder =>
32+
{
3733
builder.AddResource<Report>("reports");
3834
});
3935
opt.Namespace = "api";

src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ public class JsonApiOptions
105105
public NullAttributeResponseBehavior NullAttributeResponseBehavior { get; set; }
106106

107107
/// <summary>
108-
/// Whether or not to allow json:api v1.1 operation requests
109-
/// This will be enabled by default in JsonApiDotNetCore v2.2.1
108+
/// Whether or not to allow json:api v1.1 operation requests.
109+
/// This is a beta feature and there may be breaking changes
110+
/// in subsequent releases.
110111
/// </summary>
112+
/// <remarks>
113+
/// This will be enabled by default in JsonApiDotNetCore v2.2.1
114+
/// </remarks>
111115
public bool EnableOperations { get; set; }
112116

113117
[Obsolete("JsonContract resolver can now be set on SerializerSettings.")]

src/JsonApiDotNetCore/Extensions/DbContextExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.EntityFrameworkCore;
2-
using System.Reflection;
32
using System;
43

54
namespace JsonApiDotNetCore.Extensions

src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using JsonApiDotNetCore.Internal;
77
using JsonApiDotNetCore.Internal.Generics;
88
using JsonApiDotNetCore.Middleware;
9-
using JsonApiDotNetCore.Models;
109
using JsonApiDotNetCore.Serialization;
1110
using JsonApiDotNetCore.Services;
1211
using JsonApiDotNetCore.Services.Operations;
@@ -89,7 +88,7 @@ public static void AddJsonApiInternals(
8988
this IServiceCollection services,
9089
JsonApiOptions jsonApiOptions)
9190
{
92-
if (!jsonApiOptions.ContextGraph.UsesDbContext)
91+
if (jsonApiOptions.ContextGraph.UsesDbContext == false)
9392
{
9493
services.AddScoped<DbContext>();
9594
services.AddSingleton<DbContextOptions>(new DbContextOptionsBuilder().Options);
@@ -122,6 +121,7 @@ public static void AddJsonApiInternals(
122121
services.AddSingleton<IContextGraph>(jsonApiOptions.ContextGraph);
123122
services.AddScoped<IJsonApiContext, JsonApiContext>();
124123
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
124+
services.AddScoped<IScopedServiceProvider, RequestScopedServiceProvider>();
125125
services.AddScoped<JsonApiRouteHandler>();
126126
services.AddScoped<IMetaBuilder, MetaBuilder>();
127127
services.AddScoped<IDocumentBuilder, DocumentBuilder>();

src/JsonApiDotNetCore/Internal/Generics/GenericProcessorFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using JsonApiDotNetCore.Services;
12
using Microsoft.AspNetCore.Http;
23
using System;
34

@@ -35,9 +36,9 @@ public class GenericProcessorFactory : IGenericProcessorFactory
3536
{
3637
private readonly IServiceProvider _serviceProvider;
3738

38-
public GenericProcessorFactory(IHttpContextAccessor httpContextAccessor)
39+
public GenericProcessorFactory(IScopedServiceProvider serviceProvider)
3940
{
40-
_serviceProvider = httpContextAccessor.HttpContext.RequestServices;
41+
_serviceProvider = serviceProvider;
4142
}
4243

4344
public TInterface GetProcessor<TInterface>(Type openGenericType, Type resourceType)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
4+
namespace JsonApiDotNetCore.Services
5+
{
6+
/// <summary>
7+
/// An interface used to separate the registration of the global ServiceProvider
8+
/// from a request scoped service provider. This is useful in cases when we need to
9+
/// manually resolve services from the request scope (e.g. operation processors)
10+
/// </summary>
11+
public interface IScopedServiceProvider : IServiceProvider { }
12+
13+
/// <summary>
14+
/// A service provider that uses the current HttpContext request scope
15+
/// </summary>
16+
public class RequestScopedServiceProvider : IScopedServiceProvider
17+
{
18+
private readonly IServiceProvider _serviceProvider;
19+
20+
public RequestScopedServiceProvider(IHttpContextAccessor httpContextAccessor)
21+
{
22+
_serviceProvider = httpContextAccessor.HttpContext.RequestServices;
23+
}
24+
25+
/// <inheritdoc />
26+
public object GetService(Type serviceType) => _serviceProvider.GetService(serviceType);
27+
}
28+
}

src/JsonApiDotNetCore/api/.manifest

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@
283283
"JsonApiDotNetCore.Internal.Generics.GenericProcessor`2.SetRelationships(System.Object,JsonApiDotNetCore.Models.RelationshipAttribute,System.Collections.Generic.IEnumerable{System.String})": "JsonApiDotNetCore.Internal.Generics.GenericProcessor-2.yml",
284284
"JsonApiDotNetCore.Internal.Generics.GenericProcessor`2.UpdateRelationshipsAsync(System.Object,JsonApiDotNetCore.Models.RelationshipAttribute,System.Collections.Generic.IEnumerable{System.String})": "JsonApiDotNetCore.Internal.Generics.GenericProcessor-2.yml",
285285
"JsonApiDotNetCore.Internal.Generics.GenericProcessorFactory": "JsonApiDotNetCore.Internal.Generics.GenericProcessorFactory.yml",
286-
"JsonApiDotNetCore.Internal.Generics.GenericProcessorFactory.#ctor(Microsoft.AspNetCore.Http.IHttpContextAccessor)": "JsonApiDotNetCore.Internal.Generics.GenericProcessorFactory.yml",
286+
"JsonApiDotNetCore.Internal.Generics.GenericProcessorFactory.#ctor(JsonApiDotNetCore.Services.IScopedServiceProvider)": "JsonApiDotNetCore.Internal.Generics.GenericProcessorFactory.yml",
287287
"JsonApiDotNetCore.Internal.Generics.GenericProcessorFactory.GetProcessor``1(System.Type,System.Type)": "JsonApiDotNetCore.Internal.Generics.GenericProcessorFactory.yml",
288288
"JsonApiDotNetCore.Internal.Generics.GenericProcessorFactory.GetProcessor``1(System.Type,System.Type,System.Type)": "JsonApiDotNetCore.Internal.Generics.GenericProcessorFactory.yml",
289289
"JsonApiDotNetCore.Internal.Generics.IGenericProcessor": "JsonApiDotNetCore.Internal.Generics.IGenericProcessor.yml",
@@ -586,6 +586,7 @@
586586
"JsonApiDotNetCore.Services.IResourceQueryService`2": "JsonApiDotNetCore.Services.IResourceQueryService-2.yml",
587587
"JsonApiDotNetCore.Services.IResourceService`1": "JsonApiDotNetCore.Services.IResourceService-1.yml",
588588
"JsonApiDotNetCore.Services.IResourceService`2": "JsonApiDotNetCore.Services.IResourceService-2.yml",
589+
"JsonApiDotNetCore.Services.IScopedServiceProvider": "JsonApiDotNetCore.Services.IScopedServiceProvider.yml",
589590
"JsonApiDotNetCore.Services.IUpdateRelationshipService`1": "JsonApiDotNetCore.Services.IUpdateRelationshipService-1.yml",
590591
"JsonApiDotNetCore.Services.IUpdateRelationshipService`2": "JsonApiDotNetCore.Services.IUpdateRelationshipService-2.yml",
591592
"JsonApiDotNetCore.Services.IUpdateRelationshipService`2.UpdateRelationshipsAsync(`1,System.String,System.Collections.Generic.List{JsonApiDotNetCore.Models.DocumentData})": "JsonApiDotNetCore.Services.IUpdateRelationshipService-2.yml",
@@ -673,5 +674,8 @@
673674
"JsonApiDotNetCore.Services.QueryParser.ParseFilterQuery(System.String,System.String)": "JsonApiDotNetCore.Services.QueryParser.yml",
674675
"JsonApiDotNetCore.Services.QueryParser.ParseIncludedRelationships(System.String)": "JsonApiDotNetCore.Services.QueryParser.yml",
675676
"JsonApiDotNetCore.Services.QueryParser.ParsePageQuery(JsonApiDotNetCore.Internal.Query.PageQuery,System.String,System.String)": "JsonApiDotNetCore.Services.QueryParser.yml",
676-
"JsonApiDotNetCore.Services.QueryParser.ParseSortParameters(System.String)": "JsonApiDotNetCore.Services.QueryParser.yml"
677+
"JsonApiDotNetCore.Services.QueryParser.ParseSortParameters(System.String)": "JsonApiDotNetCore.Services.QueryParser.yml",
678+
"JsonApiDotNetCore.Services.RequestScopedServiceProvider": "JsonApiDotNetCore.Services.RequestScopedServiceProvider.yml",
679+
"JsonApiDotNetCore.Services.RequestScopedServiceProvider.#ctor(Microsoft.AspNetCore.Http.IHttpContextAccessor)": "JsonApiDotNetCore.Services.RequestScopedServiceProvider.yml",
680+
"JsonApiDotNetCore.Services.RequestScopedServiceProvider.GetService(System.Type)": "JsonApiDotNetCore.Services.RequestScopedServiceProvider.yml"
677681
}

0 commit comments

Comments
 (0)