XmlMessageFormatter クラス

定義

XSD スキーマ定義の XML 書式を使用して、メッセージ本文との間でオブジェクトをシリアル化および逆シリアル化します。

public ref class XmlMessageFormatter : ICloneable, System::Messaging::IMessageFormatter
public class XmlMessageFormatter : ICloneable, System.Messaging.IMessageFormatter
type XmlMessageFormatter = class
    interface IMessageFormatter
    interface ICloneable
Public Class XmlMessageFormatter
Implements ICloneable, IMessageFormatter
継承
XmlMessageFormatter
実装

次のコード例には、サーバー コンポーネント、注文クラス、クライアント コードの 3 つのコードが含まれています。 order クラスは、サーバーが受信メッセージ内で認識するスキーマを生成するために、XSD.exe ユーティリティによって使用できます。 スキーマは、クラスの "図形" を記述する XML 形式のファイルです。 その後、このスキーマをクライアント側で使用して、サーバー クラスと同じスキーマを共有するクライアント固有の注文クラスを生成できます。

次のコード例は、メッセージ キューを介して注文を受信するサーバー コンポーネントを表します。 メッセージの本文は、スキーマが以下の Order.cs クラスと一致する order オブジェクトである必要があります。 サーバー プロセスまたはアプリケーションは順序を逆シリアル化します。

#using <System.dll>
#using <System.Messaging.dll>

using namespace System;
using namespace System::Messaging;

// placeholder; see complete definition elsewhere in this section
public ref class Order
{
public:
   void ShipItems(){}

};


// Creates the queue if it does not already exist.
void EnsureQueueExists( String^ path )
{
   if (  !MessageQueue::Exists( path ) )
   {
      MessageQueue::Create( path );
   }
}

int main()
{
   Console::WriteLine( "Processing Orders" );
   String^ queuePath = ".\\orders";
   EnsureQueueExists( queuePath );
   MessageQueue^ queue = gcnew MessageQueue( queuePath );
   array<String^>^temp0 = {"Order"};
   (dynamic_cast<XmlMessageFormatter^>(queue->Formatter))->TargetTypeNames = temp0;
   while ( true )
   {
      Order^ newOrder = dynamic_cast<Order^>(queue->Receive()->Body);
      newOrder->ShipItems();
   }
}
using System;
using System.Messaging;

 public class Server{

     public static void Main(){

         Console.WriteLine("Processing Orders");

         string queuePath = ".\\orders";
         EnsureQueueExists(queuePath);
         MessageQueue queue = new MessageQueue(queuePath);
         ((XmlMessageFormatter)queue.Formatter).TargetTypeNames = new string[]{"Order"};

         while(true){
             Order newOrder = (Order)queue.Receive().Body;
             newOrder.ShipItems();
         }
     }

     // Creates the queue if it does not already exist.
     public static void EnsureQueueExists(string path){
         if(!MessageQueue.Exists(path)){
             MessageQueue.Create(path);
         }
     }
 }
Imports System.Messaging



Public Class Server
    
    
    Public Shared Sub Main()
        
        Console.WriteLine("Processing Orders")
        
        Dim queuePath As String = ".\orders"
        EnsureQueueExists(queuePath)
        Dim queue As New MessageQueue(queuePath)
        CType(queue.Formatter, XmlMessageFormatter).TargetTypeNames = New String() {"Order"}
        
        While True
            Dim newOrder As Order = CType(queue.Receive().Body, Order)
            newOrder.ShipItems()
        End While
    End Sub
    
    
    ' Creates the queue if it does not already exist.
    Public Shared Sub EnsureQueueExists(path As String)
        If Not MessageQueue.Exists(path) Then
            MessageQueue.Create(path)
        End If
    End Sub
End Class

次のコード例は、サーバー上のアプリケーションが受信および逆シリアル化する注文オブジェクトのスキーマを提供する order クラスを表します。

using namespace System;
public ref class Order
{
public:
   int itemId;
   int quantity;
   String^ address;
   void ShipItems()
   {
      Console::WriteLine( "Order Placed:" );
      Console::WriteLine( "\tItem ID  : {0}", itemId );
      Console::WriteLine( "\tQuantity : {0}", quantity );
      Console::WriteLine( "\tShip To  : {0}", address );
      
      // Add order to the database.
      /* Insert code here. */
   }

};
using System;

 public class Order{

     public int itemId;
     public int quantity;
     public string address;

     public void ShipItems(){

         Console.WriteLine("Order Placed:");
         Console.WriteLine("\tItem ID  : {0}",itemId);
         Console.WriteLine("\tQuantity : {0}",quantity);
         Console.WriteLine("\tShip To  : {0}",address);

         // Add order to the database.
         /* Insert code here. */
     }
 }
Public Class Order
    
    Public itemId As Integer
    Public quantity As Integer
    Public address As String
    
    
    Public Sub ShipItems()
        
        Console.WriteLine("Order Placed:")
        Console.WriteLine(ControlChars.Tab & "Item ID  : {0}", itemId)
        Console.WriteLine(ControlChars.Tab & "Quantity : {0}", quantity)
        Console.WriteLine(ControlChars.Tab & "Ship To  : {0}", address)

        ' Add order to the database.
        ' Insert code here.
 
    End Sub
End Class

サーバー上のアプリケーションと対話するすべてのクライアント アプリケーションは、ローカルに定義された order クラスの情報をメッセージ本文にシリアル化して、サーバーにメッセージを送信する必要があります。 ローカルで定義された順序クラスには、サーバー上のアプリケーションがメッセージ本文の逆シリアル化を試みるサーバー定義の順序クラスと同じスキーマが必要です。 XSD.exe ユーティリティを使用すると、サーバー上のアプリケーションのマネージャーは、サーバーに送信されるメッセージをシリアル化するためにクライアントが使用する必要があるスキーマを作成して配布できます。

クライアント アプリケーションのマネージャーが注文クラスのスキーマを受け取ると、XSD.exe ユーティリティが再び使用され、スキーマからクライアント固有の注文クラスが生成されます。 サーバーの order クラスではなく、次のクライアント コード例で使用されるこのクラスです (XSD.exe ユーティリティを使用すると、スキーマ生成クラスの名前は元のクラスと同じになります)。 この新しい order クラスは、注文をメッセージ本文にシリアル化するために使用されます。

次のコード例は、注文をシリアル化し、注文に関連付けられている情報をキューに送信するために使用されるクライアント側の処理です。 このコードは、Item、Quantity、Address 情報を、XSD.exe ユーティリティによって Order.cs クラス用に生成されたスキーマの要素に関連付けます。 注文は、ローカル コンピューターの Orders キューに送信されます。

#using <System.dll>
#using <System.Messaging.dll>

using namespace System;
using namespace System::Messaging;

// placeholder; see complete definition elsewhere in this section
public ref class Order
{
public:
   int itemId;
   int quantity;
   String^ address;
   void ShipItems(){}

};


// Creates the queue if it does not already exist.
void EnsureQueueExists( String^ path )
{
   if (  !MessageQueue::Exists( path ) )
   {
      MessageQueue::Create( path );
   }
}

int main()
{
   String^ queuePath = ".\\orders";
   EnsureQueueExists( queuePath );
   MessageQueue^ queue = gcnew MessageQueue( queuePath );
   Order^ orderRequest = gcnew Order;
   orderRequest->itemId = 1025;
   orderRequest->quantity = 5;
   orderRequest->address = "One Microsoft Way";
   queue->Send( orderRequest );
   
   // This line uses a new method you define on the Order class:
   // orderRequest.PrintReceipt();
}
using System;
using System.Messaging;

 class Client{

     public static void Main(){

         string queuePath = ".\\orders";
         EnsureQueueExists(queuePath);
         MessageQueue queue = new MessageQueue(queuePath);

         Order orderRequest = new Order();
         orderRequest.itemId = 1025;
         orderRequest.quantity = 5;
         orderRequest.address = "One Microsoft Way";

         queue.Send(orderRequest);
         // This line uses a new method you define on the Order class:
         // orderRequest.PrintReceipt();
     }

     // Creates the queue if it does not already exist.
     public static void EnsureQueueExists(string path){
         if(!MessageQueue.Exists(path)){
             MessageQueue.Create(path);
         }
     }
 }
Imports System.Messaging

Class Client
    
    
    Public Shared Sub Main()
        
        Dim queuePath As String = ".\orders"
        EnsureQueueExists(queuePath)
        Dim queue As New MessageQueue(queuePath)
        
        Dim orderRequest As New Order()
        orderRequest.itemId = 1025
        orderRequest.quantity = 5
        orderRequest.address = "One Microsoft Way"
        
        queue.Send(orderRequest)
        ' This line uses a new method you define on the Order class:
        ' orderRequest.PrintReceipt()

    End Sub
    
    ' Creates the queue if it does not already exist.
    Public Shared Sub EnsureQueueExists(path As String)
        If Not MessageQueue.Exists(path) Then
            MessageQueue.Create(path)
        End If
    End Sub
End Class

サーバー上の order クラスからスキーマが生成されたら、 クラスを変更できます。 スキーマが変更されない限り、スキーマを再配布する必要はありません。 スキーマを分散し、クライアント側の順序クラスを生成した後、スキーマ自体が変更されていない限り、そのクライアント クラスはサーバーの注文クラスとは無関係に変更することもできます。 2 つのクラスは疎結合になっています。

注釈

XmlMessageFormatterは、 のインスタンスMessageQueueがキューに書き込まれたメッセージをシリアル化するために使用する既定のフォーマッタです。 のインスタンスを作成すると、 のMessageQueueXmlMessageFormatterインスタンスが自動的に作成され、 にMessageQueue関連付けられます。 コードで別のフォーマッタを作成し、 の プロパティに Formatter 割り当てることで、別のフォーマッタを MessageQueue指定できます。

キューの既定XmlMessageFormatterのインスタンスを使用してキューに書き込むことができますが、フォーマッタで または TargetTypeNames プロパティを設定TargetTypesするまで、キューからの読み取りには使用できません。 これらの値の一方または両方を既定のフォーマッタ インスタンスに設定するか、フォーマッタの新しいインスタンスを作成し、値を適切な XmlMessageFormatter コンストラクターに引数として渡すことで自動的に設定できます。

ではなく TargetTypeNamesTargetTypes指定する場合、型の存在は読み取り時間ではなくコンパイル時にチェックされるため、エラーが発生する可能性が低くなります。 TargetTypeNames では、すべてのエントリを完全修飾し、アセンブリ名を指定する必要があります。 さらに、複数の同時実行バージョンを使用する場合は、ターゲットの型名にもバージョン番号を追加する必要があります。

プロパティと TargetTypes プロパティはTargetTypeNames、メッセージの逆シリアル化時に照合を試みるスキーマをフォーマッタに通知します。 これにより、フォーマッタはメッセージ本文を解釈できます。

メッセージ本文でシリアル化されるインスタンスは、型配列で表されるスキーマのいずれかに準拠している必要があります。 メソッドを使用して Receive メッセージを読み取ると、 メソッドは、識別されたスキーマに対応する型のオブジェクトを作成し、メッセージ本文を読み取ります。

キューからの読み取り時に設定する必要があるプロパティは 2 つのうち 1 つだけですが、両方を設定できます。 型のセットは、2 つのプロパティの組み合わせセットです。 使用するプロパティの決定は、アプリケーションに固有です。 メッセージ本文に、いずれかのプロパティの配列内のどの型にも一致しないスキーマを持つ型が含まれている場合、メッセージの読み取り時に例外がスローされます。

XmlMessageFormatterは、疎結合 XML ベースのメッセージングの重要なコンポーネントです。 XSD.exe ユーティリティは、XML 形式を使用して XML スキーマを生成します。たとえば、 ユーティリティを使用してアプリケーションで使用されるクラスをシリアル化する場合などです。 このクラスには、パラメーターなしのコンストラクターが含まれている必要があります。

この形式は、クラス データを記述するために配布するスキーマに基づいてユーティリティによってクラスが生成されるときに、逆プロセスで再び使用されます。 ユーティリティと生成される XML スキーマを使用すると、クラスの実装が変更された後にクラスを再コンパイルするたびにファイルをredistributing.dll回避できます。 クライアントまたはサーバーでスキーマが変更されない限り、どちらかの側の他の変更は他方には影響しません。

コンストラクター

XmlMessageFormatter()

対象の型を設定せずに、XmlMessageFormatter クラスの新しいインスタンスを初期化します。

XmlMessageFormatter(String[])

XmlMessageFormatter クラスの新しいインスタンスを初期化し、完全限定型の文字列値の配列として渡された対象の型を設定します。

XmlMessageFormatter(Type[])

XmlMessageFormatter クラスの新しいインスタンスを初期化し、オブジェクト型の配列として渡された対象の型を設定します。

プロパティ

TargetTypeNames

提供されたメッセージからフォーマッタで逆シリアル化できる型のセットを指定します。

TargetTypes

提供されたメッセージからフォーマッタで逆シリアル化できる型のセットを指定します。

メソッド

CanRead(Message)

フォーマッタがメッセージを逆シリアル化できるかどうかを判断します。

Clone()

現在の XmlMessageFormatter インスタンスと同じ読み取り/書き込みプロパティ (対象の型のセット) の XmlMessageFormatter クラスのインスタンスを作成します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Read(Message)

指定されたメッセージからコンテンツを読み取り、逆シリアル化されたメッセージを含むオブジェクトを作成します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)
Write(Message, Object)

メッセージ本文にオブジェクトをシリアル化します。

適用対象

こちらもご覧ください