简体   繁体   中英

System.AggregateException exception when using with Parallel.Invoke()

I am trying to run 2 exe in Parallel using Parallel.Invoke() Both the exe is trying to access the same Database (but different tables). Also, the exe's will run in two different servers. I am using WMI with C# to start the batch file (which will invoke the other exe file) in the remote server.

Parallel.Invoke(() =>
            {
                RunInParallel();
            },
            () =>
            {
                InvokeModule("EmiCalculator.exe");
            } 
            );

void RunInParallel()
{
                var connoptions = new ConnectionOptions();
                var managementScope = new ManagementScope
                    (String.Format(@"\\{0}\ROOT\CIMV2", REMOTE_COMPUTER), connoptions);
                var wmiProc = new ManagementClass(managementScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
                string commandLineInput = @"\\win-server1\D$\Data\Run.bat";
                var processToRun = new[] { commandLineInput };
                wmiProc.InvokeMethod("Create", processToRun);
}

Run.bat is calling SalaryCalculator.exe

Both the exe are having a configuration file with ConnectionString as below.

<add name="CONN" connectionString="Data Source=SERVER1-LAB1;Initial Catalog=Loan;Integrated Security=True"/>

When running, the exe in remote machine is getting crashed and throwing below exception :

Application: SalaryCalculator.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AggregateException
Stack:
   at System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[], Int32, System.Threading.CancellationToken)
   at System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[], Int32)
   at System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[])

NOTE: If I run the batch file after connecting to remote machine, it is working perfectly. It is only throwing error when using with Parallel.Invoke()

How can I proceed here ? Any help is appreciated.

I was able to make this work. The issue was connection string as Integrated Secuity was mentioned. I just changed the connectionstring to use SQL server authentication instead of Windows authentication. And it worked fine.

< add name="CONN" connectionString="Data Source=SERVER1-LAB1;Initial Catalog=Loan;User Id=sa;Password=xxxx12345678" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM