Dışarıya aktar (0) Yazdır
Tümünü Genişlet
EN
Bu içerik dilinizde bulunmamaktadır ancak İngilizce sürümüne buradan bakabilirsiniz.

Response Handler Service

Updated: March 13, 2014

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";
    #endregion

    #region Public Operations
    [OperationBehavior]
    public void ReceiveResponse(CalculatorResponseMessage calculatorResponseMessage)
    {
        try
        {
            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 
                                              BrokeredMessageProperty;

                // Trace the response message
                var builder = new StringBuilder();
                if (brokeredMessageProperty != null)
                    builder.AppendLine(string.Format(MessageSuccessfullyReceived, 
                                                     OperationContext.Current.Channel.LocalAddress.Uri.AbsoluteUri,
                                                     brokeredMessageProperty.CorrelationId ?? Empty,
                                                     brokeredMessageProperty.SessionId ?? Empty,
                                                     brokeredMessageProperty.Label ?? Empty));
                builder.AppendLine(PayloadFormat);
                builder.AppendLine(string.Format(StatusFormat,
                                                    calculatorResponse.Status));
                if (calculatorResponse.Results != null && 
                    calculatorResponse.Results.Count > 0)
                {
                    for (int i = 0; i < calculatorResponse.Results.Count; i++)
                    {
                        builder.AppendLine(string.Format(ResultFormat, 
                                                            i + 1, 
                                                            calculatorResponse.Results[i].Value,
                                                            calculatorResponse.Results[i].Error));
                    }
                }
                builder.AppendLine(ReceivedMessagePropertiesHeader);
                if (brokeredMessageProperty != null)
                {
                    foreach (var property in brokeredMessageProperty.Properties)
                    {
                        builder.AppendLine(string.Format(MessagePropertyFormat,
                                                            property.Key,
                                                            property.Value));
                    }
                }
                var traceMessage = builder.ToString();
                Trace.WriteLine(traceMessage.Substring(0, traceMessage.Length - 1));

                //Complete the Message
                ReceiveContext receiveContext;
                if (ReceiveContext.TryGet(incomingProperties, out receiveContext))
                {
                    receiveContext.Complete(TimeSpan.FromSeconds(10.0d));
                }
                else
                {
                    throw new InvalidOperationException("An exception occurred.");
                }
            }
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message);
        }
    } 
    #endregion
}

Show:
© 2014 Microsoft