クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
.NET Framework 3.5
.NET Framework 3.5
Message クラス

  低帯域幅での表示をオンにする
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2008/.NET Framework 3.5

その他のバージョンについては、以下の情報を参照してください。
.NET Framework クラス ライブラリ
Message クラス

更新 : 2007 年 11 月

分散環境でのエンドポイント間の通信の単位を表します。

名前空間 :  System.ServiceModel.Channels
アセンブリ :  System.ServiceModel (System.ServiceModel.dll 内)
Visual Basic (宣言)
Public MustInherit Class Message _
    Implements IDisposable
Visual Basic (使用法)
Dim instance As Message
C#
public abstract class Message : IDisposable
Visual C++
public ref class Message abstract : IDisposable
J#
public abstract class Message implements IDisposable
JScript
public abstract class Message implements IDisposable

Message クラスは、ネットワーク上で送信側と受信側の間で任意の情報を通信するための方法を提供します。情報の転送、対処方法の提案または要求、またはデータの要求のために使用できます。

Message オブジェクトの構造は、SOAP エンベロープを表します。このオブジェクトは、メッセージの本文と、Headers クラスで表されるオプションのヘッダーのコレクションという 2 つの別個の部分で構成されます。このメッセージの内容は、送信者から受信者に送られるアプリケーションで定義されたデータです。メッセージ ヘッダーによりシステムやアプリケーションが拡張可能となり、変化する要件に適合できます。これは、特定のヘッダーを処理して応答するコードを定義できるためです。独自のヘッダーを定義することもできます。メッセージ ヘッダーは、メッセージの内容と一緒にシリアル化および逆シリアル化されます。

メッセージは、特定の形式で送受信されます。標準のテキスト ベースの XML 形式と、バイナリ ベースの XML 形式という 2 つの形式がサポートされます。SOAP 1.1 および SOAP 1.2 の両エンベロープを表すために Message オブジェクトを使用できます。Message のインスタンスは、作成時に修正され、特定の SOAP バージョンに関連付けられることに注意してください。Version プロパティは、メッセージの SOAP バージョンを表します。

Message オブジェクトは、WriteMessage メソッドを使用することにより、外部ストアにシリアル化できます。メッセージのプロパティもシリアル化できますが、個々に識別し、個別にシリアル化する必要があります。メモリ内 Message オブジェクトを作成するためのメッセージの逆シリアル化は、CreateMessage を使用して実行できます。プロパティは、やはり、個別に逆シリアル化し、特定の Message インスタンスに対するプロパティ コレクションに手動で追加する必要があります。

Message オブジェクトのサイズは、それが送信するデータのサイズに固定されます。すべての本文は XmlReader のインスタンスとしてモデル化され、XmlReader のインスタンスがラップするストリームのサイズには事前に定義された制限はありません。ただし、特定のチャネル プロバイダでは、各自が処理するメッセージのサイズが制限されている可能性があります。

Message には、メッセージを調査して処理したエンティティが生成する有用な情報で注釈を付けることができます。この機能は、Headers プロパティと Properties プロパティによって提供されます。Headers コレクションは、メッセージの一連の SOAP ヘッダーを表します。

Properties プロパティは、メッセージに対する処理レベルの注釈を表します。ヘッダー内の情報はネットワークを介して送信されるので、ヘッダーを調査するエンティティは、ヘッダーにより使用されるプロトコルの基になるバージョンをサポートする必要があります。ただし、プロパティはメッセージを注釈するバージョンに対して、より独立した方法を提供しています。

Message インスタンスを作成するには、いずれかの CreateMessage メソッドを使用します。

メッセージのコンシューマは、メッセージの内容へのアクセスが終了したら必ず、Close を呼び出すことをお勧めします。このようにすることで、メッセージの有効期間に関連付けられている有限のシステム リソース (ソケットや名前付きパイプなど) が解放されます。

このクラスから派生するマネージ C++ ユーザーに対する特別な注意 :

  • クリーンアップ コードは、デストラクタではなく (On)(Begin)Close (または OnAbort のどちらか一方または両方) に置いてください。

  • デストラクタは使用しないでください。使用すると、コンパイラが IDisposable を自動生成します。

  • 非参照メンバを使用しないでください。使用すると、コンパイラが IDisposable を自動生成します。

  • ファイナライザを使用しないでください。使用する場合は、ビルド警告を抑制し、SuppressFinalize(Object) を呼び出し、ファイナライザ自体を (On)(Begin)Close (または OnAbort のどちらか一方または両方) から呼び出して、自動生成される IDisposable の動作をエミュレートする必要があります。

継承元へのメモ :

Message から継承する場合は、Headers メンバと Version メンバをオーバーライドする必要があります。

次のコード例は、メッセージを送信して応答を読み取るためにチャネル ファクトリを使用する基本的なクライアントを示しています。

C#
using System;
using System.Messaging;

public class Order
{
    private int _orderId;
    private DateTime _orderTime;

    public int orderId
    {
        get
        {
            return _orderId;
        }
        set
        {
            _orderId = value;
        }
    }
    public DateTime orderTime
    {
        get
        {
            return _orderTime;
        }
        set
        {
            _orderTime = value;
        }
    }

    public Order()
    {
    }
};    

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

        try
        {
            // Create a non-transactional queue on the local computer.
            // Note that the queue might not be immediately accessible, and
            // therefore this example might throw an exception of type
            // System.Messaging.MessageQueueException when trying to send a
            // message to the newly created queue.
            CreateQueue(".\\orderQueue", false);

            // Send a message to a queue.
            processor.SendMessage();

            // Receive a message from a queue.
            processor.ReceiveMessage();

            // Create a transactional queue on the local computer.
            CreateQueue(".\\orderTransQueue", true);

            // Send a message to a transactional queue.
            processor.SendMessageToTransQueue();

            // Receive a message from a transactional queue.
            processor.ReceiveMessageFromTransQueue();
        }
        catch(System.Exception e)
        {
            // Write the exception information to the console.
            Console.WriteLine(e);
        }
    }

    // Creates a new queue.
    public static void CreateQueue(string queuePath, bool transactional)
    {
        if(!MessageQueue.Exists(queuePath))
        {
            MessageQueue.Create(queuePath, transactional);
        }
        else
        {
            Console.WriteLine(queuePath + " already exists.");
        }
    }

    // Sends an Order to a queue.
    public void SendMessage()
    {
        // Create a new order and set values.
        Order sentOrder = new Order();
        sentOrder.orderId = 3;
        sentOrder.orderTime = DateTime.Now;

        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\orderQueue");

        // Create the new order.
        Message orderMessage = new Message(sentOrder);

        // Set the message's TimeToReachQueue property to five minutes.
        orderMessage.TimeToReachQueue = TimeSpan.FromMinutes(5.0);

        // Display the new value of the message's TimeToReachQueue property.
        Console.WriteLine("Message.TimeToReachQueue: {0}",
            orderMessage.TimeToReachQueue.ToString());

        // Set the message's TimeToBeReceived property to five minutes.
        orderMessage.TimeToBeReceived = TimeSpan.FromMinutes(5.0);

        // Display the new value of the message's TimeToBeReceived property.
        Console.WriteLine("Message.TimeToBeReceived: {0}",
            orderMessage.TimeToBeReceived.ToString());

        // Set the message's UseTracing property to true.
        orderMessage.UseTracing = true;

        // Display the new value of the message's UseTracing property.
        Console.WriteLine("Message.UseTracing: {0}", orderMessage.UseTracing);

        // Set the message's Label property.
        orderMessage.Label = "Order Message";

        // Display the new value of the message's Label property.
        Console.WriteLine("Message Label: {0}", orderMessage.Label);

        // Set the message's AttachSenderId property.
        orderMessage.AttachSenderId = true;

        // Display the new value of the message's AttachSenderId property.
        Console.WriteLine("Message.AttachSenderId: {0}",
            orderMessage.AttachSenderId);

        // Set the message's Recoverable property.
        orderMessage.Recoverable = true;

        // Display the new value of the message's Recoverable property.
        Console.WriteLine("Message.Recoverable: {0}",
            orderMessage.Recoverable);

        // Set the message's ResponseQueue property.
        // (You must have a connection to a queue.)
        orderMessage.ResponseQueue = queue;

        // Display the new value of the message's ResponseQueue property.
        Console.WriteLine("Message.ResponseQueue.QueueName: {0}",
            orderMessage.ResponseQueue.QueueName);

        // Set the message's TimeToReachQueue property to Message.InfiniteTimeout.
        orderMessage.TimeToReachQueue = Message.InfiniteTimeout;

        // Display the new value of the message's TimeToReachQueue property.
        Console.WriteLine("Message.TimeToReachQueue: {0}",
            orderMessage.TimeToReachQueue.ToString());

        // Set the message's UseDeadLetterQueue property.
        orderMessage.UseDeadLetterQueue = true;

        // Display the new value of the message's UseDeadLetterQueue property.
        Console.WriteLine("Message.UseDeadLetterQueue: {0}",
            orderMessage.UseDeadLetterQueue);

        // Set the message's UseJournalQueue property.
        orderMessage.UseJournalQueue = true;

        // Display the new value of the message's UseJournalQueue property.
        Console.WriteLine("Message.UseJournalQueue: {0}",
            orderMessage.UseJournalQueue);

        // Set the message's UseEncryption property.
        orderMessage.UseEncryption = true;

        // Display the new value of the message's UseEncryption property.
        Console.WriteLine("Message.UseEncryption: {0}",
            orderMessage.UseEncryption);

        // Send the order to the queue.
        queue.Send(orderMessage);
    }

    // Receives a message containing an order.
    public void ReceiveMessage()
    {
        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\orderQueue");

        // Set the formatter to indicate the message body contains an order.
        queue.Formatter = new XmlMessageFormatter(new Type[]
            {typeof(Order)});

        // Set the queue's MessageReadPropertyFilter properties to ensure that
        // the message includes the properties in which we are interested.
        queue.MessageReadPropertyFilter.ArrivedTime = true;
        queue.MessageReadPropertyFilter.DestinationQueue = true;
        queue.MessageReadPropertyFilter.SenderVersion = true;
        queue.MessageReadPropertyFilter.SentTime = true;
        queue.MessageReadPropertyFilter.SourceMachine = true;
        queue.MessageReadPropertyFilter.Authenticated = true;

        // Receive the message. Time out after ten seconds in case the message
        // was not delivered.
        Message orderMessage = queue.Receive(TimeSpan.FromSeconds(10.0));

        // Format the message.
        Order sentOrder = (Order)orderMessage.Body;

        // Display message information.
        Console.WriteLine("Order ID: {0}", sentOrder.orderId.ToString());
        Console.WriteLine("Sent: {0}", sentOrder.orderTime.ToString());

        // Display the value of the message's MessageType property.
        Console.WriteLine("Message Type: {0}", orderMessage.MessageType);

        // Display the value of the message's SourceMachine property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.SourceMachine property must be set to
        // true before the message is received.
        Console.WriteLine("Source Machine: {0}", orderMessage.SourceMachine);

        // Display the value of the message's DestinationQueue property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.DestinationQueue property must be set to
        // true before the message is received.
        Console.WriteLine("Message.DestinationQueue.QueueName: {0}",
            orderMessage.DestinationQueue.QueueName);

        // Display the value of the message's SentTime property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.SentTime property must be set to
        // true before the message is received.
        Console.WriteLine("Time sent from source queue: {0}",
            orderMessage.SentTime.ToString());

        // Display the value of the message's ArrivedTime property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.ArrivedTime property must be set to
        // true before the message is received.
        Console.WriteLine("Time arrived in destination queue: {0}",
            orderMessage.ArrivedTime.ToString());

        // Display the value of the message's Authenticated property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.Authenticated property must be set to
        // true before the message is received.
        Console.WriteLine("Authentication requested for this message: {0}",
            orderMessage.Authenticated.ToString());

        // Display the value of the message's SenderVersion property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.SenderVersion property must be set to
        // true before the message is received.
        Console.WriteLine("Message.SenderVersion: {0}",
            orderMessage.SenderVersion);

        // Message Queuing sets this property when the message is sent.
        // Display the value of the message's BodyType property.
        Console.WriteLine("Message.BodyType: {0}",
            orderMessage.BodyType.ToString());
    }

    // Sends an Order to a transactional queue.
    public void SendMessageToTransQueue()
    {
        // Create a new order and set values.
        Order sentOrder = new Order();
        sentOrder.orderId = 3;
        sentOrder.orderTime = DateTime.Now;

        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\orderTransQueue");

        // Create the new order.
        Message orderMessage = new Message(sentOrder);

        // Create a message queuing transaction.
        MessageQueueTransaction transaction = new MessageQueueTransaction();

        try
        {
            // Begin a transaction.
            transaction.Begin();

            // Send the order to the queue.
            queue.Send(orderMessage, transaction);

            // Commit the transaction.
            transaction.Commit();
        }
        catch (MessageQueueException e)
        {
            // Abort the transaction.
            transaction.Abort();

            // Propagate the exception.
            throw e;
        }
        finally
        {
            // Dispose of the transaction object.
            transaction.Dispose();
        }
    }

    // Receives a message containing an order.
    public void ReceiveMessageFromTransQueue()
    {
        Message orderMessage;

        // Connect to the a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\orderTransQueue");

        // Set the formatter to indicate the message body contains an order.
        queue.Formatter = new XmlMessageFormatter(new Type[]
            {typeof(Order)});

        // Set the queue's MessageReadPropertyFilter properties to ensure that
        // the message includes the properties in which we are interested.
        queue.MessageReadPropertyFilter.TransactionId = true;
        queue.MessageReadPropertyFilter.TransactionStatusQueue = true;
        queue.MessageReadPropertyFilter.IsFirstInTransaction = true;
        queue.MessageReadPropertyFilter.IsLastInTransaction = true;

        // Create a message queuing transaction.
        MessageQueueTransaction transaction = new MessageQueueTransaction();

        try
        {
            // Begin a transaction.
            transaction.Begin();

            // Receive the message. Time out after ten seconds in case the
            // message was not delivered.
            orderMessage = queue.Receive(TimeSpan.FromSeconds(10.0),
                transaction);

            // Commit the transaction.
            transaction.Commit();
        }
        catch(System.Exception e)
        {
            // Abort the transaction.
            transaction.Abort();

            // Propagate the exception.
            throw e;
        }
        finally
        {
            // Dispose of the transaction object.
            transaction.Dispose();
        }

        // Message Queuing sets this property when the message is sent.
        // Display the value of the message's TransactionStatusQueue property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.TransactionStatusQueue property must be
        // set to true before the message is received.
        Console.WriteLine("Message.TransactionStatusQueue.QueueName: {0}",
            orderMessage.TransactionStatusQueue.QueueName);

        // Display the value of the message's TransactionId property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.TransactionId property must be set to true
        // before the message is received.
        Console.WriteLine("Message.TransactionId: {0}",
            orderMessage.TransactionId);

        // Display the value of the message's IsFirstInTransaction property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.IsFirstInTransaction property must be set
        // to true before the message is received.
        Console.WriteLine("Message.IsFirstInTransaction: {0}",
            orderMessage.IsFirstInTransaction);

        // Display the value of the message's IsLastInTransaction property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.IsLastInTransaction property must be set
        // to true before the message is received.
        Console.WriteLine("Message.IsLastInTransaction: {0}",
            orderMessage.IsLastInTransaction);
    }
}


Visual C++
#using <System.Messaging.dll>
#using <System.dll>

using namespace System;
using namespace System::Messaging;

public ref class Order
{
private:
    int orderIdValue;
private:
    DateTime orderTimeValue;

public:
    property int OrderId
    {
        int get()
        {
            return orderIdValue;
        }
        void set(int value)
        {
            orderIdValue = value;
        }
    }
public:
    property DateTime OrderTime
    {
        DateTime get()
        {
            return orderTimeValue;
        }
        void set(DateTime value)
        {
            orderTimeValue = value;
        }
    }

public:
    Order()
    {
    }
};

// Creates a new queue.
void CreateQueue(String^ queuePath, bool transactional)
{
    if(!MessageQueue::Exists(queuePath))
    {
        MessageQueue^ queue = MessageQueue::Create(queuePath, transactional);
        queue->Close();
    }
    else
    {
        Console::WriteLine("{0} already exists.", queuePath);
    }
}

// Sends an Order to a queue.
void SendMessage()
{
    // Create a new order and set values.
    Order^ sentOrder = gcnew Order();
    sentOrder->OrderId = 3;
    sentOrder->OrderTime = DateTime::Now;

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\orderQueue");

    // Create the new order.
    Message^ orderMessage = gcnew Message(sentOrder);

    // Set the message's TimeToReachQueue property to five minutes.
    orderMessage->TimeToReachQueue = TimeSpan::FromMinutes(5.0);

    // Display the new value of the message's TimeToReachQueue property.
    Console::WriteLine("Message.TimeToReachQueue: {0}",
        orderMessage->TimeToReachQueue.ToString());

    // Set the message's TimeToBeReceived property to five minutes.
    orderMessage->TimeToBeReceived = TimeSpan::FromMinutes(5.0);

    // Display the new value of the message's TimeToBeReceived property.
    Console::WriteLine("Message.TimeToBeReceived: {0}",
        orderMessage->TimeToBeReceived.ToString());

    // Set the message's UseTracing property to true.
    orderMessage->UseTracing = true;

    // Display the new value of the message's UseTracing property.
    Console::WriteLine("Message.UseTracing: {0}", orderMessage->UseTracing);

    // Set the message's Label property.
    orderMessage->Label = "Order Message";

    // Display the new value of the message's Label property.
    Console::WriteLine("Message Label: {0}", orderMessage->Label);

    // Set the message's AttachSenderId property.
    orderMessage->AttachSenderId = true;

    // Display the new value of the message's AttachSenderId property.
    Console::WriteLine("Message.AttachSenderId: {0}",
        orderMessage->AttachSenderId);

    // Set the message's Recoverable property.
    orderMessage->Recoverable = true;

    // Display the new value of the message's Recoverable property.
    Console::WriteLine("Message.Recoverable: {0}",
        orderMessage->Recoverable);

    // Set the message's ResponseQueue property.
    // (You must have a connection to a queue.)
    orderMessage->ResponseQueue = queue;

    // Display the new value of the message's ResponseQueue property.
    Console::WriteLine("Message.ResponseQueue.QueueName: {0}",
        orderMessage->ResponseQueue->QueueName);

    // Set the message's TimeToReachQueue property to
    // Message.InfiniteTimeout.
    orderMessage->TimeToReachQueue = Message::InfiniteTimeout;

    // Display the new value of the message's TimeToReachQueue property.
    Console::WriteLine("Message.TimeToReachQueue: {0}",
        orderMessage->TimeToReachQueue.ToString());

    // Set the message's UseDeadLetterQueue property.
    orderMessage->UseDeadLetterQueue = true;

    // Display the new value of the message's UseDeadLetterQueue property.
    Console::WriteLine("Message.UseDeadLetterQueue: {0}",
        orderMessage->UseDeadLetterQueue);

    // Set the message's UseJournalQueue property.
    orderMessage->UseJournalQueue = true;

    // Display the new value of the message's UseJournalQueue property.
    Console::WriteLine("Message.UseJournalQueue: {0}",
        orderMessage->UseJournalQueue);

    // Set the message's UseEncryption property.
    orderMessage->UseEncryption = true;

    // Display the new value of the message's UseEncryption property.
    Console::WriteLine("Message.UseEncryption: {0}",
        orderMessage->UseEncryption);

    // Send the order to the queue.
    queue->Send(orderMessage);

    queue->Close();
}

// Receives a message containing an order.
void ReceiveMessage()
{
    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\orderQueue");

    // Set the formatter to indicate the message body contains an order.
    queue->Formatter = gcnew XmlMessageFormatter(gcnew array<Type^>
    {Order::typeid});

    // Set the queue's MessageReadPropertyFilter properties to ensure that
    // the message includes the properties in which we are interested.
    queue->MessageReadPropertyFilter->ArrivedTime = true;
    queue->MessageReadPropertyFilter->DestinationQueue = true;
    queue->MessageReadPropertyFilter->SenderVersion = true;
    queue->MessageReadPropertyFilter->SentTime = true;
    queue->MessageReadPropertyFilter->SourceMachine = true;
    queue->MessageReadPropertyFilter->Authenticated = true;

    // Receive the message. Time out after ten seconds in case the message
    // was not delivered.
    Message^ orderMessage = queue->Receive(TimeSpan::FromSeconds(10.0));

    // Format the message.
    Order^ sentOrder = (Order^)orderMessage->Body;

    // Display message information.
    Console::WriteLine("Order ID: {0}", sentOrder->OrderId.ToString());
    Console::WriteLine("Sent: {0}", sentOrder->OrderTime.ToString());

    // Display the value of the message's MessageType property.
    Console::WriteLine("Message Type: {0}", orderMessage->MessageType);

    // Display the value of the message's SourceMachine property.
    // To view this property value, the queue's
    // MessageReadPropertyFilter.SourceMachine property must be set to
    // true before the message is received.
    Console::WriteLine("Source Machine: {0}", orderMessage->SourceMachine);

    // Display the value of the message's DestinationQueue property.
    // To view this property value, the queue's
    // MessageReadPropertyFilter.DestinationQueue property must be set to
    // true before the message is received.
    Console::WriteLine("Message.DestinationQueue.QueueName: {0}",
        orderMessage->DestinationQueue->QueueName);

    // Display the value of the message's SentTime property.
    // To view this property value, the queue's
    // MessageReadPropertyFilter.SentTime property must be set to
    // true before the message is received.
    Console::WriteLine("Time sent from source queue: {0}",
        orderMessage->SentTime.ToString());

    // Display the value of the message's ArrivedTime property.
    // To view this property value, the queue's
    // MessageReadPropertyFilter.ArrivedTime property must be set to
    // true before the message is received.
    Console::WriteLine("Time arrived in destination queue: {0}",
        orderMessage->ArrivedTime.ToString());

    // Display the value of the message's Authenticated property.
    // To view this property value, the queue's
    // MessageReadPropertyFilter.Authenticated property must be set to
    // true before the message is received.
    Console::WriteLine("Authentication requested for this message: {0}",
        orderMessage->Authenticated.ToString());

    // Display the value of the message's SenderVersion property.
    // To view this property value, the queue's
    // MessageReadPropertyFilter.SenderVersion property must be set to
    // true before the message is received.
    Console::WriteLine("Message.SenderVersion: {0}",
        orderMessage->SenderVersion);

    // Message Queuing sets this property when the message is sent.
    // Display the value of the message's BodyType property.
    Console::WriteLine("Message.BodyType: {0}",
        orderMessage->BodyType.ToString());

    queue->Close();
}

// Sends an Order to a transactional queue.
void SendMessageToTransQueue()
{
    // Create a new order and set values.
    Order^ sentOrder = gcnew Order();
    sentOrder->OrderId = 3;
    sentOrder->OrderTime = DateTime::Now;

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\orderTransQueue");

    // Create the new order.
    Message^ orderMessage = gcnew Message(sentOrder);

    // Create a message queuing transaction.
    MessageQueueTransaction^ transaction = gcnew MessageQueueTransaction();

    try
    {
        // Begin a transaction.
        transaction->Begin();

        // Send the order to the queue.
        queue->Send(orderMessage, transaction);

        // Commit the transaction.
        transaction->Commit();
    }
    catch (MessageQueueException^ ex)
    {
        // Abort the transaction.
        transaction->Abort();

        // Propagate the exception.
        throw;
    }
    finally
    {
        // Dispose of the transaction object.
        delete transaction;

        queue->Close();
    }
}

// Receives a message containing an order.
void ReceiveMessageFromTransQueue()
{
    Message^ orderMessage;

    // Connect to the a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\orderTransQueue");

    // Set the formatter to indicate the message body contains an order.
    queue->Formatter = gcnew XmlMessageFormatter(gcnew array<Type^>
    {Order::typeid});

    // Set the queue's MessageReadPropertyFilter properties to ensure that
    // the message includes the properties in which we are interested.
    queue->MessageReadPropertyFilter->TransactionId = true;
    queue->MessageReadPropertyFilter->TransactionStatusQueue = true;
    queue->MessageReadPropertyFilter->IsFirstInTransaction = true;
    queue->MessageReadPropertyFilter->IsLastInTransaction = true;

    // Create a message queuing transaction.
    MessageQueueTransaction^ transaction = gcnew MessageQueueTransaction();

    try
    {
        // Begin a transaction.
        transaction->Begin();

        // Receive the message. Time out after ten seconds in case the
        // message was not delivered.
        orderMessage = queue->Receive(TimeSpan::FromSeconds(10.0),
            transaction);

        // Commit the transaction.
        transaction->Commit();
    }
    catch (MessageQueueException^ ex)
    {
        // Abort the transaction.
        transaction->Abort();

        // Propagate the exception.
        throw;
    }
    finally
    {
        // Dispose of the transaction object.
        delete transaction;

        queue->Close();
    }

    // Message Queuing sets this property when the message is sent.
    // Display the value of the message's TransactionStatusQueue property.
    // To view this property value, the queue's
    // MessageReadPropertyFilter.TransactionStatusQueue property must be
    // set to true before the message is received.
    Console::WriteLine("Message.TransactionStatusQueue.QueueName: {0}",
        orderMessage->TransactionStatusQueue->QueueName);

    // Display the value of the message's TransactionId property.
    // To view this property value, the queue's
    // MessageReadPropertyFilter.TransactionId property must be set to true
    // before the message is received.
    Console::WriteLine("Message.TransactionId: {0}",
        orderMessage->TransactionId);

    // Display the value of the message's IsFirstInTransaction property.
    // To view this property value, the queue's
    // MessageReadPropertyFilter.IsFirstInTransaction property must be set
    // to true before the message is received.
    Console::WriteLine("Message.IsFirstInTransaction: {0}",
        orderMessage->IsFirstInTransaction);

    // Display the value of the message's IsLastInTransaction property.
    // To view this property value, the queue's
    // MessageReadPropertyFilter.IsLastInTransaction property must be set
    // to true before the message is received.
    Console::WriteLine("Message.IsLastInTransaction: {0}",
        orderMessage->IsLastInTransaction);

}

int main()
{
    try
    {
        // Create a non-transactional queue on the local computer.
        // Note that the queue might not be immediately accessible, and
        // therefore this example might throw an exception of type
        // System.Messaging.MessageQueueException when trying to send a
        // message to the newly created queue.
        CreateQueue(".\\orderQueue", false);

        // Send a message to a queue.
        SendMessage();

        // Receive a message from a queue.
        ReceiveMessage();

        // Create a transactional queue on the local computer.
        CreateQueue(".\\orderTransQueue", true);

        // Send a message to a transactional queue.
        SendMessageToTransQueue();

        // Receive a message from a transactional queue.
        ReceiveMessageFromTransQueue();
    }

    catch (InvalidOperationException^)
    {
        Console::WriteLine("Please install Message Queuing.");
    }

    catch (MessageQueueException^ ex)
    {
        // Write the exception information to the console.
        Console::WriteLine(ex);
    }
}


J#
import System.*;
import System.Messaging.*;

public class Order
{
    private int _orderId;
    private DateTime _orderTime;

    /** @property 
     */
    public int get_orderId()
    {
        return _orderId;
    } //get_orderId

    /** @property 
     */
    public void set_orderId(int value)
    {
        _orderId = value;
    } //set_orderId

    /** @property 
     */
    public DateTime get_orderTime()
    {
        return _orderTime;
    } //get_orderTime

    /** @property 
     */
    public void set_orderTime(DateTime value)
    {
        _orderTime = value;
    } //set_orderTime

    public Order()
    {
    } //Order
} //Order

public class OrderProcessor
{
    public static void main(String[] args)
    {
        // Create a new instance of the class.
        OrderProcessor processor = new OrderProcessor();

        try {
            // Create a non-transactional queue on the local computer.
            // Note that the queue might not be immediately accessible, and
            // therefore this example might throw an exception of type
            // System.Messaging.MessageQueueException when trying to send a
            // message to the newly created queue.
            CreateQueue(".\\orderQueue", false);
            // Send a message to a queue.
            processor.SendMessage();
            // Receive a message from a queue.
            processor.ReceiveMessage();
            // Create a transactional queue on the local computer.
            CreateQueue(".\\orderTransQueue", true);
            // Send a message to a transactional queue.
            processor.SendMessageToTransQueue();
            // Receive a message from a transactional queue.
            processor.ReceiveMessageFromTransQueue();
        }
        catch (System.Exception e) {
            // Write the exception information to the console.
            Console.WriteLine(e);
        }
    } //main

    // Creates a new queue.
    public static void CreateQueue(String queuePath, boolean transactional)
    {
        if (!(MessageQueue.Exists(queuePath))) {
            MessageQueue.Create(queuePath, transactional);
        }
        else {
            Console.WriteLine((queuePath + " already exists."));
        }
    } //CreateQueue

    // Sends an Order to a queue.
    public void SendMessage()
    {
        // Create a new order and set values.
        Order sentOrder = new Order();
        sentOrder.set_orderId(3);
        sentOrder.set_orderTime(DateTime.get_Now());
        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\orderQueue");
        // Create the new order.
        Message orderMessage = new Message(sentOrder);
        // Set the message's TimeToReachQueue property to five minutes.
        orderMessage.set_TimeToReachQueue(TimeSpan.FromMinutes(5.0));
        // Display the new value of the message's TimeToReachQueue property.
        Console.WriteLine("Message.TimeToReachQueue: {0}", 
            orderMessage.get_TimeToReachQueue().ToString());
        // Set the message's TimeToBeReceived property to five minutes.
        orderMessage.set_TimeToBeReceived(TimeSpan.FromMinutes(5.0));
        // Display the new value of the message's TimeToBeReceived property.
        Console.WriteLine("Message.TimeToBeReceived: {0}", 
            orderMessage.get_TimeToBeReceived().ToString());
        // Set the message's UseTracing property to true.
        orderMessage.set_UseTracing(true);
        // Display the new value of the message's UseTracing property.
        Console.WriteLine("Message.UseTracing: {0}", 
            System.Convert.ToString(orderMessage.get_UseTracing()));
        // Set the message's Label property.
        orderMessage.set_Label("Order Message");
        // Display the new value of the message's Label property.
        Console.WriteLine("Message Label: {0}", orderMessage.get_Label());
        // Set the message's AttachSenderId property.
        orderMessage.set_AttachSenderId(true);
        // Display the new value of the message's AttachSenderId property.
        Console.WriteLine("Message.AttachSenderId: {0}", 
            System.Convert.ToString(orderMessage.get_AttachSenderId()));
        // Set the message's Recoverable property.
        orderMessage.set_Recoverable(true);
        // Display the new value of the message's Recoverable property.
        Console.WriteLine("Message.Recoverable: {0}", 
            System.Convert.ToString(orderMessage.get_Recoverable()));
        // Set the message's ResponseQueue property.
        // (You must have a connection to a queue.)
        orderMessage.set_ResponseQueue(queue);
        // Display the new value of the message's ResponseQueue property.
        Console.WriteLine("Message.ResponseQueue.QueueName: {0}", 
            orderMessage.get_ResponseQueue().get_QueueName().ToString());
        // Set the message's TimeToReachQueue property to Message.InfiniteTimeout.
        orderMessage.set_TimeToReachQueue(Message.InfiniteTimeout);
        // Display the new value of the message's TimeToReachQueue property.
        Console.WriteLine("Message.TimeToReachQueue: {0}", 
            System.Convert.ToString(orderMessage.get_TimeToReachQueue()));
        // Set the message's UseDeadLetterQueue property.
        orderMessage.set_UseDeadLetterQueue(true);
        // Display the new value of the message's UseDeadLetterQueue property.
        Console.WriteLine("Message.UseDeadLetterQueue: {0}", 
            System.Convert.ToString(orderMessage.get_UseDeadLetterQueue()));
        // Set the message's UseJournalQueue property.
        orderMessage.set_UseJournalQueue(true);
        // Display the new value of the message's UseJournalQueue property.
        Console.WriteLine("Message.UseJournalQueue: {0}", 
            System.Convert.ToString(orderMessage.get_UseJournalQueue()));
        // Set the message's UseEncryption property.
        orderMessage.set_UseEncryption(true);
        // Display the new value of the message's UseEncryption property.
        Console.WriteLine("Message.UseEncryption: {0}", 
            System.Convert.ToString(orderMessage.get_UseEncryption()));
        // Send the order to the queue.
        queue.Send(orderMessage);
    } //SendMessage

    // Receives a message containing an order.
    public void ReceiveMessage()
    {
        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\orderQueue");
        // Set the formatter to indicate the message body contains an order.
        queue.set_Formatter(new XmlMessageFormatter(new Type[] { 
            Order.class.ToType() }));
        // Set the queue's MessageReadPropertyFilter properties to ensure that
        // the message includes the properties in which we are interested.
        queue.get_MessageReadPropertyFilter().set_ArrivedTime(true);
        queue.get_MessageReadPropertyFilter().set_DestinationQueue(true);
        queue.get_MessageReadPropertyFilter().set_SenderVersion(true);
        queue.get_MessageReadPropertyFilter().set_SentTime(true);
        queue.get_MessageReadPropertyFilter().set_SourceMachine(true);
        queue.get_MessageReadPropertyFilter().set_Authenticated(true);
        // Receive the message. Time out after ten seconds in case the message
        // was not delivered.
        Message orderMessage = queue.Receive(TimeSpan.FromSeconds(10.0));
        // Format the message.
        Order sentOrder = ( Order)orderMessage.get_Body();
        // Display message information.
        Console.WriteLine("Order ID: {0}", 
            System.Convert.ToString(sentOrder.get_orderId()));
        Console.WriteLine("Sent: {0}", 
            System.Convert.ToString(sentOrder.get_orderTime()));
        // Display the value of the message's MessageType property.
        Console.WriteLine("Message Type: {0}", 
            System.Convert.ToString(orderMessage.get_MessageType()));
        // Display the value of the message's SourceMachine property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.SourceMachine property must be set to
        // true before the message is received.
        Console.WriteLine("Source Machine: {0}", 
            System.Convert.ToString(orderMessage.get_SourceMachine()));
        // Display the value of the message's DestinationQueue property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.DestinationQueue property must be set to
        // true before the message is received.
        Console.WriteLine("Message.DestinationQueue.QueueName: {0}", 
            System.Convert.ToString(orderMessage.get_DestinationQueue().
            get_QueueName()));
        // Display the value of the message's SentTime property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.SentTime property must be set to
        // true before the message is received.
        Console.WriteLine("Time sent from source queue: {0}", 
            System.Convert.ToString(orderMessage.get_SentTime()));
        // Display the value of the message's ArrivedTime property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.ArrivedTime property must be set to
        // true before the message is received.
        Console.WriteLine("Time arrived in destination queue: {0}", 
            System.Convert.ToString(orderMessage.get_ArrivedTime()));
        // Display the value of the message's Authenticated property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.Authenticated property must be set to
        // true before the message is received.
        Console.WriteLine("Authentication requested for this message: {0}", 
            System.Convert.ToString(orderMessage.get_Authenticated()));
        // Display the value of the message's SenderVersion property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.SenderVersion property must be set to
        // true before the message is received.
        Console.WriteLine("Message.SenderVersion: {0}", 
            System.Convert.ToString(orderMessage.get_SenderVersion()));
        // Message Queuing sets this property when the message is sent.
        // Display the value of the message's BodyType property.
        Console.WriteLine("Message.BodyType: {0}", 
            System.Convert.ToString(orderMessage.get_BodyType()));
    } //ReceiveMessage

    // Sends an Order to a transactional queue.
    public void SendMessageToTransQueue() throws MessageQueueException
    {
        // Create a new order and set values.
        Order sentOrder = new Order();
        sentOrder.set_orderId(3);
        sentOrder.set_orderTime(DateTime.get_Now());
        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\orderTransQueue");
        // Create the new order.
        Message orderMessage = new Message(sentOrder);
        // Create a message queuing transaction.
        MessageQueueTransaction transaction = new MessageQueueTransaction();

        try {
            // Begin a transaction.
            transaction.Begin();
            // Send the order to the queue.
            queue.Send(orderMessage, transaction);
            // Commit the transaction.
            transaction.Commit();
        }
        catch (MessageQueueException e) {
            // Abort the transaction.
            transaction.Abort();
            // Propagate the exception.
            throw e;
        }
        finally {
            // Dispose of the transaction object.
            transaction.Dispose();
        }
    } //SendMessageToTransQueue

    // Receives a message containing an order.
    public void ReceiveMessageFromTransQueue() throws System.Exception
    {
        Message orderMessage;
        // Connect to the a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\orderTransQueue");
        // Set the formatter to indicate the message body contains an order.
        queue.set_Formatter(new XmlMessageFormatter(new Type[] { 
            Order.class.ToType() }));
        // Set the queue's MessageReadPropertyFilter properties to ensure that
        // the message includes the properties in which we are interested.
        queue.get_MessageReadPropertyFilter().set_TransactionId(true);
        queue.get_MessageReadPropertyFilter().set_TransactionStatusQueue(true);
        queue.get_MessageReadPropertyFilter().set_IsFirstInTransaction(true);
        queue.get_MessageReadPropertyFilter().set_IsLastInTransaction(true);
        // Create a message queuing transaction.
        MessageQueueTransaction transaction = new MessageQueueTransaction();

        try {
            // Begin a transaction.
            transaction.Begin();
            // Receive the message. Time out after ten seconds in case the
            // message was not delivered.
            orderMessage = queue.Receive(TimeSpan.FromSeconds(10.0), 
                transaction);
            // Commit the transaction.
            transaction.Commit();
        }
        catch (System.Exception e) {
            // Abort the transaction.
            transaction.Abort();
            // Propagate the exception.
            throw e;
        }
        finally {
            // Dispose of the transaction object.
            transaction.Dispose();
        }
        // Message Queuing sets this property when the message is sent.
        // Display the value of the message's TransactionStatusQueue property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.TransactionStatusQueue property must be
        // set to true before the message is received.
        Console.WriteLine("Message.TransactionStatusQueue.QueueName: {0}", 
            orderMessage.get_TransactionStatusQueue().get_QueueName());
        // Display the value of the message's TransactionId property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.TransactionId property must be set to true
        // before the message is received.
        Console.WriteLine("Message.TransactionId: {0}", 
            System.Convert.ToString(orderMessage.get_TransactionId()));
        // Display the value of the message's IsFirstInTransaction property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.IsFirstInTransaction property must be set
        // to true before the message is received.
        Console.WriteLine("Message.IsFirstInTransaction: {0}", 
            System.Convert.ToString(orderMessage.get_IsFirstInTransaction()));
        // Display the value of the message's IsLastInTransaction property.
        // To view this property value, the queue's
        // MessageReadPropertyFilter.IsLastInTransaction property must be set
        // to true before the message is received.
        Console.WriteLine("Message.IsLastInTransaction: {0}", 
            System.Convert.ToString(orderMessage.get_IsLastInTransaction()));
    } //ReceiveMessageFromTransQueue
} //OrderProcessor 

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;

namespace ConsoleApplication1
{
    class client
    {


        static void RunClient()
        {
            //Step1: create a binding with just HTTP
            CustomBinding binding = new CustomBinding();
            binding.Elements.Add(new HttpTransportBindingElement());
            //Step2: use the binding to build the channel factory
            IChannelFactory<IRequestChannel> factory =
            binding.BuildChannelFactory<IRequestChannel>(
                             new BindingParameterCollection());
            //open the channel factory
            factory.Open();
            //Step3: use the channel factory to create a channel
            IRequestChannel channel = factory.CreateChannel(
               new EndpointAddress("http://localhost:8080/channelapp"));
            channel.Open();
            //Step4: create a message
            Message requestmessage = Message.CreateMessage(
                MessageVersion.Soap12WSAddressing10,
                "http://contoso.com/someaction",
                 "This is the body data");
            //send message
            Message replymessage = channel.Request(requestmessage);
            Console.WriteLine("Reply message received");
            Console.WriteLine("Reply action: {0}",
                                  replymessage.Headers.Action);
            string data = replymessage.GetBody<string>();
            Console.WriteLine("Reply content: {0}", data);
            //Step5: don't forget to close the message
            requestmessage.Close();
            replymessage.Close();
            //don't forget to close the channel
            channel.Close();
            //don't forget to close the factory
            factory.Close();
        }
        public static void Main()
        {
            Console.WriteLine("Press [ENTER] when service is ready");
            Console.ReadLine();
            RunClient();
            Console.WriteLine("Press [ENTER] to exit");
            Console.ReadLine();
        }
    }
}

System..::.Object
  System.ServiceModel.Channels..::.Message
この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。

Windows Vista, Windows XP SP2, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。

.NET Framework

サポート対象 : 3.5、3.0

.NET Compact Framework

サポート対象 : 3.5
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2009 Microsoft Corporation. All rights reserved. 使用条件  |  商標  |  プライバシー
Page view tracker