BinaryMessageFormatter 클래스

정의

개체나 연결된 개체의 전체 그래프를 메시지 큐 본문으로 또는 메시지 큐 본문에서 이진 형식으로 직렬화하거나 역직렬화합니다.

public ref class BinaryMessageFormatter : ICloneable, System::Messaging::IMessageFormatter
public class BinaryMessageFormatter : ICloneable, System.Messaging.IMessageFormatter
type BinaryMessageFormatter = class
    interface IMessageFormatter
    interface ICloneable
Public Class BinaryMessageFormatter
Implements ICloneable, IMessageFormatter
상속
BinaryMessageFormatter
구현

예제

using System;
using System.Messaging;
using System.Drawing;
using System.IO;

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 queue.
        //**************************************************

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

            // Create a queue on the local computer.
            CreateQueue(".\\myQueue");
            
            // Send a message to a queue.
            myNewQueue.SendMessage();

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

            return;
        }

        //**************************************************
        // Creates a new queue.
        //**************************************************

        public static void CreateQueue(string queuePath)
        {
            try	
            {
                if(!MessageQueue.Exists(queuePath))
                {
                    MessageQueue.Create(queuePath);
                }
                else
                {
                    Console.WriteLine(queuePath + " already exists.");
                }
            }
            catch (MessageQueueException e)
            {
                Console.WriteLine(e.Message);
            }
        }

        //**************************************************
        // Sends an image to a queue, using the BinaryMessageFormatter.
        //**************************************************
        
        public void SendMessage()
        {
            try{

                // Create a new bitmap.
                // The file must be in the \bin\debug or \bin\retail folder, or
                // you must give a full path to its location.
                Image myImage = Bitmap.FromFile("SentImage.bmp");

                // Connect to a queue on the local computer.
                MessageQueue myQueue = new MessageQueue(".\\myQueue");
                
                Message myMessage = new Message(myImage, new BinaryMessageFormatter());

                // Send the image to the queue.
                myQueue.Send(myMessage);
            }
            catch(ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }

            return;
        }

        //**************************************************
        // Receives a message containing an image.
        //**************************************************
        
        public  void ReceiveMessage()
        {
                        
            try
            {

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

                // Set the formatter to indicate body contains an Order.
                myQueue.Formatter = new BinaryMessageFormatter();

                // Receive and format the message.
                System.Messaging.Message myMessage = myQueue.Receive();
                Bitmap myImage = (Bitmap)myMessage.Body;
                
                // This will be saved in the \bin\debug or \bin\retail folder.
                myImage.Save("ReceivedImage.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
            }
            
            catch (MessageQueueException)
            {
                // Handle Message Queuing exceptions.
            }

            // Handle invalid serialization format.
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.Message);
            }

            catch (IOException e)
            {
                // Handle file access exceptions.
            }
            
            // Catch other exceptions as necessary.

            return;
        }
    }
}
Imports System.Messaging
Imports System.Drawing
Imports System.IO


Namespace MyProj
    _
   
   
   Public Class MyNewQueue
      
      
      '**************************************************
      ' Provides an entry point into the application.
      '		 
      ' This example sends and receives a message from
      ' a queue.
      '**************************************************
      Public Shared Sub Main()
         ' Create a new instance of the class.
         Dim myNewQueue As New MyNewQueue()
         
         ' Create a queue on the local computer.
         CreateQueue(".\myQueue")
         
         ' Send a message to a queue.
         myNewQueue.SendMessage()
         
         ' Receive a message from a queue.
         myNewQueue.ReceiveMessage()
         
         Return
      End Sub
      
      
      '**************************************************
      ' Creates a new queue.
      '**************************************************
      Public Shared Sub CreateQueue(queuePath As String)
         Try
            If Not MessageQueue.Exists(queuePath) Then
               MessageQueue.Create(queuePath)
            Else
               Console.WriteLine((queuePath + " already exists."))
            End If
         Catch e As MessageQueueException
            Console.WriteLine(e.Message)
         End Try
      End Sub
       
      
      '**************************************************
      ' Sends an image to a queue, using the BinaryMessageFormatter.
      '**************************************************
      Public Sub SendMessage()
         Try
            
            ' Create a new bitmap.
            ' The file must be in the \bin\debug or \bin\retail folder, or
            ' you must give a full path to its location.
            Dim myImage As Image = Bitmap.FromFile("SentImage.bmp")
            
            ' Connect to a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myQueue")
            
            Dim myMessage As New Message(myImage, New BinaryMessageFormatter())
            
            ' Send the image to the queue.
            myQueue.Send(myMessage)
         Catch e As ArgumentException
            Console.WriteLine(e.Message)
         End Try 
         
         Return
      End Sub
      
      
      
      '**************************************************
      ' Receives a message containing an image.
      '**************************************************
      Public Sub ReceiveMessage()
         
         Try
            
            ' Connect to the a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myQueue")
            
            ' Set the formatter to indicate body contains an Order.
            myQueue.Formatter = New BinaryMessageFormatter()
            
            ' Receive and format the message. 
            Dim myMessage As System.Messaging.Message = myQueue.Receive()
            Dim myImage As Bitmap = CType(myMessage.Body, Bitmap)
            
            ' This will be saved in the \bin\debug or \bin\retail folder.
            myImage.Save("ReceivedImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
         
         
         
         'Catch
         ' Handle Message Queuing exceptions.
         
         ' Handle invalid serialization format.
         Catch e As InvalidOperationException
            Console.WriteLine(e.Message)
         
         Catch e As IOException
         End Try
         ' Handle file access exceptions.
         
         ' Catch other exceptions as necessary.
         Return
      End Sub
   End Class
End Namespace 'MyProj

설명

중요

신뢰할 수 없는 데이터로 이 클래스에서 메서드를 호출하는 것은 보안상 위험합니다. 신뢰할 수 있는 데이터로만 이 클래스에서 메서드를 호출하세요. 자세한 내용은 모든 입력 유효성 검사를 참조하세요.

BinaryMessageFormatter 매우 효율적이며 대부분의 개체를 직렬화하는 데 사용할 수 있습니다. 결과는 매우 작고 구문 분석 속도가 빠르지만 와 같이 느슨하게 결합된 메시징을 XmlMessageFormatter 허용하지 않습니다. 느슨하게 결합됨은 클라이언트와 서버가 보내고 받은 형식의 버전을 독립적으로 버전화할 수 있음을 의미합니다.

애플리케이션의 인스턴스를 사용 하 여 큐에 메시지를 보낼 때의 MessageQueue 포맷터 클래스 개체를 스트림으로 serialize 하 고 메시지 본문에 삽입 합니다. 사용 하 여 큐에서 읽을 때를 MessageQueue, 포맷터 메시지 데이터를 역직렬화합니다 Body 의 속성을 Message.

BinaryMessageFormatter 는 보다 XmlMessageFormatter더 빠른 처리량을 제공합니다. BinaryMessageFormatter 느슨하게 결합된 메시징이 아닌 순수한 속도를 원하는 경우 를 사용합니다.

생성자

BinaryMessageFormatter()

형식 스타일이나 Top 개체 어셈블리 스타일을 지정하지 않고 BinaryMessageFormatter 클래스의 새 인스턴스를 초기화합니다.

BinaryMessageFormatter(FormatterAssemblyStyle, FormatterTypeStyle)

루트 개체와 형식 설명의 형식을 지정하지 않고 BinaryMessageFormatter 클래스의 새 인스턴스를 초기화합니다.

속성

TopObjectFormat

어셈블리를 찾고 로드하는 작업과 관련하여 그래프의 Top(루트) 개체가 역직렬화되는 방식을 정의하는 값을 가져오거나 설정합니다.

TypeFormat

serialize된 스트림에서 형식 설명이 레이아웃되는 방법을 정의하는 값을 가져오거나 설정합니다.

메서드

CanRead(Message)

포맷터에서 메시지 내용을 역직렬화할 수 있는지 여부를 확인합니다.

Clone()

현재 BinaryMessageFormatter와 읽기/쓰기 속성(루트 개체 및 형식 설명 형식)이 같은 BinaryMessageFormatter 클래스의 인스턴스를 만듭니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
Read(Message)

지정된 메시지의 내용을 읽고 역직렬화된 메시지가 포함된 개체를 만듭니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)
Write(Message, Object)

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

적용 대상

추가 정보