none
[E2010][EWSMA][C#] Multithreading with impersonation RRS feed

  • Question

  • Not sure if this should go in the Exchange forums or Parallel Computing forums, but I'll start here.

    I am writing a program that has to iterate over every mailbox in my company to perform various actions (> 1,000 mailboxes).  I've got this running as a single threaded app and it works just fine.  Unfortunately it takes too long to perform these tasks sequentially, so now we need to multi-thread this app.  The thought is that since the actions taken on each users' mailbox are A) well-defined, and B) the same for all users, we can launch a thread for each user and process several mailboxes at once without being blocked by processing large mailboxes.

    I wrote a wrapper class around the EWS API that handles impersonating the user as well as all the details of talking to Exchange.  A new instance of this object is instantiated in each thread, so I expected that impersonation wouldn't be a problem.  However, it doesn't appears that this is the case as I'm getting lots of ServiceResponseExceptions for items not found or access denied.  I was hoping to do this without having to impersonate before every query or locks around the EWS query as that would remove a lot of the benefits I am seeking.

    Advice is appreciated.

    Monday, July 25, 2011 8:49 PM

Answers

  • I figured this out.  The Exchange2010 variable was a class member (in the class creating the Exchange2010 object) so that other methods in the class could use it.  I moved all of the mailbox operation methods, including the Exchange2010 object to a separate class.  Then for each user mailbox, I create a new instance of the new worker class.  Now everything works just as I expected.
    • Marked as answer by TruckStuff Tuesday, July 26, 2011 3:41 PM
    Tuesday, July 26, 2011 3:40 PM

All replies

  • Hi TruckStuff,

    this forum is fine :-)

    you are writing that you have a wrapper that handles impersonation. How do you do that actually? Are you using Exchange impersonation or are you using Windows impersonation?

    Kind regards,
    Henning

    "TruckStuff" wrote in message news:8663bd1e-abda-43cc-97e7-fdeb90701b3d@communitybridge.codeplex.com...

    Not sure if this should go in the Exchange forums or Parallel Computing forums, but I'll start here.

    I am writing a program that has to iterate over every mailbox in my company to perform various actions (> 1,000 mailboxes).  I've got this running as a single threaded app and it works just fine.  Unfortunately it takes too long to perform these tasks sequentially, so now we need to multi-thread this app.  The thought is that since the actions taken on each users' mailbox are A) well-defined, and B) the same for all users, we can launch a thread for each user and process several mailboxes at once without being blocked by processing large mailboxes.

    I wrote a wrapper class around the EWS API that handles impersonating the user as well as all the details of talking to Exchange.  A new instance of this object is instantiated in each thread, so I expected that impersonation wouldn't be a problem.  However, it doesn't appears that this is the case as I'm getting lots of ServiceResponseExceptions for items not found or access denied.  I was hoping to do this without having to impersonate before every query or locks around the EWS query as that would remove a lot of the benefits I am seeking.

    Advice is appreciated.

    Monday, July 25, 2011 9:05 PM
  • Exchange impersonation.  My code looks something like this:

    public class Exchange2010
    {
    private ExchangeService _exchange;

    public Exchange2010(string userEmail)
    {
    _exchange = new ExchangeService();
    _exchange.Credentials = new WebCredentials(<Exchange Impersonator User Account>, <Exchange Impersonator Pass>);
    _exchange.Url = new Uri(<EWSMA URL>);
    _exchange.ImpersonatedUserId = new ImperonsatedUserId(ConnectingIdType.SmtpAddress, userEmail);
    }
    }

    Please forgive the formatting. I don't find it easy to paste code in these forums. :o

    And again, this works fine single threaded.

    Monday, July 25, 2011 9:20 PM
  • Hi,

    It would be interesting to know whether your code runs fine under these circumstances:

    * Multiple threads on one mailbox (but working on different items)
    * Single threaded, but start multiple instances on different mailboxes

    This way we could rule out client side issues.

    Kind regards,
    Henning Krause

    Tuesday, July 26, 2011 6:51 AM
  • I'm assuming its client side, i.e. in my code. :)

    My guess is that even though I'm declaring a new Exchange2010 object in each thread, .NET is actually re-using the same object over and over again (I've done some other reading that has me thinking that is what is going on).  Since the impersonation is taking place in a different method in the object, a different thread asks to impersonate another user before the first thread actually queries Exchange.

    I'm going to do some more coding this morning to see if I can confirm any of this.  In the mean time, other advice is appreciated.

    Tuesday, July 26, 2011 2:14 PM
  • I figured this out.  The Exchange2010 variable was a class member (in the class creating the Exchange2010 object) so that other methods in the class could use it.  I moved all of the mailbox operation methods, including the Exchange2010 object to a separate class.  Then for each user mailbox, I create a new instance of the new worker class.  Now everything works just as I expected.
    • Marked as answer by TruckStuff Tuesday, July 26, 2011 3:41 PM
    Tuesday, July 26, 2011 3:40 PM