Skip to content

Commit 7407f7d

Browse files
committed
fix request scope issue
1 parent bafaca6 commit 7407f7d

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using AutoMapper;
32
using JsonApiDotNetCore.Configuration;
43
using JsonApiDotNetCore.Controllers;
54
using JsonApiDotNetCore.Routing;
@@ -11,10 +10,11 @@ public static class IServiceCollectionExtensions
1110
{
1211
public static void AddJsonApi(this IServiceCollection services, Action<IJsonApiModelConfiguration> configurationAction)
1312
{
14-
var configBuilder = new JsonApiConfigurationBuilder(configurationAction);
15-
var config = configBuilder.Build();
16-
IRouter router = new Router(config, new RouteBuilder(config), new ControllerBuilder());
17-
services.AddSingleton(_ => router);
13+
services.AddScoped(_ => {
14+
var configBuilder = new JsonApiConfigurationBuilder(configurationAction);
15+
var config = configBuilder.Build();
16+
return (IRouter)new Router(config, new RouteBuilder(config), new ControllerBuilder());
17+
});
1818
}
1919
}
2020
}

JsonApiDotNetCore/Routing/Router.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task<bool> HandleJsonApiRouteAsync(HttpContext context, IServicePro
3333
if (route == null) return false;
3434

3535
InitializeContext(context, route);
36-
await CallController();
36+
await CallController(context);
3737

3838
return true;
3939
}
@@ -44,15 +44,15 @@ private void InitializeContext(HttpContext context, Route route)
4444
_jsonApiContext = new JsonApiContext(context, route, dbContext, _jsonApiModelConfiguration);
4545
}
4646

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

5151
var result = ActivateControllerMethod(controller);
5252

5353
result.Value = SerializeResult(result.Value);
5454

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

5858
private ObjectResult ActivateControllerMethod(IJsonApiController controller)
@@ -78,9 +78,8 @@ private object SerializeResult(object result)
7878
return result == null ? null : new JsonApiSerializer(_jsonApiContext).ToJsonApiDocument(result);
7979
}
8080

81-
private async Task SendResponse(ObjectResult result)
81+
private async Task SendResponse(HttpContext context, ObjectResult result)
8282
{
83-
var context = _jsonApiContext.HttpContext;
8483
context.Response.StatusCode = result.StatusCode ?? 500;
8584
context.Response.ContentType = "application/vnd.api+json";
8685
await context.Response.WriteAsync(result.Value == null ? "" : result.Value.ToString(), Encoding.UTF8);

0 commit comments

Comments
 (0)