次の方法で共有


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

サーバー上のアプリケーションとやり取りするすべてのクライアント アプリケーションは、ローカルで定義された注文クラスの情報をメッセージ本文にシリアル化することによって、サーバーにメッセージを送信する必要があります。 ローカル定義の順序クラスには、サーバー上のアプリケーションがメッセージ本文の逆シリアル化を試行するサーバー定義の順序クラスと同じスキーマが必要です。 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のインスタンスが使用する既定のフォーマッタです。 MessageQueueのインスタンスを作成すると、XmlMessageFormatterのインスタンスが自動的に作成され、MessageQueueに関連付けられます。 コードで別のフォーマッタを作成し、MessageQueueFormatter プロパティに割り当てることで、別のフォーマッタを指定できます。

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

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

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

メッセージ本文でシリアル化されたインスタンスは、型配列で表されるスキーマのいずれかに準拠している必要があります。 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)

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

適用対象

こちらもご覧ください