Skip to content

Commit ea3e4f6

Browse files
committed
do not persist context state across requests
1 parent 7407f7d commit ea3e4f6

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class IServiceCollectionExtensions
1010
{
1111
public static void AddJsonApi(this IServiceCollection services, Action<IJsonApiModelConfiguration> configurationAction)
1212
{
13-
services.AddScoped(_ => {
13+
services.AddTransient(_ => {
1414
var configBuilder = new JsonApiConfigurationBuilder(configurationAction);
1515
var config = configBuilder.Build();
1616
return (IRouter)new Router(config, new RouteBuilder(config), new ControllerBuilder());

JsonApiDotNetCore/Routing/Router.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class Router : IRouter
1414
{
1515
private readonly JsonApiModelConfiguration _jsonApiModelConfiguration;
1616
private IServiceProvider _serviceProvider;
17-
private JsonApiContext _jsonApiContext;
1817
private IRouteBuilder _routeBuilder;
1918
private IControllerBuilder _controllerBuilder;
2019

@@ -32,50 +31,51 @@ public async Task<bool> HandleJsonApiRouteAsync(HttpContext context, IServicePro
3231
var route = _routeBuilder.BuildFromRequest(context.Request);
3332
if (route == null) return false;
3433

35-
InitializeContext(context, route);
36-
await CallController(context);
34+
var jsonApiContext = InitializeContext(context, route);
35+
await CallController(jsonApiContext);
3736

3837
return true;
3938
}
4039

41-
private void InitializeContext(HttpContext context, Route route)
40+
private JsonApiContext InitializeContext(HttpContext context, Route route)
4241
{
4342
var dbContext = _serviceProvider.GetService(_jsonApiModelConfiguration.ContextType);
44-
_jsonApiContext = new JsonApiContext(context, route, dbContext, _jsonApiModelConfiguration);
43+
Console.WriteLine("InitializingContext");
44+
return new JsonApiContext(context, route, dbContext, _jsonApiModelConfiguration);
4545
}
4646

47-
private async Task CallController(HttpContext context)
47+
private async Task CallController(JsonApiContext jsonApiContext)
4848
{
49-
var controller = _controllerBuilder.BuildController(_jsonApiContext);
49+
var controller = _controllerBuilder.BuildController(jsonApiContext);
5050

51-
var result = ActivateControllerMethod(controller);
51+
var result = ActivateControllerMethod(controller, jsonApiContext);
5252

53-
result.Value = SerializeResult(result.Value);
53+
result.Value = SerializeResult(result.Value, jsonApiContext);
5454

55-
await SendResponse(context, result);
55+
await SendResponse(jsonApiContext.HttpContext, result);
5656
}
5757

58-
private ObjectResult ActivateControllerMethod(IJsonApiController controller)
58+
private ObjectResult ActivateControllerMethod(IJsonApiController controller, JsonApiContext jsonApiContext)
5959
{
60-
var route = _jsonApiContext.Route;
60+
var route = jsonApiContext.Route;
6161
switch (route.RequestMethod)
6262
{
6363
case "GET":
6464
return string.IsNullOrEmpty(route.ResourceId) ? controller.Get() : controller.Get(route.ResourceId);
6565
case "POST":
66-
return controller.Post(new JsonApiDeserializer(_jsonApiContext).GetEntityFromRequest());
66+
return controller.Post(new JsonApiDeserializer(jsonApiContext).GetEntityFromRequest());
6767
case "PATCH":
68-
return controller.Patch(route.ResourceId, new JsonApiDeserializer(_jsonApiContext).GetEntityPatch());
68+
return controller.Patch(route.ResourceId, new JsonApiDeserializer(jsonApiContext).GetEntityPatch());
6969
case "DELETE":
7070
return controller.Delete(route.ResourceId);
7171
default:
7272
throw new ArgumentException("Request method not supported", nameof(route));
7373
}
7474
}
7575

76-
private object SerializeResult(object result)
76+
private object SerializeResult(object result, JsonApiContext jsonApiContext)
7777
{
78-
return result == null ? null : new JsonApiSerializer(_jsonApiContext).ToJsonApiDocument(result);
78+
return result == null ? null : new JsonApiSerializer(jsonApiContext).ToJsonApiDocument(result);
7979
}
8080

8181
private async Task SendResponse(HttpContext context, ObjectResult result)

0 commit comments

Comments
 (0)