Export (0) Print
Expand All

How to: Implement a Queued Component that Displays a Message Asynchronously

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

Code Example

The COM+ Queued Components service provides a way for a component class to be invoked asynchronously using Microsoft Message Queuing. For more information about queued components derived from the System.EnterpriseServices.ServicedComponent class, see Queued Components.

To implement a queued component that displays a message asynchronously

  1. Import the System.EnterpriseServices namespace.

    Imports System.EnterpriseServices
    

    using System.EnterpriseServices;
    
    
  2. Set the ApplicationActivation attribute to ActivationOption.Server; enable application queuing and the queue listener by setting the ApplicationQueuing assembly attribute, as shown in the following sample code:

    <assembly: ApplicationActivation(ActivationOption.Server)>
    <assembly: ApplicationQueuing(Enabled := True, _
                QueueListenerEnabled := True)>
    

    [assembly: ApplicationActivation(ActivationOption.Server)]
    [assembly: ApplicationQueuing(Enabled=true, QueueListenerEnabled=true)]
    
    
  3. Define a queued interface, and apply the InterfaceQueuing attribute to the interface. For example the following code shows the InterfaceQueuing attribute being applied to the IQComponent interface that has a single method DisplayMessage.

    <InterfaceQueuing>
    Public Interface IQComponent
        Sub DisplayMessage(msg As String)
    End Interface
    

    [InterfaceQueuing]
    public interface IQComponent 
    {
        void DisplayMessage(string msg);
    }
    
    
  4. Define a class that derives from the System.EnterpriseServices.ServicedComponent class and implements the queued interface. For example, the following sample code declares a class named QComponent that derives from the System.EnterpriseServices.ServicedComponent class and implements the IQComponent interface.

    Public Class QComponent 
        Inherits ServicedComponent Implements IQComponent 
        Public Sub DisplayMessage(msg As String) implements _
                              IQComponent.DisplayMessage
                MessageBox.Show(msg, "Processing message")
        End Sub
    End Class 
    
    

    public class QComponent  : ServicedComponent, IQComponent
    {
        public void DisplayMessage(string msg)
        {
            MessageBox.Show(msg, "Processing message");
        }
    }
    
  5. Create a client application and test the queued component.

    1. Create a variable whose datatype is the queued interface from which the queued component class is derived.

      Dim iQc As IQComponent = Nothing
      

      IQComponent iQc = null;
      
      
    2. Bind to the moniker corresponding to the queued interface, and call the method on the queued component to display the message asynchronously. For example the following code binds to the queued component that corresponds to the IQComponent interface, and calls the method DisplayMessage to display a message asynchronously.

      Try
          iQc =
            CType(Marshal.BindToMoniker("queue:/new:QCDemo.QComponent"), _
                  IQComponent)
      Catch l as Exception
          Console.Writeline("Caught Exception: " & l.Message)
      End Try
      iQc.DisplayMessage(messageToSend.Text)
      Marshal.ReleaseComObject(iQc)
      

      IQComponent iQc = null;
      try
      {
          iQc = (IQComponent)
                 Marshal.BindToMoniker("queue:/new:QCDemo.QComponent");
      }
      catch
      {
          MessageBox.Show("Cannot create Queued Component");
      }
      iQc.DisplayMessage (messageToSend.Text);
      Marshal.ReleaseComObject(iQc);
      
      

using System.Reflection;
using System.EnterpriseServices;

[assembly: ApplicationName("QCDemoSvr")]
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationQueuing(Enabled=true, QueueListenerEnabled=true)]
[assembly: AssemblyKeyFile("QCDemoSvr.snk")]

namespace QCDemo
{
    [InterfaceQueuing]
    public interface IQComponent 
    {
        void DisplayMessage(string msg);
    }

    public class QComponent  : ServicedComponent, IQComponent
    {
        public void DisplayMessage(string msg)
        {
            MessageBox.Show(msg, "Processing message");
        }
    }
} 
Show:
© 2015 Microsoft