次の方法で共有


MessageQueue.Send メソッド (Object, MessageQueueTransaction)

この MessageQueue で参照されるトランザクション キューにオブジェクトを送信します。

Overloads Public Sub Send( _
   ByVal obj As Object, _   ByVal transaction As MessageQueueTransaction _)
[C#]
public void Send(objectobj,MessageQueueTransactiontransaction);
[C++]
public: void Send(Object* obj,MessageQueueTransaction* transaction);
[JScript]
public function Send(
   obj : Object,transaction : MessageQueueTransaction);

パラメータ

例外

例外の種類 条件
ArgumentNullException transaction パラメータが null 参照 (Visual Basic では Nothing) です。
MessageQueueException Path プロパティが設定されていません。

または

メッセージ キュー アプリケーションが、トランザクションの使用方法が間違っていることを示しました。

または

メッセージ キューの API にアクセスしたときにエラーが発生しました。

解説

このオーバーロードを使用すると、 transaction パラメータで定義された内部トランザクション コンテキストを使用して、 obj パラメータを含むメッセージを MessageQueue が参照するトランザクション キューに送信します。キューに送信するオブジェクトは、 Message 、または任意のマネージ オブジェクトにすることができます。 Message 以外のオブジェクトを送信した場合、オブジェクトはシリアル化され、メッセージ本文に挿入されます。

このオーバーロードを使用して非トランザクション キューにメッセージを送信すると、例外がスローされることなく、メッセージが配信不能キューに送信されることがあります。

Send を呼び出す前に Formatter プロパティを設定しないと、書式指定子が既定の XmlMessageFormatter に設定されます。

DefaultPropertiesToSend プロパティは、 Message 以外の任意のオブジェクトに適用されます。たとえば、 DefaultPropertiesToSend メンバを使用してラベルまたは優先順位を指定すると、指定した値は、アプリケーションが Message 以外の型のオブジェクトを含むメッセージをキューに送信するときに、そのメッセージに適用されます。 Message を送信する場合は、 Message に設定したプロパティ値が DefaultPropertiesToSend よりも優先され、このメッセージの Message.Formatter プロパティがキューの MessageQueue.Formatter プロパティよりも優先されます。

[Visual Basic] メモ    MessageQueueTransaction がスレッド アパートメントに対応しているため、アパートメントの状態が STA の場合は、複数のスレッドで同じトランザクションを使用できません。Visual Basic はメイン スレッドの状態を STA に設定するため、 Main サブルーチンで MTAThreadAttribute を適用する必要があります。この処理を行っていない場合は、別のスレッドを使用してトランザクション メッセージを送信すると、 MessageQueueException 例外がスローされます。 MTAThreadAttribute を適用するには、次のコード片を使用します。

 
<System.MTAThreadAttribute>
public sub Main()

このメソッドが各種のワークグループ モードで使用できるかどうかを次の表に示します。

ワークグループ モード 使用可否
ローカル コンピュータ はい
ローカル コンピュータ + 直接書式名 はい
リモート コンピュータ いいえ
リモート コンピュータ + 直接書式名 はい

使用例

[Visual Basic, C#, C++] 文字列をトランザクション キューに送信し、そのキューからメッセージを受信する例を次に示します。

 
Imports System
Imports 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 sends and receives a message from
        ' a transactional queue.
        '**************************************************

        Public Shared Sub Main()

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

            ' Send a message to a queue.
            myNewQueue.SendMessageTransactional()

            ' Receive a message from a queue.
            myNewQueue.ReceiveMessageTransactional()

            Return

        End Sub 'Main


        '**************************************************
        ' Sends a message to a queue.
        '**************************************************

        Public Sub SendMessageTransactional()

            ' Connect to a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myTransactionalQueue")

            ' Send a message to the queue.
            If myQueue.Transactional = True Then
                myQueue.Send("My Message Data.", New _
                    MessageQueueTransaction())
            End If

            Return

        End Sub 'SendMessageTransactional


        '**************************************************
        ' Receives a message containing an Order.
        '**************************************************

        Public Sub ReceiveMessageTransactional()

            ' Connect to a transactional queue on the local computer.
            Dim myQueue As New MessageQueue(".\myTransactionalQueue")

            ' Set the formatter.
            myQueue.Formatter = New XmlMessageFormatter(New Type() _
                {GetType([String])})

            ' Create a transaction.
            Dim myTransaction As New MessageQueueTransaction()

            Try

                ' Begin the transaction.
                myTransaction.Begin()

                ' Receive the message. 
                Dim myMessage As Message = _
                    myQueue.Receive(myTransaction)
                Dim myOrder As [String] = CType(myMessage.Body, _
                    [String])

                ' Display message information.
                Console.WriteLine(myOrder)

                ' Commit the transaction.
                myTransaction.Commit()


            Catch e As MessageQueueException

                ' Handle nontransactional queues.
                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.TransactionUsage Then

                    Console.WriteLine("Queue is not transactional.")

                End If

                ' Else catch other sources of a MessageQueueException.


                ' Roll back the transaction.
                myTransaction.Abort()


                ' Catch other exceptions as necessary, such as 
                ' InvalidOperationException, thrown when the formatter
                ' cannot deserialize the message.

            End Try

            Return

        End Sub 'ReceiveMessageTransactional

    End Class 'MyNewQueue
End Namespace 'MyProject

[C#] 
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 sends and receives a message from
        // a transactional queue.
        //**************************************************

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

            // Send a message to a queue.
            myNewQueue.SendMessageTransactional();

            // Receive a message from a queue.
            myNewQueue.ReceiveMessageTransactional();
        
            return;
        }


        //**************************************************
        // Sends a message to a queue.
        //**************************************************
        
        public void SendMessageTransactional()
        {
                        
            // Connect to a queue on the local computer.
            MessageQueue myQueue = new 
                MessageQueue(".\\myTransactionalQueue");

            // Send a message to the queue.
            if (myQueue.Transactional == true)
            {
                myQueue.Send("My Message Data.", new 
                    MessageQueueTransaction());
            }

            return;
        }


        //**************************************************
        // Receives a message containing an Order.
        //**************************************************
        
        public  void ReceiveMessageTransactional()
        {
            // Connect to a transactional queue on the local computer.
            MessageQueue myQueue = new 
                MessageQueue(".\\myTransactionalQueue");

            // Set the formatter.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(String)});
            
            // Create a transaction.
            MessageQueueTransaction myTransaction = new 
                MessageQueueTransaction();

            try
            {
                // Begin the transaction.
                myTransaction.Begin();
                
                // Receive the message. 
                Message myMessage =    myQueue.Receive(myTransaction); 
                String myOrder = (String)myMessage.Body;

                // Display message information.
                Console.WriteLine(myOrder);

                // Commit the transaction.
                myTransaction.Commit();

            }
            
            catch (MessageQueueException e)
            {
                // Handle nontransactional queues.
                if (e.MessageQueueErrorCode == 
                    MessageQueueErrorCode.TransactionUsage)
                { 
                    Console.WriteLine("Queue is not transactional.");
                }
                
                // Else catch other sources of MessageQueueException.

                // Roll back the transaction.
                myTransaction.Abort();
            }

            // Catch other exceptions as necessary, such as 
            // InvalidOperationException, thrown when the formatter 
            // cannot deserialize the message.

            return;
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;

/// <summary>
/// Provides a container class for the example.
/// </summary>
__gc class MyNewQueue 
{
    //*************************************************
    // Sends a message to a queue.
    //*************************************************
public:
    void SendMessageTransactional() 
    {
        // Connect to a queue on the local computer.
        MessageQueue* myQueue = new MessageQueue(S".\\myTransactionalQueue");

        // Send a message to the queue.
        if (myQueue->Transactional == true) 
        {
            myQueue->Send(S"My Message Data.", new MessageQueueTransaction());
        }

        return;
    }

    //*************************************************
    // Receives a message containing an Order.
    //*************************************************
public:
    void ReceiveMessageTransactional() 
    {
        // Connect to a transactional queue on the local computer.
        MessageQueue* myQueue = new MessageQueue(S".\\myTransactionalQueue");

        // Set the formatter.
        Type* p __gc[] = new Type* __gc[1];
        p[0] = __typeof(String);
        myQueue->Formatter = new XmlMessageFormatter( p );

        // Create a transaction.
        MessageQueueTransaction* myTransaction = new MessageQueueTransaction();

        try 
        {
            // Begin the transaction.
            myTransaction->Begin();

            // Receive the message. 
            Message* myMessage = myQueue->Receive(myTransaction); 
            String* myOrder = static_cast<String*>(myMessage->Body);

            // Display message information.
            Console::WriteLine(myOrder);

            // Commit the transaction.
            myTransaction->Commit();

        } 
        catch (MessageQueueException* e)
        {
            // Handle nontransactional queues.
            if (e->MessageQueueErrorCode == 
                MessageQueueErrorCode::TransactionUsage) 
            { 
                Console::WriteLine(S"Queue is not transactional.");
            }

            // Else catch other sources of MessageQueueException.

            // Roll back the transaction.
            myTransaction->Abort();
        }

        // Catch other exceptions as necessary, such as 
        // InvalidOperationException, thrown when the formatter 
        // cannot deserialize the message.

        return;
    }
};

//*************************************************
// Provides an entry point into the application.
// 
// This example sends and receives a message from
// a transactional queue.
//*************************************************

int main() 
{
    // Create a new instance of the class.
    MyNewQueue* myNewQueue = new MyNewQueue();

    // Send a message to a queue.
    myNewQueue->SendMessageTransactional();

    // Receive a message from a queue.
    myNewQueue->ReceiveMessageTransactional();

    return 0;
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

MessageQueue クラス | MessageQueue メンバ | System.Messaging 名前空間 | MessageQueue.Send オーバーロードの一覧 | DefaultPropertiesToSend | Message | MessageQueueTransaction | Transactional | Peek | Receive | BeginPeek | BeginReceive