ERR_CONNECTION_RESET returning Async including object child collections

南楼画角 提交于 2019-12-02 19:27:36

问题


I am building a net core stack and implementing the layers as async.

I have the following customer repository code that is called by a web api layer

 public class CustomerRepository : ICustomerRepository
{
    //interface this up
    private DbContextOptionsBuilder<SykesTestContext> optionsBuilder = new DbContextOptionsBuilder<SykesTestContext>();
    public CustomerRepository(string connection)
    {
        optionsBuilder.UseSqlServer(connection);
    }
    public async Task<List<Dom.Customer>> GetAll()
    {
        List<Dom.Customer> customers = new List<Dom.Customer>();
        var efCustomers = new List<Ef.Customer>();

        using (var customerContext = new TestContext(optionsBuilder.Options))
        {
            efCustomers = await customerContext.Customer
                                   .Include(cust => cust.Address)
                                   .Include(cust => cust.Telephone)
                                   .ToListAsync();

            foreach (var customer in efCustomers)
            {
                customers.Add(Mapper.Map<Ef.Customer, Dom.Customer>(customer));
            }
        }

        return customers;
    }


}

THE API Calling method

 [Produces("application/json")]
[Route("api/Customers")]
public class CustomersController : Controller
{
    ICustomerRepository _customerRepository;
    public CustomersController(ICustomerRepository customerRepository)
    {
        _customerRepository = customerRepository;
    }

    public async Task<List<Customer>> Get()
    {
        try
        {
            var customers = await _customerRepository.GetAll();
            return customers;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

If I comment out the "includes" then it all works fine.

If I use the includes (to build the object child collections) I get the following error in the browser: net::ERR_CONNECTION_RESET and only part of the json object is returned?

Any ideas?

 public partial class Customer
{
    public Customer()
    {
        Address = new HashSet<Address>();
        Telephone = new HashSet<Telephone>();
    }

    public int Id { get; set; }
    public int ClientId { get; set; }
    public int TypeId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public int? ExternalId { get; set; }

    public virtual ICollection<Address> Address { get; set; }
    public virtual ICollection<Telephone> Telephone { get; set; }
}

Domain Model

public class Customer
{
    public int Id { get; set; }
    public int ClientId { get; set; }
    public int TypeId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public int? ExternalId { get; set; }

    public virtual ICollection<Address> Address { get; set; }
    public virtual ICollection<Telephone> Telephone { get; set; }
}

来源:https://stackoverflow.com/questions/44648707/err-connection-reset-returning-async-including-object-child-collections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!