mscorlib.dll中出现“System.ArgumentOutOfRangeException”类型的异常,但未在用户代码

时间:2017-08-22 12:01:24

标签: c# asp.net-mvc-4 console-application

我有这个控制台应用程序,它会在数据库上生成一组虚假数据/信息以供测试。

public List<LCS_ExchangeDataStaging> GenerateNewData(int count)
            {
                var newData = new List<LCS_ExchangeDataStaging>();
                var random = new Random();
                var branches = new LCSBranchContext().GetAllBranches();
                var schemeIds = new LCSSchemePortfolioContext().SchemePortfolios();

            var branchCount = branches.Count - 1;
            var schemeCount = schemeIds.Count - 1;

            Parallel.For(1, count + 1, i =>
            {
                int gender = random.Next(0, 1);

                var data = new LCS_ExchangeDataStaging
                {
                    AccountNumber = (3000000000 + random.Next(999999999)).ToString(),
                    AccountStatement = random.Next(),
                    Address = Faker.Address.StreetAddress(),
                    AddressOfKeyContact = Faker.Address.StreetAddress(),
                    Age = RandomDate(DateTime.Now.AddYears(-70), DateTime.Now.AddYears(-18), random),
                    AmountDisbursed = Faker.RandomNumber.Next(20000000),
                    AmountOverdue = Faker.RandomNumber.Next(1000000),
                    BranchName = branches[random.Next(branchCount)].Name,
                    ClientId = $"R{(Faker.RandomNumber.Next(99999999) + 200000000).ToString()}",
                    ClientName = gender == 0 ? NameGenerator.Generate(Gender.Male) : NameGenerator.Generate(Gender.Female),
                    CompaniesMandate = "",
                    DateOfIncorporation = RandomDate(DateTime.Now.AddYears(-70), DateTime.Now, random),
                    DaysPastDue = 1,
                    DisbursmentDate = RandomDate(DateTime.Now.AddYears(-3), DateTime.Now, random),
                    Email = Faker.Internet.Email(),
                    EmergencyContact = Faker.Name.FullName(),
                    EmployeeContact = Faker.Name.FullName(),
                    EmployeeContactTelePhone = Faker.Phone.Number(),
                    EmployerAddress = Faker.Address.StreetAddress(),
                    EmployerName = Faker.Company.Name(),
                    EmployerTelephone = Faker.Phone.Number(),
                    ExpiryDate = RandomDate(DateTime.Now.AddYears(1), DateTime.Now.AddYears(7), random),
                    FeesOverdue = Faker.RandomNumber.Next(2000),
                    Gender = gender == 0 ? "M" : "F",
                    GuarantorAddress = Faker.Address.StreetAddress(),
                    GuarantorName = Faker.Name.FullName(),
                    GuarantorTelephone = Faker.Phone.Number(),
                    Income = random.Next(50000, 1000000),
                    InsuranceCompany = Faker.Company.Name(),
                    InsuredAmount = Faker.RandomNumber.Next(2000),
                    InterestRate = random.Next(3, 24),
                    IntrducerAddress = Faker.Address.StreetAddress(),
                    IntrestOverdue = Faker.RandomNumber.Next(2000),
                    IntroducerName = Faker.Name.FullName(),
                    IntroducerTelephone = Faker.Phone.Number(),
                    KeyContactPerson = Faker.Name.FullName(),
                    LastCrAmount = Faker.RandomNumber.Next(20000),
                    LastCrDate = RandomDate(DateTime.Now.AddYears(-1), DateTime.Now, random),
                    LienAmount = Faker.RandomNumber.Next(2000),
                    LoanRestructured = "No",
                    MissedPayin24MonthCount = random.Next(0, 15),
                    MonthlyInflow = Faker.RandomNumber.Next(1000000),
                    OperativeAccountNumber = $"01{random.Next(99999999)}",
                    OutstandingPrincipal = Faker.RandomNumber.Next(200000),
                    PenalChargeDue = Faker.RandomNumber.Next(2000),
                    PrincipalOverdue = Faker.RandomNumber.Next(2000),
                    ProductCategory = "",
                    ProductType = "LAA",
                    RelationshipYears = random.Next(0, 40),
                    RepaymentAmount = Faker.RandomNumber.Next(2000),
                    ReturnedChequeIn6MonthCount = random.Next(0, 3),
                    SchemeID = schemeIds[random.Next(schemeCount)].SchemeCode,
                    Telephone1 = Faker.Phone.Number(),
                    Telephone2 = Faker.Phone.Number(),
                    TelephoneOfKeyContact = Faker.Phone.Number(),
                    Tenor = random.Next(3, 124).ToString(),
                };

                newData.Add(data);
            });

            return newData;
        }

然而,当方法 GenerateNewData 运行时,我得到此异常

mscorlib.dll中出现“System.ArgumentOutOfRangeException”类型的异常,但未在用户代码中处理

附加信息:'maxValue'必须大于零。

System.ArgumentOutOfRangeException was unhandled by user code
  HResult=-2146233086
  Message='maxValue' must be greater than zero.
Parameter name: maxValue
  ParamName=maxValue
  Source=mscorlib
  StackTrace:
       at System.Random.Next(Int32 maxValue)
       at RLCS.Tasks.Data.DataGenerator.<>c__DisplayClass1_0.<GenerateNewData>b__0(Int32 i) in C:\Users\Solution Architect\Documents\WORK\lcs\lcs\RLCS.Tasks\Data\DataGenerator.cs:line 50
       at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.<ForWorker>b__1()
  InnerException:

请提供您对此可能提出的任何建议。 感谢

1 个答案:

答案 0 :(得分:2)

如果没有运行或看到所有代码,很难分辨,但问题是您对random.Next(Int32 maxValue)的一次调用提供的Int32 maxValue为0或更少。这缩小到以下行之一 -

BranchName = branches[random.Next(branchCount)].Name,

SchemeID = schemeIds[random.Next(schemeCount)].SchemeCode,

所以重要的变量似乎是branchCountschemeCount。这些设置为其各自上下文方法结果的Count - 1

我怀疑LCSBranchContext().GetAllBranches()LCSSchemePortfolioContext().SchemePortfolios()正在返回1或0结果,我们可以&#34;修复&#34;通过让它们返回更多结果,但更好的解决方案是使用Faker.Random.ListItem()和C#6安全导航操作符(?),如下所示 -

BranchName = Faker.Random.ListItem(branches)?.Name,

SchemeID = Faker.Random.ListItem(schemeIds)?.SchemeCode,

如果提供的列表中没有任何条目,则会将值设置为null