如何:实现异步显示消息的排队组件

代码示例

COM+ 排队组件服务提供了一种使用 Microsoft 消息队列异步调用组件类的方法。有关从 System.EnterpriseServices.ServicedComponent 类派生的排队组件的详细信息,请参阅排队组件

实现异步显示消息的排队组件

  1. 导入 System.EnterpriseServices 命名空间。

    Imports System.EnterpriseServices
    
    using System.EnterpriseServices;
    
  2. ApplicationActivation 属性设置为 ActivationOption.Server;通过设置 ApplicationQueuing 程序集属性启用应用程序排队和队列侦听器,如以下示例代码所示:

    <assembly: ApplicationActivation(ActivationOption.Server)>
    <assembly: ApplicationQueuing(Enabled := True, _
                QueueListenerEnabled := True)>
    
    [assembly: ApplicationActivation(ActivationOption.Server)]
    [assembly: ApplicationQueuing(Enabled=true, QueueListenerEnabled=true)]
    
  3. 定义一个排队接口,并将 InterfaceQueuing 属性应用于该接口。例如,以下代码显示 InterfaceQueuing 属性,该属性要应用于具有单个方法 DisplayMessageIQComponent 接口。

    <InterfaceQueuing>
    Public Interface IQComponent
        Sub DisplayMessage(msg As String)
    End Interface
    
    [InterfaceQueuing]
    public interface IQComponent 
    {
        void DisplayMessage(string msg);
    }
    
  4. 定义一个类,该类从 System.EnterpriseServices.ServicedComponent 类派生并实现排队接口。例如,以下示例代码声明一个名为 QComponent 的类,该类从 System.EnterpriseServices.ServicedComponent 派生,并实现 IQComponent 接口。

    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. 创建客户端应用程序并测试该排队组件。

    1. 创建一个变量,该变量的数据类型是派生排队组件类的排队接口。

      Dim iQc As IQComponent = Nothing
      
      IQComponent iQc = null;
      
    2. 绑定到与该排队接口相对应的名字对象,对该排队组件调用相应方法以异步显示消息。例如,以下代码绑定到与 IQComponent 接口相对应的排队组件,并调用方法 DisplayMessage 以异步显示消息。

      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);
      

示例

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
    <InterfaceQueuing>
    Public Interface IQComponent
       Sub DisplayMessage(msg As String)
    End 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 
End Namespace 
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");
        }
    }
} 

请参见

参考

System.EnterpriseServices Namespace

概念

可用的 COM+ 服务摘要
排队组件

Footer image

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。