(0) exportieren Drucken
Alle erweitern
Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
Übersetzung
Original

Message.Close-Methode

Schließt Message und gibt alle Ressourcen frei.

Namespace:  System.ServiceModel.Channels
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public void Close()

Message verfügt über einen Finalizer, wodurch Close bei der Garbage Collection für die Nachricht aufgerufen wird. Dies ist nicht optimal, da der Garbage Collection (GC)-Mechanismus von .NET Framework nicht unbedingt ausgeführt wird, wenn andere Ressourcen als der Arbeitsspeicher zur Neige gehen. Aus diesem Grund sollten Sie diese Methode immer aufrufen, wenn Sie den Inhalt der Nachricht nicht mehr benötigen. Die Close-Methode ist synonym mit Dispose (die ebenfalls durch Message implementiert wird). Die Nachricht gibt außerdem das Objekt frei, das zum Konstruieren des Texts verwendet wurde, wenn er verworfen wird.

Wenn Sie eine Methode aufrufen oder auf Eigenschaften der Nachricht zugreifen, nachdem sie geschlossen wurde, wird eine ObjectDisposedException ausgelöst. Das Aufrufen einer Methode oder Zugreifen auf Eigenschaften anderer Objekte, die sich auf die Nachricht beziehen, nachdem die Nachricht geschlossen wurde (wie z. B. Auflistung der Nachrichtenheader, Eigenschaftswerte der Nachricht oder für den Text bzw. einen Header zurückgegebene XmlReader-Instanzen), weist ein nicht definiertes Verhalten auf.

Das folgende Codebeispiel veranschaulicht das Erstellen einer Nachricht, Senden der Nachricht an einen Dienst und Empfangen einer Antwortnachricht. Der Inhalt der Antwortnachricht wird angezeigt, und die Nachricht wird anschließend geschlossen.



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);
    }
}



.NET Framework

Unterstützt in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Unterstützt in: 4, 3.5 SP1

Portable Klassenbibliothek

Unterstützt in: Portable Klassenbibliothek

.NET für Windows Store-Apps

Unterstützt in: Windows 8

.NET für Windows Phone-Apps

Unterstützt in: Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core-Rolle wird nicht unterstützt), Windows Server 2008 R2 (Server Core-Rolle wird mit SP1 oder höher unterstützt; Itanium wird nicht unterstützt)

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.

Community-Beiträge

HINZUFÜGEN
Anzeigen:
© 2014 Microsoft