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
구현

예제

다음 코드 예제에서는 세 가지 코드를 포함 합니다: 서버 구성 요소, order 클래스 및 클라이언트 코드입니다. Order 클래스 서버가 들어오는 메시지에서 인식 하는 스키마를 생성 하는 XSD.exe 유틸리티에서 사용할 수 있습니다. 스키마가 클래스의 "모양"를 설명 하는 XML 형식된 파일입니다. 이 스키마 서버 클래스와 동일한 스키마를 공유 하는 클라이언트 관련 order 클래스를 생성 하는 클라이언트 쪽에서 사용할 수 있습니다.

다음 코드 예제에서는 메시지 큐를 통해 주문을 받는 서버 구성 요소를 나타냅니다. 메시지의 본문에 해당 스키마에는 아래 Order.cs 클래스와 일치 순서 개체 여야 합니다. 서버 프로세스 또는 애플리케이션에는 순서를 역직렬화합니다.

#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 개체에 대 한 스키마를 제공 하는 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 클래스에는 애플리케이션 서버에서 메시지 본문을 역직렬화 할 시도가 order 서버에서 정의 된 클래스와 동일한 스키마가 있어야 합니다. XSD.exe 유틸리티 관리자를 서버에 애플리케이션 만들기 및 배포 서버로 이동 하는 메시지를 serialize 하는 클라이언트가 사용 해야 하는 스키마를 수 있습니다.

클라이언트 애플리케이션의 관리자가 order 클래스에 대 한 스키마를 받으면 XSD.exe 유틸리티 클래스를 생성할 클라이언트 관련 주문 스키마에서 다시 사용 됩니다. 클라이언트 코드 아래 예제에서는 order 클래스가 아닌 서버에에서 사용 되는이 클래스가 (XSD.exe 유틸리티는 원래 클래스와 이름이 같은 스키마에서 생성 된 클래스를 발생 하는 데 사용). 이 새 order 클래스 순서 메시지 본문으로 serialize 됩니다.

다음 코드 예제에서는 클라이언트 쪽 처리, 큐에 순서와 관련 된 정보를 보내고 주문을 serialize 하는 데 사용 됩니다. XSD.exe 유틸리티에서 Order.cs 클래스에 대해 생성 된 스키마의 요소를 사용 하 여 항목, Quantity 및 주소 정보를 연결 하는 코드입니다. 주문은 로컬 컴퓨터의 주문 큐로 전송 됩니다.

#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 클래스에서 생성 된 후에 클래스를 수정할 수 있습니다. 스키마가 변경 하지 않는 한 스키마를 다시 배포할 필요가 없습니다. 스키마를 배포 하 고 클라이언트 쪽 order 클래스를 생성, 후 해당 클라이언트 클래스도 수정할 수 있습니다 서버의 order 클래스와 독립적으로으로 스키마 자체는 수정 되지 않습니다. 두 클래스는 느슨하게 결합 합니다.

설명

합니다 XmlMessageFormatter 은 기본 포맷터는 인스턴스의 MessageQueue 큐에 기록 되는 메시지 직렬화를 사용 하 여 합니다. 인스턴스를 만들면 MessageQueue, 인스턴스의 XmlMessageFormatter 생성 되 고 연결 된는 MessageQueue합니다. 코드에서 만들고 할당 하 여 다른 포맷터를 지정할 수 있습니다 합니다 Formatter 의 속성에 MessageQueue입니다.

큐의 기본 XmlMessageFormatter 인스턴스를 큐에 쓸 수 있지만 중 하나를 설정 하기 전에 큐에서 읽기를 사용할 수는 TargetTypes 또는 TargetTypeNames 포맷터의 속성입니다. 기본 포맷터 인스턴스를 하나 또는 두이 값을 설정 하거나 또는 포맷터의 새 인스턴스를 만들고를 적절 한 인수로 전달 하 여 값을 자동으로 설정할 수 있습니다 XmlMessageFormatter 생성자입니다.

지정 하는 경우 TargetTypes 대신 TargetTypeNames, 오류가 발생할 가능성이 줄어듭니다 읽기 시간 보다는 컴파일 시간에 형식의 존재 여부 검사 됩니다. TargetTypeNames 정규화 된 어셈블리 이름만 지정 되도록 모든 항목이 필요 합니다. 또한 여러 동시 작업 버전을 사용 하는 경우 버전 번호는 대상 형식 이름에 추가 해야 합니다.

합니다 TargetTypeNamesTargetTypes 속성 알 포맷터는 스키마를 메시지를 역직렬화 하는 동안 일치 시 키 려 합니다. 따라서 메시지 본문을 해석 하는 포맷터입니다.

메시지 본문에 serialize 된 인스턴스는 형식 배열에 있는 스키마 중 하나를 사용 하 여 준수 해야 합니다. 사용 하 여 메시지를 읽을 때의 Receive 메서드를 메서드를 메시지 본문을 읽고 식별 스키마에 해당 하는 형식의 개체를 만듭니다.

두 속성 중 하나만 큐에서 읽을 때 설정 해야 하지만 둘 다 설정할 수 있습니다. 형식 집합이 결합된 된 두 속성 집합입니다. 속성을 사용 하 여를 결정 하는 애플리케이션에 따라 다릅니다. 메시지 본문 형식 스키마와 일치 하지 않습니다 형식 속성 중 하나에 대 한 배열에 들어 있으면 메시지를 읽을 때 예외가 throw 됩니다.

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)

메시지 본문으로 개체를 serialize합니다.

적용 대상

추가 정보