Loosely Coupled Events

The loosely coupled event model provided by COM+ supports late-bound events or method calls between the publisher and subscriber and the event system. Rather than repeatedly polling the server, the event system notifies interested parties as information becomes available.

To use this service, the event class and the event sink must derive directly or indirectly from the System.EnterpriseServices.ServicedComponent class.

Note   To create persistent or transient subscriptions between the sink and the event class, use the COM+ administration objects with scripts or managed code. You can also create transient subscriptions using the Component Services administrative tool, accessible from the Windows Control Panel.

The following example creates an event interface, ILceMsg, an event class, an event sink, and a publisher. The event class and the event sink both derive from the ILceMsg interface.

Event

Imports System
Imports System.IO
Imports System.Reflection
Imports System.EnterpriseServices
Imports System.Runtime.InteropServices

<assembly: ApplicationName("EventDemo")>
<assembly: ApplicationActivation(ActivationOption.Library)>
<assembly: AssemblyKeyFile("EventDemoSvr.snk")>

Namespace EventDemo
   Public Interface ILceMsg
      Sub EventMethod(message As String)
   End Interface    
      <EventClass()> _
      Public Class LceClass 
Inherits ServicedComponent Implements ILceMsg 
      Public Sub EventMethod(message As String) implements _
            ILceMsg.EventMethod
      End Sub 
   End Class 
    
   Public Class LceSink 
   Inherits ServicedComponent Implements ILceMsg 
      Public Sub EventMethod(message As String) implements _
            ILceMsg.EventMethod
         MessageBox.Show(message, "Event sink")
      End Sub
   End Class 
End Namespace 
[C#]
using System;
using System.IO;
using System.Reflection;
using System.EnterpriseServices;
using System.Runtime.InteropServices;

[assembly: ApplicationName("EventDemo")]
[assembly: ApplicationActivation(ActivationOption.Library)]
[assembly: AssemblyKeyFile("EventDemoSvr.snk")]

namespace EventDemo
{
    public interface ILceMsg
    {
        void EventMethod(string message);
    }

    [EventClass]
    public class LceClass : ServicedComponent, ILceMsg
    {
        public void EventMethod(string message){}
    }

    public class LceSink : ServicedComponent, ILceMsg
    {   
        public void EventMethod(string message)
        {
            MessageBox.Show(message, "Event sink");
        }
    }
}

Publisher

Protected Sub Fire_Click(sender As Object, e As System.EventArgs)_
Handles fireEvent.Click
      Dim evt As ILceMsg = CType(New LceClass(), ILceMsg)
      evt.EventMethod("Hello events")
End Sub 
[C#]
protected void Fire_Click (object sender, System.EventArgs e)
{
      ILceMsg evt = (ILceMsg) new LceClass();
      evt.EventMethod("Hello events");
}

See Also

Summary of Available COM+ Services |System.EnterpriseServices Namespace