共用方式為


MessageQueue 建構函式

定義

初始化 MessageQueue 類別的新執行個體。

多載

名稱 Description
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: 唱片公司

若要離線運作,您必須使用格式名稱語法,而不是建構函式的路徑名稱語法。 否則會擲回例外狀況,因為主要域控制器無法解析格式名稱的路徑。

下表顯示了 的 MessageQueue初始屬性值。 這些值是根據訊息排隊的屬性,並由參數指定的 path 路徑。

財產 初始值
Authenticate false
BasePriority 0
Category Empty
DefaultPropertiesToSend 由類別無 DefaultPropertiesToSend 參數建構子所設定的值。
EncryptionRequired true,若訊息佇列的隱私等級設定為「Body」;否則,。 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 綁定到特定訊息佇列時,可以使用這個過載,因為你知道該佇列的路徑、格式名稱或標籤。 如果你想對第一個引用該佇列的應用程式授予專屬存取權,請將參數設 sharedModeDenyReceivetrue。 否則,則設定 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: 唱片公司

若要離線運作,您必須使用格式名稱語法,而不是易記名稱語法。 否則會擲回例外狀況,因為主要域控制器(Active Directory 所在的位置)無法解析格式名稱的路徑。

若 a MessageQueue 開啟一個參數sharedModeDenyReceivetrue為 的MessageQueue隊列,任何隨後嘗試從隊列讀取的 者,因共享違規而產生 aMessageQueueException。 若 a MessageQueue 嘗試以獨佔模式存取佇列,而另一MessageQueue方已擁有非獨佔存取權,則 A MessageQueueException 也會被拋出。

下表顯示了 的 MessageQueue初始屬性值。 這些值是基於訊息排隊的屬性,路徑由 path 參數指定。

財產 初始值
Authenticate false
BasePriority 0.
Category Empty
DefaultPropertiesToSend 由類別無 DefaultPropertiesToSend 參數建構子所設定的值。
EncryptionRequired true,若訊息佇列的隱私等級設定為「Body」;否則,。 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 價值。

適用於