MessageQueue 构造函数

定义

初始化 MessageQueue 类的新实例。

重载

MessageQueue()

初始化 MessageQueue 类的新实例。 无参数构造函数初始化新实例后,必须设置该实例的 Path 属性才能使用该实例。

MessageQueue(String)

初始化 MessageQueue 类的新实例,该实例引用指定路径处的“消息队列”队列。

MessageQueue(String, Boolean)

初始化 MessageQueue 类的新实例,该实例引用位于指定路径处而且具有指定读访问限制的“消息队列”队列。

MessageQueue(String, QueueAccessMode)

初始化 MessageQueue 类的新实例。

MessageQueue(String, Boolean, Boolean)

初始化 MessageQueue 类的新实例。

MessageQueue(String, Boolean, Boolean, QueueAccessMode)

初始化 MessageQueue 类的新实例。

MessageQueue()

初始化 MessageQueue 类的新实例。 无参数构造函数初始化新实例后,必须设置该实例的 Path 属性才能使用该实例。

public:
 MessageQueue();
public MessageQueue ();
Public Sub New ()

示例

下面的代码示例创建新的 MessageQueue

// Connect to a queue on the local computer. You must set the queue's
// Path property before you can use the queue.
MessageQueue queue = new MessageQueue();
queue.Path = ".\\exampleQueue";

注解

使用此重载创建 类的新实例,该实例 MessageQueue 不会立即绑定到消息队列服务器上的队列。 使用此实例之前,必须通过设置 Path 属性将其连接到现有的消息队列队列。 或者,可以设置对MessageQueueCreate(String)方法的返回值的引用,从而创建新的消息队列队列。

构造 MessageQueue 函数实例化 类的新实例 MessageQueue ;它不会创建新的消息队列队列。

下表显示了 实例 MessageQueue的初始属性值。

属性 初始值
DefaultPropertiesToSend 由 类的无参数构造函数 DefaultPropertiesToSend 设置的值。
Formatter XmlMessageFormatter
MessageReadPropertyFilter 由 类的无参数构造函数 MessagePropertyFilter 设置的值。 所有筛选器值都设置为 true
DenySharedReceive false

另请参阅

适用于

MessageQueue(String)

初始化 MessageQueue 类的新实例,该实例引用指定路径处的“消息队列”队列。

public:
 MessageQueue(System::String ^ path);
public MessageQueue (string path);
new System.Messaging.MessageQueue : string -> System.Messaging.MessageQueue
Public Sub New (path As String)

参数

path
String

MessageQueue 引用的队列的位置。

例外

Path 属性无效,很可能因为尚未设置该属性。

示例

下面的代码示例使用各种路径名称语法类型创建新 MessageQueue 对象。 在每种情况下,它都会向在构造函数中定义其路径的队列发送消息。

#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;
ref class MyNewQueue
{
public:

   // References public queues.
   void SendPublic()
   {
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );
      myQueue->Send( "Public queue by path name." );
      return;
   }


   // References private queues.
   void SendPrivate()
   {
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\Private$\\myQueue" );
      myQueue->Send( "Private queue by path name." );
      return;
   }


   // References queues by label.
   void SendByLabel()
   {
      MessageQueue^ myQueue = gcnew MessageQueue( "Label:TheLabel" );
      myQueue->Send( "Queue by label." );
      return;
   }


   // References queues by format name.
   void SendByFormatName()
   {
      MessageQueue^ myQueue = gcnew MessageQueue( "FormatName:Public=5A5F7535-AE9A-41d4 -935C-845C2AFF7112" );
      myQueue->Send( "Queue by format name." );
      return;
   }


   // References computer journal queues.
   void MonitorComputerJournal()
   {
      MessageQueue^ computerJournal = gcnew MessageQueue( ".\\Journal$" );
      while ( true )
      {
         Message^ journalMessage = computerJournal->Receive();
         
         // Process the journal message.
      }
   }


   // References queue journal queues.
   void MonitorQueueJournal()
   {
      MessageQueue^ queueJournal = gcnew MessageQueue( ".\\myQueue\\Journal$" );
      while ( true )
      {
         Message^ journalMessage = queueJournal->Receive();
         
         // Process the journal message.
      }
   }


   // References dead-letter queues.
   void MonitorDeadLetter()
   {
      MessageQueue^ deadLetter = gcnew MessageQueue( ".\\DeadLetter$" );
      while ( true )
      {
         Message^ deadMessage = deadLetter->Receive();
         
         // Process the dead-letter message.
      }
   }


   // References transactional dead-letter queues.
   void MonitorTransactionalDeadLetter()
   {
      MessageQueue^ TxDeadLetter = gcnew MessageQueue( ".\\XactDeadLetter$" );
      while ( true )
      {
         Message^ txDeadLetter = TxDeadLetter->Receive();
         
         // Process the transactional dead-letter message.
      }
   }

};


//*************************************************
// Provides an entry point into the application.
//         
// This example demonstrates several ways to set
// a queue's path.
//*************************************************
int main()
{
   
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;
   myNewQueue->SendPublic();
   myNewQueue->SendPrivate();
   myNewQueue->SendByLabel();
   myNewQueue->SendByFormatName();
   myNewQueue->MonitorComputerJournal();
   myNewQueue->MonitorQueueJournal();
   myNewQueue->MonitorDeadLetter();
   myNewQueue->MonitorTransactionalDeadLetter();
   return 0;
}
using System;
using System.Messaging;

namespace MyProject
{
    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        //		
        // This example demonstrates several ways to set
        // a queue's path.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            myNewQueue.SendPublic();
            myNewQueue.SendPrivate();
            myNewQueue.SendByLabel();
            myNewQueue.SendByFormatName();
            myNewQueue.MonitorComputerJournal();
            myNewQueue.MonitorQueueJournal();
            myNewQueue.MonitorDeadLetter();
            myNewQueue.MonitorTransactionalDeadLetter();

            return;
        }
        
        // References public queues.
        public void SendPublic()
        {
            MessageQueue myQueue = new MessageQueue(".\\myQueue");
            myQueue.Send("Public queue by path name.");

            return;
        }

        // References private queues.
        public void SendPrivate()
        {
            MessageQueue myQueue = new
                MessageQueue(".\\Private$\\myQueue");
            myQueue.Send("Private queue by path name.");

            return;
        }

        // References queues by label.
        public void SendByLabel()
        {
            MessageQueue myQueue = new MessageQueue("Label:TheLabel");
            myQueue.Send("Queue by label.");

            return;
        }

        // References queues by format name.
        public void SendByFormatName()
        {
            MessageQueue myQueue = new
                MessageQueue("FormatName:Public=5A5F7535-AE9A-41d4" +
                "-935C-845C2AFF7112");
            myQueue.Send("Queue by format name.");

            return;
        }

        // References computer journal queues.
        public void MonitorComputerJournal()
        {
            MessageQueue computerJournal = new
                MessageQueue(".\\Journal$");
            while(true)
            {
                Message journalMessage = computerJournal.Receive();
                // Process the journal message.
            }
        }

        // References queue journal queues.
        public void MonitorQueueJournal()
        {
            MessageQueue queueJournal = new
                MessageQueue(".\\myQueue\\Journal$");
            while(true)
            {
                Message journalMessage = queueJournal.Receive();
                // Process the journal message.
            }
        }
        
        // References dead-letter queues.
        public void MonitorDeadLetter()
        {
            MessageQueue deadLetter = new
                MessageQueue(".\\DeadLetter$");
            while(true)
            {
                Message deadMessage = deadLetter.Receive();
                // Process the dead-letter message.
            }
        }

        // References transactional dead-letter queues.
        public void MonitorTransactionalDeadLetter()
        {
            MessageQueue TxDeadLetter = new
                MessageQueue(".\\XactDeadLetter$");
            while(true)
            {
                Message txDeadLetter = TxDeadLetter.Receive();
                // Process the transactional dead-letter message.
            }
        }
    }
}
Imports System.Messaging

Public Class MyNewQueue


        
        ' Provides an entry point into the application.
        '		 
        ' This example demonstrates several ways to set
        ' a queue's path.
        

        Public Shared Sub Main()

            ' Create a new instance of the class.
            Dim myNewQueue As New MyNewQueue()

            myNewQueue.SendPublic()
            myNewQueue.SendPrivate()
            myNewQueue.SendByLabel()
            myNewQueue.SendByFormatName()
            myNewQueue.MonitorComputerJournal()
            myNewQueue.MonitorQueueJournal()
            myNewQueue.MonitorDeadLetter()
            myNewQueue.MonitorTransactionalDeadLetter()

            Return

        End Sub


        ' References public queues.
        Public Sub SendPublic()

            Dim myQueue As New MessageQueue(".\myQueue")
            myQueue.Send("Public queue by path name.")

            Return

        End Sub


        ' References private queues.
        Public Sub SendPrivate()

            Dim myQueue As New MessageQueue(".\Private$\myQueue")
            myQueue.Send("Private queue by path name.")

            Return

        End Sub


        ' References queues by label.
        Public Sub SendByLabel()

            Dim myQueue As New MessageQueue("Label:TheLabel")
            myQueue.Send("Queue by label.")

            Return

        End Sub


        ' References queues by format name.
        Public Sub SendByFormatName()

            Dim myQueue As New _
                MessageQueue("FormatName:Public=" + _
                    "5A5F7535-AE9A-41d4-935C-845C2AFF7112")
            myQueue.Send("Queue by format name.")

            Return

        End Sub


        ' References computer journal queues.
        Public Sub MonitorComputerJournal()

            Dim computerJournal As New MessageQueue(".\Journal$")

            While True

                Dim journalMessage As Message = _
                    computerJournal.Receive()

                ' Process the journal message.

            End While

            Return
        End Sub


        ' References queue journal queues.
        Public Sub MonitorQueueJournal()

            Dim queueJournal As New _
                            MessageQueue(".\myQueue\Journal$")

            While True

                Dim journalMessage As Message = _
                    queueJournal.Receive()

                ' Process the journal message.

            End While

            Return
        End Sub


        ' References dead-letter queues.
        Public Sub MonitorDeadLetter()
            Dim deadLetter As New MessageQueue(".\DeadLetter$")

            While True

                Dim deadMessage As Message = deadLetter.Receive()

                ' Process the dead-letter message.

            End While

            Return

        End Sub


        ' References transactional dead-letter queues.
        Public Sub MonitorTransactionalDeadLetter()

            Dim TxDeadLetter As New MessageQueue(".\XactDeadLetter$")

            While True

                Dim txDeadLetterMessage As Message = _
                    TxDeadLetter.Receive()

                ' Process the transactional dead-letter message.

            End While

            Return

        End Sub

End Class

注解

如果要将新 MessageQueue 实例绑定到特定消息队列队列(知道其路径、格式名称或标签),请使用此重载。 如果要授予对引用队列的第一个应用程序的独占访问权限,必须将 属性设置为 DenySharedReceivetrue 或使用传递读取访问限制参数的构造函数。

构造 MessageQueue 函数实例化 类的新实例 MessageQueue ;它不会创建新的消息队列队列。 若要在消息队列中创建新队列,请使用 Create(String)

参数的 path 语法取决于它引用的队列类型,如下表所示。

队列类型 语法
公共队列 MachineName\QueueName
专用队列 MachineName\Private$\QueueName
日志队列 MachineName\QueueName\Journal$
计算机日记队列 MachineName\Journal$
计算机死信队列 MachineName\Deadletter$
计算机事务死信队列 MachineName\XactDeadletter$

或者,可以使用 FormatNameLabel 来描述队列路径,如下表所示。

参考 语法 示例
格式名 FormatName: [ 格式名称 ] FormatName:Public= 5A5F7535-AE9A-41d4-935C-845C2AFF7112

FormatName:DIRECT=SPX:NetworkNumber; HostNumber\QueueName

FormatName:DIRECT=TCP: IPAddress\QueueName

FormatName:DIRECT=OS: MachineName\QueueName
Label Label: [ label ] Label: TheLabel

若要脱机工作,必须使用格式名称语法,而不是构造函数的路径名称语法。 否则,将引发异常,因为主域控制器无法解析格式名称的路径。

下表显示了 实例 MessageQueue的初始属性值。 这些值基于具有 参数指定 path 路径的消息队列的属性。

Property 初始值
Authenticate false
BasePriority 0
Category Empty
DefaultPropertiesToSend 由 类的无参数构造函数 DefaultPropertiesToSend 设置的值。
EncryptionRequired true如果消息队列的隐私级别设置为“正文”,则为 ;否则为 false
Formatter XmlMessageFormatter
Label Empty
MachineName 消息队列的计算机名称属性的值。
MaximumJournalSize InfiniteQueueSize
MaximumQueueSize InfiniteQueueSize
MessageReadPropertyFilter 由 类的无参数构造函数 MessagePropertyFilter 设置的值。
Path Empty如果构造函数未设置,则为 。
QueueName Empty如果构造函数未设置,则为 。
DenySharedReceive false
UseJournalQueue true如果启用了消息队列对象的日记设置,则为 ;否则为 false

另请参阅

适用于

MessageQueue(String, Boolean)

初始化 MessageQueue 类的新实例,该实例引用位于指定路径处而且具有指定读访问限制的“消息队列”队列。

public:
 MessageQueue(System::String ^ path, bool sharedModeDenyReceive);
public MessageQueue (string path, bool sharedModeDenyReceive);
new System.Messaging.MessageQueue : string * bool -> System.Messaging.MessageQueue
Public Sub New (path As String, sharedModeDenyReceive As Boolean)

参数

path
String

MessageQueue 引用的队列的位置,它对于本地计算机可以是“.”。

sharedModeDenyReceive
Boolean

true,授予访问该队列的第一个应用程序独占读访问权;否则为 false

例外

Path 属性无效,很可能因为尚未设置该属性。

示例

下面的代码示例创建具有独占访问权限的新 MessageQueue ,设置其路径,并将消息发送到队列。

#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;
ref class MyNewQueue
{
public:

   // Requests exlusive read access to the queue. If
   // access is granted, receives a message from the 
   // queue.
   void GetExclusiveAccess()
   {
      try
      {
         
         // Request exclusive read access to the queue.
         MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue",true );
         
         // Receive a message. This is where SharingViolation 
         // exceptions would be thrown.
         Message^ myMessage = myQueue->Receive();
      }
      catch ( MessageQueueException^ e ) 
      {
         
         // Handle request for denial of exclusive read access.
         if ( e->MessageQueueErrorCode == MessageQueueErrorCode::SharingViolation )
         {
            Console::WriteLine( "Denied exclusive read access" );
         }

         
         // Handle other sources of a MessageQueueException.
      }

      
      // Handle other exceptions as necessary.
      return;
   }

};


// Provides an entry point into the application.
// This example connects to a message queue, and
// requests exclusive read access to the queue.
int main()
{
   
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;
   
   // Output the count of Lowest priority messages.
   myNewQueue->GetExclusiveAccess();
   return 0;
}
using System;
using System.Messaging;

namespace MyProject
{
    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {
        //**************************************************
        // Provides an entry point into the application.
        //		
        // This example connects to a message queue, and
        // requests exclusive read access to the queue.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Output the count of Lowest priority messages.
            GetExclusiveAccess();
                        
            return;
        }

        //**************************************************
        // Requests exlusive read access to the queue. If
        // access is granted, receives a message from the
        // queue.
        //**************************************************
        
        public static void GetExclusiveAccess()
        {
            try
            {
                // Request exclusive read access to the queue.
                MessageQueue myQueue = new
                    MessageQueue(".\\myQueue", true);

                // Receive a message. This is where SharingViolation
                // exceptions would be thrown.
                Message myMessage = myQueue.Receive();
            }
            
            catch (MessageQueueException e)
            {
                // Handle request for denial of exclusive read access.
                if (e.MessageQueueErrorCode ==
                    MessageQueueErrorCode.SharingViolation)
                {
                    Console.WriteLine("Denied exclusive read access");
                }

                // Handle other sources of a MessageQueueException.
            }

            // Handle other exceptions as necessary.

            return;
        }
    }
}
Imports System.Messaging

Public Class MyNewQueue


        ' Provides an entry point into the application.
        '		 
        ' This example connects to a message queue, and
        ' requests exclusive read access to the queue.
 

        Public Shared Sub Main()

            ' Create a new instance of the class.
            Dim myNewQueue As New MyNewQueue()

            ' Output the count of Lowest priority messages.
            myNewQueue.GetExclusiveAccess()

            Return

        End Sub


  
        ' Requests exlusive read access to the queue. If
        ' access is granted, receives a message from the 
        ' queue.
  

        Public Sub GetExclusiveAccess()

            Try

                ' Request exclusive read access to the queue.
                Dim myQueue As New MessageQueue(".\myQueue", True)

                ' Receive a message. This is where a SharingViolation 
                ' exception would be thrown.
                Dim myMessage As Message = myQueue.Receive()

            Catch e As MessageQueueException

                ' Handle request for denial of exclusive read access.
                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.SharingViolation Then

                    Console.WriteLine("Denied exclusive read access.")

                End If

                ' Handle other sources of a MessageQueueException.

                ' Handle other exceptions as necessary.

            End Try

            Return

        End Sub

End Class

注解

如果要将 new MessageQueue 绑定到特定消息队列,你知道其路径、格式名称或标签,请使用此重载。 如果要授予对引用队列的第一个应用程序的独占访问权限,请将 sharedModeDenyReceive 参数设置为 true。 否则,请将 设置为 sharedModeDenyReceivefalse 或使用只有 参数的 path 构造函数。

将 设置为 sharedModeDenyReceivetrue 会影响访问消息队列队列的所有对象,包括其他应用程序。 参数的效果不限于此应用程序。

构造 MessageQueue 函数创建 类的新实例 MessageQueue ;它不会创建新的消息队列队列。 若要在消息队列中创建新队列,请使用 Create(String)

参数的 path 语法取决于队列的类型。

队列类型 语法
公共队列 MachineName\QueueName
专用队列 MachineName\Private$\QueueName
日志队列 MachineName\QueueName\Journal$
计算机日记队列 MachineName\Journal$
计算机死信队列 MachineName\Deadletter$
计算机事务死信队列 MachineName\XactDeadletter$

或者,可以使用消息队列队列的格式名称或标签来描述队列路径。

参考 语法 示例
格式名 FormatName: [ 格式名称 ] FormatName:Public= 5A5F7535-AE9A-41d4-935C-845C2AFF7112

FormatName:DIRECT=SPX:NetworkNumber; HostNumber\QueueName

FormatName:DIRECT=TCP: IPAddress\QueueName

FormatName:DIRECT=OS: MachineName\QueueName
Label Label: [ label ] Label: TheLabel

若要脱机工作,必须使用格式名称语法,而不是友好名称语法。 否则,将引发异常,因为 Active Directory 所在的主域控制器 () 无法解析格式名称的路径。

MessageQueue如果 打开一个队列,sharedModeDenyReceive并将 参数设置为 true,则MessageQueue任何随后尝试从队列中读取的内容都会MessageQueueException由于共享冲突而生成 。 MessageQueueException如果 MessageQueue 尝试以独占模式访问队列,而另一个MessageQueue已具有对队列的非独占访问权限,则也会引发 。

下表显示了 实例 MessageQueue的初始属性值。 这些值基于消息队列的属性,其路径由 path 参数指定。

Property 初始值
Authenticate false.
BasePriority 0。
Category Empty.
DefaultPropertiesToSend 由 类的无参数构造函数 DefaultPropertiesToSend 设置的值。
EncryptionRequired true如果消息队列的隐私级别设置为“正文”,则为 ;否则为 false
Formatter XmlMessageFormatter.
Label Empty.
MachineName 消息队列的计算机名称属性的值。
MaximumJournalSize InfiniteQueueSize.
MaximumQueueSize InfiniteQueueSize.
MessageReadPropertyFilter 由 类的无参数构造函数 MessagePropertyFilter 设置的值。
Path Empty如果构造函数未设置,则为 。
QueueName Empty如果构造函数未设置,则为 。
DenySharedReceive sharedModeDenyReceive 参数的值。
UseJournalQueue true如果启用了消息队列对象的日记设置,则为 ;否则为 false

另请参阅

适用于

MessageQueue(String, QueueAccessMode)

初始化 MessageQueue 类的新实例。

public:
 MessageQueue(System::String ^ path, System::Messaging::QueueAccessMode accessMode);
public MessageQueue (string path, System.Messaging.QueueAccessMode accessMode);
new System.Messaging.MessageQueue : string * System.Messaging.QueueAccessMode -> System.Messaging.MessageQueue
Public Sub New (path As String, accessMode As QueueAccessMode)

参数

path
String

MessageQueue 引用的队列的位置,它对于本地计算机可以是“.”。

accessMode
QueueAccessMode

QueueAccessMode 值之一。

适用于

MessageQueue(String, Boolean, Boolean)

初始化 MessageQueue 类的新实例。

public:
 MessageQueue(System::String ^ path, bool sharedModeDenyReceive, bool enableCache);
public MessageQueue (string path, bool sharedModeDenyReceive, bool enableCache);
new System.Messaging.MessageQueue : string * bool * bool -> System.Messaging.MessageQueue
Public Sub New (path As String, sharedModeDenyReceive As Boolean, enableCache As Boolean)

参数

path
String

MessageQueue 引用的队列的位置,它对于本地计算机可以是“.”。

sharedModeDenyReceive
Boolean

true,授予访问该队列的第一个应用程序独占读访问权;否则为 false

enableCache
Boolean

如果创建和使用连接缓存,则为 true;否则为 false

示例

下面的代码示例创建一个具有独占读取访问权限且启用了连接缓存的 。MessageQueue

// Connect to a queue on the local computer, grant exclusive read
// access to the first application that accesses the queue, and
// enable connection caching.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue", true, true);

queue->Close();
// Connect to a queue on the local computer, grant exclusive read
// access to the first application that accesses the queue, and
// enable connection caching.
MessageQueue queue = new MessageQueue(".\\exampleQueue", true, true);

适用于

MessageQueue(String, Boolean, Boolean, QueueAccessMode)

初始化 MessageQueue 类的新实例。

public:
 MessageQueue(System::String ^ path, bool sharedModeDenyReceive, bool enableCache, System::Messaging::QueueAccessMode accessMode);
public MessageQueue (string path, bool sharedModeDenyReceive, bool enableCache, System.Messaging.QueueAccessMode accessMode);
new System.Messaging.MessageQueue : string * bool * bool * System.Messaging.QueueAccessMode -> System.Messaging.MessageQueue
Public Sub New (path As String, sharedModeDenyReceive As Boolean, enableCache As Boolean, accessMode As QueueAccessMode)

参数

path
String

MessageQueue 引用的队列的位置,它对于本地计算机可以是“.”。

sharedModeDenyReceive
Boolean

true,授予访问该队列的第一个应用程序独占读访问权;否则为 false

enableCache
Boolean

如果创建和使用连接缓存,则为 true;否则为 false

accessMode
QueueAccessMode

QueueAccessMode 值之一。

适用于