ASP.NET Core Невозможно получить доступ к удаленному объекту ⇐ C#
ASP.NET Core Невозможно получить доступ к удаленному объекту
I created a custom IAsyncAuthorizationFilter attribute.
public class CustomeAttribute : Attribute, IAsyncAuthorizationFilter { public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { var dto = new DTO(); var httpContextAccessor = context.HttpContext.RequestServices.GetRequiredService(); var requestBody = httpContextAccessor.HttpContext?.Request.Body; if (requestBody != null && requestBody.CanSeek) { requestBody.Position = 0; using var reader = new StreamReader(requestBody); var requestBodyString = await reader.ReadToEndAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; dto = JsonSerializer.Deserialize(requestBodyString, options); } } } in program.cs, I added the following
builder.Services.AddScoped(); and
app.Use(async (context, next) => { context.Request.EnableBuffering(); await next(); }); and I used the attribute in the controller as follows:
[HttpPost] [CustomeAttribute )] public async Task Add([FromBody] DTO dTO) OnAuthorizationAsync Finish correctly, but then I receive the error:
{"Cannot access a disposed object.\r\nObject name: 'System.String'."}
why?
Источник: https://stackoverflow.com/questions/781 ... sed-object
I created a custom IAsyncAuthorizationFilter attribute.
public class CustomeAttribute : Attribute, IAsyncAuthorizationFilter { public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { var dto = new DTO(); var httpContextAccessor = context.HttpContext.RequestServices.GetRequiredService(); var requestBody = httpContextAccessor.HttpContext?.Request.Body; if (requestBody != null && requestBody.CanSeek) { requestBody.Position = 0; using var reader = new StreamReader(requestBody); var requestBodyString = await reader.ReadToEndAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; dto = JsonSerializer.Deserialize(requestBodyString, options); } } } in program.cs, I added the following
builder.Services.AddScoped(); and
app.Use(async (context, next) => { context.Request.EnableBuffering(); await next(); }); and I used the attribute in the controller as follows:
[HttpPost] [CustomeAttribute )] public async Task Add([FromBody] DTO dTO) OnAuthorizationAsync Finish correctly, but then I receive the error:
{"Cannot access a disposed object.\r\nObject name: 'System.String'."}
why?
Источник: https://stackoverflow.com/questions/781 ... sed-object
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Невозможно получить доступ к удаленному объекту в ASP.NET Core при внедрении DbContext.
Anonymous » » в форуме C# - 0 Ответы
- 16 Просмотры
-
Последнее сообщение Anonymous
-