Azure Logic Apps – Custom assemblies inside XSLTs

 

Azure Logic Apps team announced an important feature on their YouTube webcast to support custom assemblies inside XSLTs. So we can go ahead and upload the dll of of the C# class library project to Azure Integration Account and we can refer the same inside the XSLTs. How cool!

Since we are having an immediate requirement on one of our projects to do a lookup during the transformation, I went ahead and gave it a try.

Please note that to utilize custom assemblies inside XSLTs, we will not be using the extension object XML to refer the dlls rather use <msxsl:assembly> to declare assemblies for use in script blocks.

What I did:

  • Created a C# Class Library project and wrote a method inside it to concatenate first name and last name and return full name.
  • Created an Integration Account project in Visual Studio 2015 and added two schemas (Input and Output), and a map to generate XSLTs.
    • Input schema elements: FirstName and LastName
    • Output schema element: FullName
    • BizTalk Map: Input Schema to Output schema
    • XSLT: Template that calls the assembly and does the transformation

I used Visual Studio 2015 for this development since Microsoft Azure Logic Apps Enterprise Integration Tools for Visual Studio 2017 is not available yet. This Tools add-on template helps generate BizTalk style (XML and flatfile) schemas and maps that can be uploaded to the Integration Account to be used inside Logic Apps for Flat file decoding/encoding, schema validation and XML transformations.

Here is the sample C# class library project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MapHelper
{
public class XSLTHelper
{
public string ConcatenateNames(string firstName, string lastName)
{
return firstName + ” ” + lastName;
}
}
}

ClassLibrary

 

Integration Account artifacts:

Project creation in Visual Studio 2015:

File –> New –> ProjectVSIntegrationAccount.PNG

 

Right click on the Project and select Add New Item to add schemas and maps:AddSchemasMaps.PNG

 

Input schema: InputSchema

 

Output schema: OutputSchema

 

Map (only to generate xslts and unit testing in Visual Studio. Map cannot be uploaded to Integration Account):Map

 

XSLT: XSLT

 

XSLT code:

<?xml version=”1.0″ encoding=”UTF-16″?>
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform&#8221;
xmlns:msxsl=”urn:schemas-microsoft-com:xslt”
xmlns:var=”http://schemas.microsoft.com/BizTalk/2003/var&#8221;
exclude-result-prefixes=”msxsl var s0″ version=”1.0″
xmlns:s0=”http://CustomAssembly.InputSchema&#8221;
xmlns:ns0=”http://CustomAssembly.OutputSchema&#8221;
xmlns:ns3=”http://schemas.microsoft.com/BizTalk/2016/ScriptNS3&#8243;
xmlns:user=”http://MyCompany/”&gt;
<xsl:output omit-xml-declaration=”yes” method=”xml” version=”1.0″ />
<xsl:template match=”/”>
<xsl:apply-templates select=”/s0:Employee” />
</xsl:template>
<xsl:template match=”/s0:Employee”>
<xsl:variable name=”firstName” select=”FirstName/text()”/>
<xsl:variable name=”lastName” select=”LastName/text()”/>
<ns0:Employee>
<FullName>
<xsl:value-of select=”user:Concatenate($firstName, $lastName)”/>
</FullName>
</ns0:Employee>
</xsl:template>
<msxsl:script language=”C#” implements-prefix=”user”>
<msxsl:assembly name=”MapHelper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=92f92a6720d1e6ec” />
<msxsl:using namespace=”MapHelper” />
public string Concatenate(string a, string b)
{
XSLTHelper xsltHelper = new XSLTHelper();
return xsltHelper.ConcatenateNames(a, b);
}
</msxsl:script>
</xsl:stylesheet>

If you are from BizTalk background, please note that we are not using Custom Extension object XML to refer the dlls inside the XSLT. When you refer the class library project dll, please add the fully qualified name of the assembly.

 

Once all done and unit tested successfully, please upload the Input schema, the XSLT and the custom assembly dll to Azure Integration Account:

AzureIntegrationAccount

 

Once done, associate the integration account to your new Logic App and refer the map to do the transformation.

IntAccLogicAppAssociation.png

The Logic App I created for this was exposed as a HTTP endpoint, and I used Postman to send the XML request payload (Input schema instance) to get the transformed XML (Output schema instance)

Logic App flow:

LogicAppFlow

 

Postman request: Upon saving the logic app, you would get the callback uri. Copy it to call it from the Postman app. Please make sure you set the Content-Type to application/xml since so the XML payload would be passed over without any issues. Postman

Hope this helps you if you are trying to utilize custom assemblies inside XSLTs to be used in the Logic Apps. However, if you are trying to port your existing XSLTs that are using Extension Object XMLs, you may not be able to port as-is. In other words, I am not sure how would you do that since there is not a way to export your Extension Object xml yet. You may want to explore Azure Functions or API Apps route. There is a nice post here for that.

Please feel free to reach out to me in case of any questions / comments.

Thanks to Logic Apps team for continuing to listen to the community and adding key features to make Logic Apps a robust Enterprise Integration Platform in the cloud.

Shri

Advertisements

4 thoughts on “Azure Logic Apps – Custom assemblies inside XSLTs

  1. Hi Shri,

    Nice post.

    Is the Logic Apps Mapper in VS 2015 updated to support custom assemblies or did you have to manually add the “msxsl:assembly” in the xslt?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s