语言

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 属性将其连接到现有的消息队列队列。 或者,可以设置 MessageQueue 对 Create(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 实例绑定到特定消息队列队列,请在此队列中了解路径、格式名称或标签,请使用此重载。 如果要授予对引用队列的第一个应用程序的独占访问权限,则必须将该 DenySharedReceive 属性 true 设置为或使用传递读取访问限制参数的构造函数。

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

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

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

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

Reference Syntax 示例
格式名称 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: TheLabel

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

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

财产 初始值
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

注解

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

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

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

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

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

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

Reference Syntax 示例
格式名称 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: TheLabel

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

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

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

财产 初始值
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 值。

适用于