Export (0) Print
Expand All

How to: Implement Loosely Coupled Events

[Note: This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Code Example

The following procedure and example show how to implement an event class and event sink that implement a common event interface, plus a publisher to fire an event. For more information about using the COM+ loosely coupled event model, see Loosely Coupled Events.

To implement loosely coupled events

  1. Define an event interface. For example, the following code defines an event interface named IlceMsg that has a method named EventMethod.

    Public Interface ILceMsg
        Sub EventMethod(message As String)
    End Interface
    

    public interface ILceMsg
    {
        void EventMethod(string message);
    }
    
  2. Define an event class that derives from the System.EnterpriseServices.ServicedComponent class and implements the event interface defined in step 1. For example, the following code defines an event class named LceClass.

    <EventClass()> _
    Public Class LceClass 
    Inherits ServicedComponent Implements ILceMsg 
        Public Sub EventMethod(message As String) implements _
            ILceMsg.EventMethod
        End Sub 
    End Class
    

    [EventClass]
    public class LceClass : ServicedComponent, ILceMsg
    {
        public void EventMethod(string message){}
    }
    
  3. Define an even sink class that derives from the System.EnterpriseServices.ServicedComponent class and implements the event interface defined in step 1. For example, the following code defines an event sink class named LceSink.

    <EventClass()> _
    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
    

    public class LceSink : ServicedComponent, ILceMsg
    {
        public void EventMethod(string message)
        {
            MessageBox.Show(message, "Event sink");
        }
    }
    
  4. Create an event publisher to trigger the LCE.

    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 
    

    protected void Fire_Click (object sender, System.EventArgs e)
    {
          ILceMsg evt = (ILceMsg) new LceClass();
          evt.EventMethod("Hello events");
    }
    

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 void Fire_Click (object sender, System.EventArgs e)
{
      ILceMsg evt = (ILceMsg) new LceClass();
      evt.EventMethod("Hello events");
}
Show:
© 2014 Microsoft