Enterprise Services (.NET 1.1) Performance Guidelines - Loosely Coupled Events

From Guidance Share
Jump to navigationJump to search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, and Alex Mackman


Consider the Fire in Parallel Option

When a publisher raises an event, the method call does not return until all subscriber components are activated and contacted. With large numbers of subscribers, this can severely affect performance. You can use the FireInParallel property to instruct the event system to use multiple threads to deliver events to subscribers.


  public interface ICustomInterface
  {
     void OnEventA();
     void OnEventB();
  }
  [EventClass(FireInParallel = true)]
  public class CustomClass : ServicedComponent, ICustomInterface
  {
     public void OnEventA();
     public void OnEventB();
  }

This approach can increase performance in certain circumstances, particularly when one or more of the subscribers take a long time to process the notification.

Note Selecting Fire in parallel does not guarantee that the event is delivered at the same time to multiple subscribers, but it instructs COM+ to permit it to happen. You can set the Fire in parallel option on the Advanced tab of the event class component's Properties dialog box.


Potential Pitfalls

Fire in parallel might mean that the subscribers gain concurrent access to the same objects. For example, if you use a DataSet as a parameter, you might end up with many threads accessing it. As a result, observe the following:


Do not use STA objects as parameters to LCE events.

Make your LCE subscribers read only the data. Otherwise, you might run into synchronization issues where subscriber A writes to the same object at the same time as subscriber B reads from it.


Avoid LCE for Multicast Scenarios

Evaluate whether you have too many subscribers for an event, because LCE is not designed for large multicast scenarios where large numbers of subscribers need to be notified. For this scenario, you usually do not know or do not care whether notifications are received, and you do not want to block awaiting a response from each subscriber.

When you have large numbers of subscribers, a good alternative is to use User Datagram Protocol (UDP) to deliver messages over the network; for example, by using the Socket class.


Use Queued Components with LCE from ASP.NET

If you want to publish events from an ASP.NET application, configure the event class as a queued component. This causes the event to be published asynchronously and does not block the main thread servicing the current ASP.NET request.


Do Not Subscribe to LCE Events from ASP.NET

The transient nature of the page class makes it difficult to subscribe to loosely coupled events from an ASP.NET application without blocking and waiting for the event to occur. This approach is not recommended.


References

For more information, see Microsoft Knowledge Base article 318185, "HOW TO: Use Loosely Coupled Events from Visual Studio .NET," at http://support.microsoft.com/default.aspx?scid=kb;en-us;318185.