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 Using Directives
using System;
using System.IO;
using System.Net;
using Microsoft.XLANGs.BaseTypes;
using Microsoft.WindowsAzure.CAT.Samples.ServiceBusForWindowsServer.PropertySchemas;
#endregion

namespace Microsoft.WindowsAzure.CAT.Samples.ServiceBusForWindowsServer.BusinessLogic
{
    /// <summary>
    /// This class contains helper methods used by the orchestrations.
    /// </summary>
    [Serializable]
    public class ResponseManager
    {
        #region Private Fields
        //***************************
        // Private Fields
        //***************************
        private readonly LogHelper logHelper = new LogHelper();
        #endregion

        #region Public Methods
        //***************************
        // Public Methods
        //***************************

        /// <summary>
        /// Sets the content of the XLANGMessage passed as first parameter.
        /// </summary>
        /// <param name="message">An XLANGMessage object.</param>
        /// <param name="stream">The stream containing the payload to assign to the message.</param>
        public void SetResponse(XLANGMessage message, Stream stream)
        {
            try
            {
                if (stream != null &&
                    message != null &&
                    message.Count > 0)
                {
                    if (stream.CanSeek)
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                    message[0].LoadFrom(stream);
                }
            }
            catch (Exception ex)
            {
                logHelper.WriteLine(string.Format("[ResponseManager] {0}", ex.Message));
            }
            finally
            {
                if (message != null)
                {
                    message.Dispose();
                }
            }
        } 
        /// <summary>
        /// Gets the action for the response request.
        /// </summary>
        /// <param name="request">The request request.</param>
        /// <returns>The action to assign to the Action header of the response request.</returns>
        public string GetAction(XLANGMessage request)
        {
            const string action = "ReceiveResponse";

            try
            {
                // In a real application, the action should be retrieved from a
                // configuration repository based on the request request content and context
                // information. In addition, this data should be cached to improve performance.
                return action;
            }
            catch (Exception ex)
            {
                logHelper.WriteLine(string.Format("[ResponseManager] {0}", ex.Message));
            }
            finally
            {
                logHelper.WriteLine(string.Format("[ResponseManager] Action=[{0}]", action ?? "NULL"));
                if (request != null)
                {
                    request.Dispose();
                }
            }
            // Return default Action.
            return action;
        }

        /// <summary>
        /// Gets the binding configuration for the response request.
        /// </summary>
        /// <param name="request">The request request.</param>
        /// <returns>The binding configuration of the dynamic send port used to send
        /// out the response request.</returns>
        public string GetBindingConfiguration(XLANGMessage request)
        {
            const string bindingConfiguration = @"<binding name=""netMessagingBinding"" " +
                                                @"sendTimeout=""00:03:00"" " +
                                                @"receiveTimeout=""00:03:00"" " +
                                                @"openTimeout=""00:03:00"" " +
                                                @"closeTimeout=""00:03:00"" " +
                                                @"sessionIdleTimeout=""00:01:00"" " +
                                                @"prefetchCount=""-1""> " +
                                                @"<transportSettings batchFlushInterval=""00:00:01"" enableRedirect=""True""/>" +
                                                @"</binding>";
            try
            {
                // In a real application, the binding configuration should be retrieved from a
                // configuration repository based on the request request content and context
                // information. In addition, this data should be cached to improve performance.
                return bindingConfiguration;
            }
            catch (Exception ex)
            {
                logHelper.WriteLine(string.Format("[ResponseManager] {0}", ex.Message));
            }
            finally
            {
                logHelper.WriteLine(string.Format("[ResponseManager] BindingConfiguration=[{0}]", bindingConfiguration ?? "NULL"));
                if (request != null)
                {
                    request.Dispose();
                }
            }
            // Return default binding configuration.
            return bindingConfiguration;
        }

        /// <summary>
        /// Gets endpoint behavior configuration of the dynamic 
        /// send port used to send out the response request.
        /// </summary>
        /// <param name="request">The request request.</param>
        /// <returns>The endpoint behavior configuration of the dynamic 
        /// send port used to send out the response request.</returns>
        public string GetEndpointBehaviorConfiguration(XLANGMessage request)
        {
            var endpointBehaviorConfiguration = @"<behavior name=""EndpointBehavior"">" +
                                                @"<transportClientEndpointBehavior>" +
                                                @"<tokenProvider>" +
                                                @"<windowsAuthentication>" +
                                                @"<stsUris>" +
                                                "<stsUri value=\"https://" + Dns.GetHostEntry(string.Empty).HostName + ":9355/ServiceBusDefaultNamespace\" />" +
                                                @"</stsUris>" +
                                                @"</windowsAuthentication>" +
                                                @"</tokenProvider>" +
                                                @"</transportClientEndpointBehavior>" +
                                                @"<bizTalkMessageInspector>" +
                                                @"<propertiesToSend name=""Application,Country,City"" " +
                                                @"namespace=""http://windowsazure.cat.microsoft.com/samples/servicebusforwindowsserver/propertyschema," + 
                                                @"http://windowsazure.cat.microsoft.com/samples/servicebusforwindowsserver/propertyschema," +
                                                @"http://windowsazure.cat.microsoft.com/samples/servicebusforwindowsserver/propertyschema"" />" +
                                                @"</bizTalkMessageInspector>" +
                                                @"</behavior>";
            try
            {
                // In a real application, the endpoint behavior configuration should be retrieved from a
                // configuration repository based on the request request content and context
                // information. In addition, this data should be cached to improve performance.
                return endpointBehaviorConfiguration;
            }
            catch (Exception ex)
            {              
                logHelper.WriteLine(string.Format("[ResponseManager] {0}", ex.Message));
            }
            finally
            {
                logHelper.WriteLine(string.Format("[ResponseManager] EndpointBehaviorConfiguration=[{0}]", endpointBehaviorConfiguration ?? "NULL"));
                if (request != null)
                {
                    request.Dispose();
                }
            }
            // Return default endpoint behavior configuration.
            return string.Empty;
        }

        /// <summary>
        /// Gets the value of the MessageId context property.
        /// </summary>
        /// <param name="request">The request request.</param>
        /// <returns>The value of the MessageId context property.</returns>
        public string GetMessageId(XLANGMessage request)
        {
            try
            {
                if (request != null)
                {
                    var messageId = request.GetPropertyValue(typeof(MessageId)) as string;
                    if (!string.IsNullOrEmpty(messageId))
                    {
                        logHelper.WriteLine(string.Format("[ResponseManager] MessageId=[{0}]", messageId));
                        return messageId;
                    }
                }
            }
            catch (Exception ex)
            {
                logHelper.WriteLine(string.Format("[ResponseManager] Exception: {0}", ex.Message));
            }
            finally
            {
                if (request != null)
                {
                    request.Dispose();
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// Gets the value of the ReplyTo context property.
        /// </summary>
        /// <param name="request">The request request.</param>
        /// <returns>The value of the ReplyTo context property.</returns>
        public string GetReplyTo(XLANGMessage request)
        {
            try
            {
                if (request != null)
                {
                    var replyTo = request.GetPropertyValue(typeof(ReplyTo)) as string;
                    if (!string.IsNullOrEmpty(replyTo))
                    {
                        logHelper.WriteLine(string.Format("[ResponseManager] ReplyTo=[{0}]", replyTo));
                        return replyTo;
                    }
                }
            }
            catch (Exception ex)
            {
                logHelper.WriteLine(string.Format("[ResponseManager] Exception: {0}", ex.Message));
            }
            finally
            {
                if (request != null)
                {
                    request.Dispose();
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// Gets the value of the ReplyToSessionId context property.
        /// </summary>
        /// <param name="request">The request request.</param>
        /// <returns>The value of the ReplyToSessionId context property.</returns>
        public string GetReplyToSessionId(XLANGMessage request)
        {
            try
            {
                if (request != null)
                {
                    var replyToSessionId = request.GetPropertyValue(typeof(ReplyToSessionId)) as string;
                    if (!string.IsNullOrEmpty(replyToSessionId))
                    {
                        logHelper.WriteLine(string.Format("[ResponseManager] ReplyToSessionId=[{0}]", replyToSessionId));
                        return replyToSessionId;
                    }
                    return !string.IsNullOrEmpty(replyToSessionId) ? replyToSessionId : string.Empty;
                }
            }
            catch (Exception ex)
            {
                logHelper.WriteLine(string.Format("[ResponseManager] Exception: {0}", ex.Message));
            }
            finally
            {
                if (request != null)
                {
                    request.Dispose();
                }
            }
            return string.Empty;
        }
        #endregion
    }
}