Partager via


Queued Components

The COM+ Queued Components service provides an easy way to invoke and execute components asynchronously using Microsoft Message Queuing. Processing can occur without regard to the availability or accessibility of either the sender or receiver.

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

The MaxListenerThreads property indicates the maximum number of concurrent Queued Components listener threads. The valid range for this value is 0 through 1000. For a newly created application, the setting is derived from the algorithm currently used for determining the default number of listener threads: 16 multiplied by the number of CPUs in the server. This value does not dictate the number of threads running at all times, simply the maximum number of possible threads. On an idle server, there would be only one thread running until more messages were found in the queue. Then the server would create more threads as needed until it reaches the MaxListenerThreads value. The following example sets the maximum number of Queued Components listener threads to 64.

**Note   **The string supplied to the Marshal.BindToMoniker method can contain optional parameters to specify the computer name and other information. Please refer to the "Developing Queued Components" section of the Platform SDK for more information.

<ApplicationQueuingAttribute(QueueListenerEnabled := _ 
true, MaxListenerThreads :=  64 )>
[C#]
[ApplicationQueuingAttribute(QueueListenerEnabled = true, MaxListenerThreads =  64 )]

The following two-part example shows how to implement a QComponent class on the server to display a message asynchronously and uses a client to invoke the DisplayMessage method on a Queued Component.

Server

Imports System.Reflection
Imports System.EnterpriseServices
Imports System

<assembly: ApplicationName("QCDemoSvr")>
<assembly: ApplicationActivation(ActivationOption.Server)>
<assembly: ApplicationQueuing(Enabled := True, _
            QueueListenerEnabled := True)>
<assembly: AssemblyKeyFile("QCDemoSvr.snk")>

Namespace QCDemo
   Public Interface IQComponent
      Sub DisplayMessage(msg As String)
   End Interface 
   
   <InterfaceQueuing(Interface := "IQComponent")> _
   Public Class QComponent 
   Inherits ServicedComponent Implements IQComponent 
          Public Sub DisplayMessage(msg As String) implements _
          IQComponent.DisplayMessage
             MessageBox.Show(msg, "Processing message")
          End Sub 'DisplayMessage
   End Class 
End Namespace 
[C#]
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
{
    public interface IQComponent 
    {
        void DisplayMessage(string msg);
    }

 [InterfaceQueuing(Interface = "IQComponent"]
    public class QComponent  : ServicedComponent, IQComponent
    {
        public void DisplayMessage(string msg)
        {
            MessageBox.Show(msg, "Processing message");
        }
    }
} 

Client

Protected Sub Send_Click(sender As Object, e As System.EventArgs) _
Handles send.Click
      Dim iQc As IQComponent = Nothing
      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)
End Sub 'Send_Click
[C#]
protected void Send_Click (object sender, System.EventArgs e)
{
      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);
}

See Also

Summary of Available COM+ Services | System.EnterpriseServices Namespace