Note that there are some explanatory texts on larger screens.

plurals
  1. POException Handling not working in Parallel.ForEach
    primarykey
    data
    text
    <p><em>Problem situation</em></p> <p>In my project I call into PowerShell to retrieve a list of organizations that are configured in Exchange 2010. Then I have to do something for each organization. Invoking PowerShell is <em>sloooow</em>, so if I'd to all the operations in sequence, it would take minutes. You can, however, create a PowerShell Runspace Pool and execute commands parallel, which saves a lot of time. So this is what I did:</p> <pre><code>public void MainMethod() { var organizations = exchangeRepository.GetOrganizations(); Parallel.ForEach( organizations, organization =&gt; { try { exchangeRepository.DoSomethingWithAnOrganization(organization); } catch (Exception ex) { ... } } ); } </code></pre> <p><em>Problem</em></p> <p>This works like a charm with one exception. The DoSomethingWithAnOrganization method has a try-catch and can handle some exceptions. However, whenever an Exception occurs, it isn't handled by the catch in the DoSomething-method, but immediately jups to the catch statement of the MainMethod.</p> <p>(Note: The catch-statement of the DoSomething-method is called fine if I simple use foreach(... in ...) in stead of Parallel.ForEach).</p> <p><em>Question</em></p> <p>Why can't I handle the exception in the DoSomething-method? Is there a way to DoSomething parallel differently so the catch of that method is called if an Exception occurs?</p> <p>Thank you!</p> <hr> <p>This is the DoSomething-method:</p> <pre><code>public IEnumerable&lt;Mailbox&gt; Invoke(string organizationName) { try { var command = new PSCommand() .AddCommand("Get-Mailbox") .AddParameter("Organization", organizationName); var result = Invoke(command); var mailboxes = from mailbox in result.Results select new Mailbox() { Organization = organizationName, Name = (string)mailbox.Properties["Name"].Value, Identity = mailbox.Properties["Identity"].Value.ToString(), Plan = mailbox.Properties["MailboxPlan"].Value == null ? null : (string)mailbox.Properties["MailboxPlan"].Value .GetType().GetProperty("Name") .GetValue(mailbox.Properties["MailboxPlan"].Value, null), }; return mailboxes; } catch (Exception ex) { Log.Error(ex.Message, ex); throw new Exceptions.ReportingServiceException(Exceptions.ExceptionType.Technical, ex.Message, ex); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload