Exportar (0) Imprimir
Expandir todo
Este tema aún no ha recibido ninguna valoración - Valorar este tema

Response Handler Service

The following example contains the code of the WCF service used by the client application to retrieve and log response messages from the responsequeue and ItalyMilan subscription of the responsetopic. To accomplish this result, the service exposes two different endpoints each of which uses the NetMessagingBinding and receives messages from one of the two queues. Indeed, each subscription can be seen as a virtual queue getting copies of messages published to the topic they belong to. The example below shows the code of the ResponseHandlerService class. As you can notice, the service retrieves the BrokeredMessageProperty from the Properties collection of the incoming WCF message and uses this object to access to the properties of the response message. Since in the ICalculatorResponse service contract the ReceiveResponse method is decorated with the [ReceiveContextEnabled(ManualControl = true)] attribute, the service method must explicitly signal the receive. This signaling requires the service to explicitly invoke the ReceiveContext.Complete method to commit the receive operation. In fact, as we said at the beginning of the article, when the ManualControl property is set to true, the message received from the channel is delivered to the service operation with a lock for the message. The service implementation must call either Complete(TimeSpan) or Abandon(TimeSpan) to signal the receive completion of the message. Failure to call either of these methods causes the lock to be held on the message until the lock timeout interval elapses. Once the lock is released (either through a call to Abandon(TimeSpan)) or lock timeout) the message is redispatched from the channel to the service. Calling Complete(TimeSpan) marks the message as successfully received.

ResponseHandlerService Class

[ServiceBehavior(Namespace = "http://windowsazure.cat.microsoft.com/samples/servicebus")]
public class ResponseHandlerService : ICalculatorResponseSessionful
    #region Private Constants
    // Formats
    private const string MessageSuccessfullyReceived = "Response Message Received:\n - EndpointUrl:[{0}]" +
                                                        "\n - CorrelationId=[{1}]\n - SessionId=[{2}]\n - Label=[{3}]";
    private const string ReceivedMessagePropertiesHeader = "Properties:";
    private const string PayloadFormat = "Payload:";
    private const string StatusFormat = " - Status=[{0}]";
    private const string ResultFormat = " - Result[{0}]: Value=[{1}] Error=[{2}]";
    private const string MessagePropertyFormat = " - Key=[{0}] Value=[{1}]";

    // Constants
    private const string Empty = "EMPTY";

    #region Public Operations
    public void ReceiveResponse(CalculatorResponseMessage calculatorResponseMessage)
            if (calculatorResponseMessage != null &&
                calculatorResponseMessage.CalculatorResponse != null)
                // Initialize calculatorResponse var
                var calculatorResponse = calculatorResponseMessage.CalculatorResponse;

                // Get the message properties
                var incomingProperties = OperationContext.Current.IncomingMessageProperties;
                var brokeredMessageProperty = incomingProperties[BrokeredMessageProperty.Name] as 

                // Trace the response message
                var builder = new StringBuilder();
                if (brokeredMessageProperty != null)
                                                     brokeredMessageProperty.CorrelationId ?? Empty,
                                                     brokeredMessageProperty.SessionId ?? Empty,
                                                     brokeredMessageProperty.Label ?? Empty));
                if (calculatorResponse.Results != null && 
                    calculatorResponse.Results.Count > 0)
                    for (int i = 0; i < calculatorResponse.Results.Count; i++)
                                                            i + 1, 
                if (brokeredMessageProperty != null)
                    foreach (var property in brokeredMessageProperty.Properties)
                var traceMessage = builder.ToString();
                Trace.WriteLine(traceMessage.Substring(0, traceMessage.Length - 1));

                //Complete the Message
                ReceiveContext receiveContext;
                if (ReceiveContext.TryGet(incomingProperties, out receiveContext))
                    throw new InvalidOperationException("An exception occurred.");
        catch (Exception ex)

Fecha de compilación:

¿Te ha resultado útil?
(Caracteres restantes: 1500)
Gracias por sus comentarios

Adiciones de comunidad

© 2014 Microsoft. Reservados todos los derechos.