Skip to main content

How to integrate BizTalk Server 2010 / 2013 with Service Bus for Windows Server

This solution shows how to integrate a BizTalk Server 2010/2013 application with Service Bus for Windows Server using the WCF-Custom adapter to exchange messages with external systems in a reliable, flexible, and scalable manner.

C# (5.3 MB)
 
 
 
 
 
4.7 Star
(6)
1,708 times
Add to favorites
6/20/2014
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
#region Copyright
//=======================================================================================
//Microsoft Windows Server AppFabric Customer Advisory Team (CAT)  
//
// This sample is supplemental to the technical guidance published on the community
// blog at http://blogs.msdn.com/paolos. 
// 
// Author: Paolo Salvatori
//=======================================================================================
// Copyright © 2011 Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER 
// EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. YOU BEAR THE RISK OF USING IT.
//=======================================================================================
#endregion

#region Using Directives
using System;
using System.Text;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Microsoft.ServiceBus.Messaging;
using Microsoft.WindowsAzure.CAT.Samples.ServiceBusForWindowsServer.MessageContracts;
using Microsoft.WindowsAzure.CAT.Samples.ServiceBusForWindowsServer.ServiceContracts;
#endregion

namespace Microsoft.WindowsAzure.CAT.Samples.ServiceBusForWindowsServer.Service
{
    [ServiceBehavior(Namespace = "http://windowsazure.cat.microsoft.com/samples/servicebusforwindowsserver")]
    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
            {
                // Get the message properties
                var incomingProperties = OperationContext.Current.IncomingMessageProperties;

                if (calculatorResponseMessage != null &&
                    calculatorResponseMessage.CalculatorResponse != null)
                {
                    // Initialize calculatorResponse var
                    var calculatorResponse = calculatorResponseMessage.CalculatorResponse;

                    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
                {
                    receiveContext.Abandon(TimeSpan.FromSeconds(10.0d));
                    throw new InvalidOperationException("Receiver is in peek lock mode but receive context is not available!");
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        #endregion
    }
}