Api 使用Include后没有服务器响应ASP.NET Core

Api 使用Include后没有服务器响应ASP.NET Core,api,server,Api,Server,我在使用ASP.NET内核的数据库中遇到了一个奇怪的服务器响应问题 所以案例1返回200 OK和一个很好的列表 public IActionResult GetTrades(int id) { var positions = context.Trades.Where(x=>x.WebClientId==id).ToList(); return Ok(positions); } 然而,在这个交易表中,我有其他被其id引用的对象,我也想访问它们,所以我使用Include()

我在使用ASP.NET内核的数据库中遇到了一个奇怪的服务器响应问题

所以案例1返回200 OK和一个很好的列表

public IActionResult GetTrades(int id)
{
    var positions = context.Trades.Where(x=>x.WebClientId==id).ToList();
    return Ok(positions);
}
然而,在这个交易表中,我有其他被其id引用的对象,我也想访问它们,所以我使用Include()

现在奇怪的事情开始发生了。我在《邮递员》杂志上没有收到任何回复。没有错误,什么都没有,只是无法得到任何响应

然而,如果我进入调试模式并在返回时放置断点,我需要的一切都在那里,所有对象都在那里,我可以进入其中的每一个,策略,投资组合,并查看所有模型细节


希望其他人也有类似的问题。谢谢。

域模型中有一个循环。我不得不使用AutoMapper和Separate资源模型来消除循环,一切都开始工作了

例如:

在交易模型类中,对投资组合模型类的引用如下:

public Portfolio Portfolio { get; set; } //virtual for lazy loading
public int PortfolioId { get; set; }   
 public IList<Trade> Trades { get; set; }
public async Task<IEnumerable<TradeResource>> GetPositions(int id)
{

    var positions = await context.Trades
         .Where(x => x.WebClientId == id)
        .Include(s => s.Strategy)
        .Include(p => p.Portfolio)
         .ToListAsync();

    return mapper.Map<List<Trade>, List<TradeResource>>(positions);
}
在Porfolios类中,对Trades类的引用如下:

public Portfolio Portfolio { get; set; } //virtual for lazy loading
public int PortfolioId { get; set; }   
 public IList<Trade> Trades { get; set; }
public async Task<IEnumerable<TradeResource>> GetPositions(int id)
{

    var positions = await context.Trades
         .Where(x => x.WebClientId == id)
        .Include(s => s.Strategy)
        .Include(p => p.Portfolio)
         .ToListAsync();

    return mapper.Map<List<Trade>, List<TradeResource>>(positions);
}
公共IList交易{get;set;} 我将这两个类重新创建为PortfolioResource和TradeResource,但没有循环,并编辑控制器以使用AutoMapper,如下所示:

public Portfolio Portfolio { get; set; } //virtual for lazy loading
public int PortfolioId { get; set; }   
 public IList<Trade> Trades { get; set; }
public async Task<IEnumerable<TradeResource>> GetPositions(int id)
{

    var positions = await context.Trades
         .Where(x => x.WebClientId == id)
        .Include(s => s.Strategy)
        .Include(p => p.Portfolio)
         .ToListAsync();

    return mapper.Map<List<Trade>, List<TradeResource>>(positions);
}
public异步任务GetPositions(int-id)
{
var头寸=等待上下文。交易
.Where(x=>x.WebClientId==id)
.包括(s=>s.策略)
.包括(p=>p.Portfolio)
.ToListAsync();
返回mapper.Map(位置);
}
在ASP.NET内核中正确安装AutoMapper之后,我还使用

1) dotnet添加包自动映射器 2) dotnet添加包AutoMapper.Extensions.Microsoft.DependancyInjection 3) 网络还原

还添加了services.AddAutoMapper();到ConfigureServices中的StartUp.cs

最后是mapper类

public class MappingProfie:Profile
{
    public MappingProfie()
    {
        CreateMap<Trade, TradeResource>();
        CreateMap<Portfolio, PortfolioResource>();
        CreateMap<WebClient, WebClientResource>();
        CreateMap<Strategy, StrategyResource>();
    }
}
公共类映射profie:Profile
{
公共映射profie()
{
CreateMap();
CreateMap();
CreateMap();
CreateMap();
}
}
从这以后一切都开始运转了