Extending the ESB Guidance

If what you get from ESB Guidance is not enough, there are three areas you should look in to:

%u00b7         Adapter Providers

%u00b7         Resolvers

%u00b7         Services

Building your own Adapter Provider

When the message has been processed, it is going to be sent to its destination through a dynamic send port, (a.k.a off-ramp). Usually, when using a dynamic port, we’d set the port properties such as transport type and location programmatically in an expression shape (orchestration) or in a custom pipeline component.

In the case of ESB Guidance this is all done using an Adapter Provider. In other words, the Adapter Provider serves to write and promote context properties for specific adapters.

ESB Guidance comes with support for the most common adapters such as WCF-BasicHttp, WCF-WSHttp, MQSeries, FILE and FTP. But for the purpose of this article, we’ll create a SMTP provider.

 

1.       Start of by creating a new Class Library project. And rename the default class to AdapterProvider.

2.       Let the AdapterProvider inherit from the Microsoft.Practices.ESB.Adapter .IAdapterProvider interface and implement the two SetEndpoint methods. (I’m aware the code sample does not fit the size of the frame, but it works if you want to copy the text)

        public void SetEndpoint(Dictionary<string, string> ResolverDictionary, IBaseMessageContext pipelineContext)
        {
            if (null == ResolverDictionary)
                throw new ArgumentNullException("ResolverDictionary");
            if (null == pipelineContext)
                throw new ArgumentNullException("pipelineContext");

            try
            {
                string transportLocation = ResolverDictionary["Resolver.TransportLocation"];
                string endpointConfig = ResolverDictionary["Resolver.EndpointConfig"];

                string to = transportLocation.Replace("SMTP://", "");
                string from = string.Empty;
                string subject = string.Empty;
                string host = string.Empty;
                
                string[] endpointConfigs = endpointConfig.Split(':');

                foreach (string config in endpointConfigs)
                {
                    string[] configs = config.Split('=');
                    switch (configs[0].ToUpper())
                    {
                        case "SMTPHOST":
                            host = configs[1];
                            break;
                        case "FROM":
                            from = configs[1];
                            break;
                        case "SUBJECT":
                            subject = configs[1];
                            break;
                        default:
                            throw new ArgumentNullException("Invalid SMTP Adapter Provider Configuration");
                    } 
                }
                
                //First, set the outboundtransportlocation, transporttype and SMTP specific attributes
                pipelineContext.Write(BtsProperties.OutboundTransportLocation.Name, BtsProperties.OutboundTransportLocation.Namespace, transportLocation);
                pipelineContext.Write(BtsProperties.OutboundTransportType.Name, BtsProperties.OutboundTransportType.Namespace, "SMTP");
                pipelineContext.Write("From", "http://schemas.microsoft.com/BizTalk/2003/smtp-properties", from);
                pipelineContext.Write("OutboundTransportLocation", "http://schemas.microsoft.com/BizTalk/2003/system-properties", "mailto:" + to);
                pipelineContext.Write("Subject", "http://schemas.microsoft.com/BizTalk/2003/smtp-properties", subject);
                pipelineContext.Write("SMTPHost", "http://schemas.microsoft.com/BizTalk/2003/smtp-properties", host);

                // Second, loop through the endpointconfig, this should be ; seperated and set properties
                // by namespace of specific adapter
                if (!string.IsNullOrEmpty(endpointConfig))
                    AdapterMgr.SetContextProperties(pipelineContext, ResolverDictionary);

            }
            catch (System.Exception ex)
            {
                EventLogger.Write(MethodInfo.GetCurrentMethod(), ex);
                throw;
            }
        }

3.       Build and GAC the project. 

4.       Add the new AdapterProvider to the <AdapterProvider> section of your machine.config.

Eg:

<add key="SMTP"
         value="Blogical.Adapter.SMTP,
                Version=1.0.0.0, Culture=neutral,
                PublicKeyToken=c2c8b2b87f54180a" />

5.       Restart IIS and BizTalk.

6.       Create an Itinerary and attach a STATIC resolver to your service (“DynamicTest” in this sample)

E.g:

<Resolvers serviceId="DynamicTest0">&lt;![CDATA[
STATIC:\\TransportLocation=SMTP://[email protected];
EndpointConfig=SMTPHOST=labs:[email protected]:SUBJECT=This is a test mail;]]&gt;</Resolvers>

 ESB Guidance…part 5