我有 Stocks 和 AppUsers 表以及 Portfolios 表,它们之间是多对多关系。我想获取投资组合的数据,但我的 API 遇到问题。它说:
Newtonsoft.Json.JsonSerializationException: Error getting value from 'Context' on 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.Collections.Generic.List`1[api.Models.Stock],API.Repositories.Concrete.PortfolioRepository+<GetUserPortfolio>d__2]'.
---> System.InvalidOperationException: Could not create getter for System.Threading.ExecutionContext& Context. ByRef return values are not supported.
at Newtonsoft.Json.Utilities.ReflectionDelegateFactory.CreateGet[T](MemberInfo memberInfo)
at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
--- End of inner exception stack trace ---
at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
at Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
我的控制器:
[HttpGet]
[Authorize]
public async Task<IActionResult> GetUserPortfolio()
{
var username = User.GetUsername();
var appUser = await userManager.FindByNameAsync(username);
var userPortfolio = portfolioReposiotry.GetUserPortfolio(appUser);
return Ok(userPortfolio);
}
我的仓库:
public class PortfolioRepository : IPortfolioRepository
{
private readonly AppDbContext context;
public PortfolioRepository(AppDbContext context)
{
this.context = context;
}
public async Task<List<Stock>> GetUserPortfolio(AppUser user)
{
var val = context.Portfolios.Where(u => u.AppUserId == user.Id)
.Select(stock => new Stock
{
Id = stock.StockId,
Symbol = stock.Stock.Symbol,
CompanyName = stock.Stock.CompanyName,
Purchase = stock.Stock.Purchase,
LastDiv = stock.Stock.LastDiv,
Industry = stock.Stock.Industry,
MarketCap = stock.Stock.MarketCap
});
return await val.ToListAsync();
}
}
不知道还需要什么吗?如果是的话请告诉我
我没有尝试任何解决方案,因为我不知道发生了什么。
System.InvalidOperationException:无法创建 getter System.Threading.ExecutionContext& 上下文
不知道还需要什么吗?如果是的话请告诉我
我没有尝试任何解决方案,因为我不知道发生了什么。
根据您的情况和描述,您遇到的错误可能有几个原因。
首先,确保 Stock 模型不包含任何可能导致序列化问题的属性。 Stock 模型或其相关类之一中可能有一个名为“Context”的属性。
此外,
GetUserPortfolio
方法是异步的,您正在正确等待其结果。但是,在 appUser 之后调用控制器中的 GetUserPortfolio
方法时,您没有维护异步链。
因此,为了修复您应该重构代码,如下所示:
[HttpGet]
[Authorize]
public async Task<IActionResult> GetUserPortfolio()
{
var username = User.GetUsername();
var appUser = await userManager.FindByNameAsync(username);
var userPortfolio = await portfolioReposiotry.GetUserPortfolio(appUser);
return Ok(userPortfolio);
}
注意: 确保
User.GetUsername()
也保持相同的 asynchronous
链。