Will multithreading increase method performance in a WCF service?

I have a WCF service hosted in IIS6. The important part of the method looks like this:

public MyUser[] GetUsers(string appName, string[] names)
{
    List<User> users = new List<User>();
    foreach (string user in names)
    {
      MembershipUser mu = this.ADAMProvider.GetUser(user, false);  //Unmanaged call to AzMan
      if (mu != null)
      {
        users.Add(MyUser.CreateFrom(mu);
      }
    }
    return users.ToArray();
}

The performance of this method is very low when it is called with a large array of usernames (more than 100 or so). It may take more than a minute to return. In addition, if this method is called at the same time by more than one client, it will time out. I even saw him knock down the application pool. Note that in the loop, an AzMan call is made. AzMan is an unmanaged COM component.

To increase performance, I am considering a multi-threaded approach .. NET 4 is not an option, therefore Parallel.For is not an option, but there is an equivalent in 3.5.

( ) ? WCF, IIS6?

+3
2

-, , COM- . . , STA, , , , - . MTA, GetUser, .

, 1 ThreadPool . .

public MyUser[] GetUsers(string appName, string[] names) 
{ 
  int count = 1; // Holds the number of pending work items.
  var finished = new ManualResetEvent(false); // Used to wait for all work items to complete.
  var users = new List<User>(); 
  foreach (string user in names) 
  {
    Interlocked.Increment(ref count); // Indicate that we have another work item.
    ThreadPool.QueueUserWorkItem(
      (state) =>
      { 
        try
        {
          MembershipUser mu = this.ADAMProvider.GetUser(user, false); 
          if (mu != null) 
          { 
            lock (users)
            {
              users.Add(MyUser.CreateFrom(mu); 
            }
          } 
        }
        finally
        {
          // Signal the event if this is the last work item.
          if (Interlocked.Decrement(ref count) == 0) finished.Set();
        }
      });
  } 
  // Signal the event if this is the last work item.
  if (Interlocked.Decrement(ref count) == 0) finished.Set();
  // Wait for all work items to complete.
  finished.WaitOne();
  return users.ToArray(); 
}

, , , (, ), . . , Set WaitOne.

, , TPL .NET 3.5 Reactive Extensions .

1 , , , .

+3

, , :

, , -. , . , AzMan bening. AzMan - COM-.

, "AzMan" . , , .

, , , . , ( , ) ..

+2

Source: https://habr.com/ru/post/1773263/


All Articles