返回列表<对象>上的StackoverflowException

时间:2019-07-05 16:05:30

标签: c# reactjs asp.net-core asp.net-core-2.1

我正在尝试进行访存调用以检索对象列表并仅打印其内容。进行调用时出现502.3错误,并且回叫发生stackoverflowexception错误。参见下面的代码

[HttpGet("[action]")]
    public async Task<List<Portfolio>> GetAllPortfolios()
    {
        var portfolios = await _portfolioService.GetAllPortfolios().ConfigureAwait(false);
        return portfolios;
    }

我试图返回一个空列表,并且运行状态为200。

这是我的代码,用于调用服务器以检索数据

public async Task<List<Portfolio>> GetAllPortfolios()
    {
        List<Portfolio> PortfolioList = new List<Portfolio>();
        Portfolio portfolio;
        using (SqlConnection conn = new SqlConnection(Connection))
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "USP_Get_All_Portfolios";
            conn.Open();
            SqlDataReader reader = await command.ExecuteReaderAsync().ConfigureAwait(false);

            //Fields to be populated
            string PortfolioName, Description, CreateID, UpdateID;

            while (await reader.ReadAsync().ConfigureAwait(false))
            {
                PortfolioName = reader["Portfolio_Name"].ToString();
                Description = reader["Long_Description"].ToString();
                CreateID = reader["Create_ID"].ToString();
                UpdateID = reader["Update_ID"].ToString();
                portfolio = new Portfolio(PortfolioName, Description, CreateID, UpdateID);
                PortfolioList.Add(portfolio);
            }
            reader.Close();
        }

        return PortfolioList;
    }

我的PortfolioService的代码

public class PortfolioService : IPortfolioService
{
    private readonly IPortfolioRepository _portfolioRepository;

    public PortfolioService(IPortfolioRepository portfolioRepository)
    {
        _portfolioRepository = portfolioRepository;
    }
    public async Task<bool> CreatePortfolio(Portfolio portfolio)
    {
        return await _portfolioRepository.CreatePortfolio(portfolio).ConfigureAwait(false);
    }

    public async Task<List<Portfolio>> GetAllPortfolios()
    {
        return await _portfolioRepository.GetAllPortfolios().ConfigureAwait(false);
    }
}

我的投资组合类的代码

public class Portfolio
{
    private string mPortfolioName;
    private string mDescription;
    private string mCreateID;
    private string mUpdateID;

    public string PortfolioName
    {
        get { return mPortfolioName; }
        set { mPortfolioName = value; }
    }

    public string Description
    {
        get { return Description; }
        set { mDescription = value; }
    }

    public string CreateID
    {
        get { return mCreateID; }
        set { mCreateID = value; }
    }

    public string UpdateID
    {
        get { return UpdateID; }
        set { mUpdateID = value; }
    }

    public Portfolio(string portfolioName, string description, string createID, string updateID)
    {
        mPortfolioName = portfolioName;
        mDescription = description;
        mCreateID = createID;
        mUpdateID = updateID;
    }
}

1 个答案:

答案 0 :(得分:2)

您需要在mUpdateId的{​​{1}}中返回get,并从Portfolio.UpdateId返回mDescription

您现在正在做的是在Portfolio.Description方法中为get调用Portfolio.UpdateId方法。

打给get的人。

打给get的人。

等...

从控制器返回时,ASP.NET将为您序列化该类。进行序列化时,它试图获取get的值,这将导致无限递归,这将导致堆栈溢出。

偶然地,任何不放弃私有字段以支持Auto-Implemented Properties的理由。

UpdateId

这可能会避免您日后进行像这样的简单错字:)。