2017-08-22 93 views
0

我有這個控制檯應用程序生成一組僞造的數據/數據庫信息進行測試。在mscorlib.dll中發生類型'System.ArgumentOutOfRangeException'的異常,但未在用戶代碼中處理

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運行時,我得到這個例外

類型「System.ArgumentOutOfRangeException」的異常出現在mscorlib.dll,但在用戶代碼中沒有處理

附加信息:'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: 

請幫助您對此有任何建議。 謝謝

+0

正如旁註'int gender = random.Next(0,1);'只會生成零,因爲上限是排他性的。是否有'branches.Count'或'schemeIds.Count' 0任何機會? – Milster

+1

我認爲這可能會導致問題:'SchemeID = schemeIds [random.Next(schemeCount)]。SchemeCode'。你在哪裏設置'schemeCount'?錯誤清楚地說明了這個問題。也可以在'Faker'類中,因爲你也使用'Random'對象。嘗試調試您的代碼。 –

+0

@MichałTurczyn謝謝..是由於'branchCount'和'schemeCount'中的數據不正確,我現在已經修復了這個問題 –

回答

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的結果,我們可以讓他們返回更多結果「修復」,但更好的辦法是使用Faker.Random.ListItem()和C#6安全導航操作符(?)這樣的 -

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

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

如果沒有在提供的列表中的任何條目這將值設置爲null

+0

謝謝@gamesmad。但是,** Faker.Random.ListItem(分支)?。名稱,**未被識別。該錯誤是由於我已經排序的'branchCount'和'schemeCount'中的空/負數據。再次感謝 –

相關問題