Thursday, October 8, 2015

BizTalk Server: Receiving SAP IDOC Messages


Introduction

"Intermediate documents (IDOCS) are standardized EDI-like documents supported by SAP for asynchronously communicating with both SAP and non-SAP systems. IDOCS are used to send and receive business documents like sales orders to or from a trading partner’s SAP system or external program."
The below is a sample file of IDOC message







In this post, I will explain how to receive and process IDOC message using BizTalk.
Note that I supposed that you installed sap client and sap-wcf adapter in your development machine.

Problem

You have a new request that in your integration middle-ware that using BizTalk server needs to integrates with SAP ERP system which will generate outbound messages for external system using IDOC messages over file system or ftp..etc. As a role of BizTalk developer you need to know how to parse this message and process it into your internal business process.

Solution

BizTalk Server provides a nice wcf-sap adapter to connect to SAP and generate a meta data (schema) of related IDOC messages.
First, when you check the structure of IDOC message, you will notice that it is a flat file content
which means you need to have a flat file schema as shown in previous sample message image.

IDOC messages are complicated messages, so you don't need to build schema manually because it will take a lot of time to build such kind of schema instead you can use wcf-sap adapter to generate schema for you.

There are some tricks and tips you need to consider to generate a proper IDOC schema which I will explain in the followings steps:

  1. Create a new BizTalk Project è  right click project è  select Add è  Add Generated Item
  2. In Add Generated Item Window select Consume Adapter Service è  Consume Adapter Service
  3. In Consume Adapter Service windows select sapBinding in Select a binding drop down list è  click configure button
  4. In Configure Adapter window security tab select User Name in Client credential type drop down list è  enter User name and password as provided by SAP admin
  5.  Select URI Properties tab then set Application Server Host è  Set System Number è   Set Client è   Set Connection type as provided by SAP team è  Click Ok button.  Note these are the minimum requirements you may need to fill other properties in case there are some special configuration in SAP server side
  6.  In Consume Adapter Service window click connect buttonè   select Service (Inbound operations) from select contract type drop down list è  Select IDOC node

      Note: you may receive the following error when you try to search for a message type without selcting IDOC node
  7. Let's say that you interested in Address Master message which is is ARDMAS when you try to search on ADRMAS* you will find this
  8. When you try to select message you will get the following error 
  9. Tip: Don't try to search for SAP IDOC message using search feature, search for message manually using your eyes instead, you are lucky because messages ordered alphabetically :)
  10. Let's Say SAP team informed us to use ADRMAS03.V3(731) which is the latest version  è select the message è    Select Receive then click Add button then click ok button 

  11. Now you can see the generated files which contain 4 scehmas and Binding files. 
  12. Note: IDocOperation.ADRMAS03.731.3.Receive is the main schema that you want to deal with , IDoc.ADRMAS03.731.3 is schema used by previous schema, IDocSharedTypes which is datatype schema imported by IDoc.ADRMAS03.731.3 schema. You can ignore IDocOperation.ADRMAS03.731.3.ReceiveResponse schema and Binding file  because we only receiving file over file system
  13.  When you check IDocOperation.ADRMAS03.731.3.Receive schema you will find out there is no a flat file tab and child delimiter  0xd 0xa which means it is expecting file that generated from Windows system which is Carriage Return Line feed (CR+LF), however in my case SAP in Linux os that generates file with 0x0A Line Feed (LF) delimiter inside the file content
  14. We need to change Schema Editor Extensions to Flat File and we need to replace child delimiter 0xd 0xa with  0x0A in IDocOperation.ADRMAS03.731.3.Receive.xsd and IDoc.ADRMAS03.731.3.xsd , open files using xml editor then replace. The question what if you receive IDOC messages from different SAP providers one using window and other using Linux then you need to use a customer pipeline to replace new line delimiter instead of updating schema manually.

  15. You can test your IDOC message by validating message using schema editor and setting Validate Instance Input type to Native and Input Instance Filename with path of your sample file 
  16. Now you need to create receive pipeline and add Flat file disassembler to Disassemble component and set Document Schema to IDocOperation.ADRMAS03.731.3.Receive schema data type 
  17. Now you are ready to receive idoc message from any File system, ftp, sftp ...etc , create related adapter and set your receive flat file pipeline then you can convert flat file file to idoc xml document

Conclusion 

In this post I walked-throughed how to use wcf-sap adapter to generate IDOC schemas and I highlighted some tips and tricks for working IDOC message which I hope you will help you in any future works

Wednesday, August 5, 2015

BizTalk Server: Extending WCF Adapter



Introduction

One of the good features of BizTalk server is the extensibility of WCF adapter. WCF (Windows Communication Foundation) enables developers to extend and updates its standard run time behavior with several extensibility points like behavior, binding, binding element, channels, etc.
In this post, I will explain the followings:
  • How to extend BizTalk WCF Adapter?
  • Illustrate how to overcome one of the limitations of WCF Adatpter

Problem


I faced an issue when I tried to integrate with a web service of one of our customers.
They sent us a web service url and this url is a long url (261 characters!!) when I tried to configure a BizTalk WCF adapter it gave me the following error as shown in figure 1:
"Error saving properties.(System.ArgumentException) Invalid address length; specified address length is 261 characters, limit is 256 characters."



Figure 1. Error Message


If you try to consume a web service using a console application you will not face any issue related to url length because there is not a limit for address property.However, BizTalk WCF adapter has a limit because BizTalk team defined End Point Address property of WCF Adapter as type of URI with length 256.


Solution


To work around this issue, WCF adapter provides the capabilities to be updated and extended.
What we need is to have custom endpoint behavior and to provide a dummy url to EndPoint Address URL and provide a real long address in an extended custom property

To prepare a custom endpoint behavior we need to followings the following steps:
  1. Create a class library project and we will call it as TechnetWiki.CustomEndpointBehavior as shown in figure 2


    Figure 2. Creating Class Library Project
  2. Now we need to create 2 classes one to extend abstract class BehaviorExtensionElement  to define custom property for our custom url and interface IEndpointBehavior to set a custom url to Endpoint url at run time.To use these abstract class and interface we need to add reference to System.ServiceModel dll as shown
  3. Create a new class and we will call it as CustomBehaviorExtensionElement that extends BehaviorExtensionElement
    using System;
    using System.Configuration;
    using System.ServiceModel.Configuration;
    namespace TechnetWiki.CustomEndpointBehavior
    {
        public class CustomBehaviorExtensionElement : BehaviorExtensionElement
        {
            private ConfigurationPropertyCollection properties;
            [ConfigurationProperty("CustomAddress")]
            public Uri CustomAddress
            {
                get
                {
                    return (Uri)base["CustomAddress"];
                }
                set
                {
                    base["CustomAddress"] = value;
                }
            }
            public override Type BehaviorType
            {
                get
                {
                    return typeof(CustomEndpointBehavior);
                }
            }
            protected override ConfigurationPropertyCollection Properties
            {
                get
                {
                    if (this.properties == null)
                    {
                        this.properties = new ConfigurationPropertyCollection
                        {
                            new ConfigurationProperty("CustomAddress", typeof(Uri), null, null, null, ConfigurationPropertyOptions.IsRequired)
                        };
                    }
                    return this.properties;
                }
            }
            protected override object CreateBehavior()
            {
                return new CustomEndpointBehavior(this.CustomAddress);
            }
        }
    }
  4. Create another class called CustomEndpointBehavior that extends IEndpointBehavior interface
    using System;
    using System.ServiceModel.Description;
    namespace TechnetWiki.CustomEndpointBehavior
    {
        public class CustomEndpointBehavior : IEndpointBehavior
        {     
            public Uri CustomAddressUri { get; set; }
            public CustomEndpointBehavior(Uri customAddressUri)
            {
                if (customAddressUri == null)
                {
                    throw new ArgumentNullException("customAddressUri");
                }
                this.CustomAddressUri = customAddressUri;
            }
            public void AddBindingParameters(ServiceEndpoint serviceEndpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
            {
            }
            public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, System.ServiceModel.Dispatcher.ClientRuntime behavior)
            {
                serviceEndpoint.Address = new System.ServiceModel.EndpointAddress(this.CustomAddressUri);
            }
            public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
            {
            }
            public void Validate(ServiceEndpoint serviceEndpoint)
            {
            }
        }
    }
  5. Sign assembly by defining a strong key for the class library project as shown in figure 3


    Figure 3. Sign the assembly.
  6. Add assembly to GAC either by adding assembly to resources of BizTalk Application in BizTalk administration as shown in figure 4 or by run gacutil -i "{AssemblyPath}" command.<

    Figure 4. Adding Assembly to GAC
  7. To register behavior extensions to WCF Adapter, you need to update machine.config in path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config" by adding the following configuration inside behaviorExtensions tag <add name="customEndpointBehavior" type="TechnetWiki.CustomEndpointBehavior.CustomBehaviorExtensionElement, TechnetWiki.CustomEndpointBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=415d904e2f9a9bcd" />
    Note: in case your WCF-Custom handler runs under 64 bit you need to update the machine.config in the following path  "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config"
  8. Now we are ready to use extension in WCF adapter, you need to create WCF-Custom send adapter and set any dummy url in Address (URI) as shown in figure 5


    Figure 5. Setting Dummy URL
  9. Select Behavior tab and right click EndPointBehavior then select customEndPointBehavior which is the extension the we created and define in machine.config as shown in figure 6


    Figure 6. Select Extention
  10. Set the real URL which has the long url as shown in figure 7 


    Figure 7. Custom URL Setting

Conclusion

BizTalk Server enables developers to extend WCF adapter to overcome any limitations in WCF capabilities. In this post we explained one of the scenarios that needed to extend WCF to overcome the limitation of WCF Adapter and we walk-throw in the steps needed to register wcf extension in WCF-Custom Adapter.

See Also

Another important place to find an extensive amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki.