ASP.NET Core Невозможно получить доступ к удаленному объектуC#

Место общения программистов 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

Вернуться в «C#»