Suraj Sahoo
Suraj Sahoo

Reputation: 173

Transform XML in Azure Logic Apps, upload C# Assembly (.DLL) file to Azure Integration Account and use it inside XSLT (map)

In Log Apps, I have a Source.xml file, which is required to be transformed into another destination.xml using XSLT. Within XSLT, I need to implement some custom logic using C# code which will be uploaded to Azure Integration Account "Assemblies Section"

I have created one C# assembly file with StrongName but need to understand how the methods of C# assembly can be called from XSLT.

Would appreciate if anyone can share a working sample code with a very basic example.

Upvotes: 3

Views: 1117

Answers (2)

Hury Shen
Hury Shen

Reputation: 15724

I found an example for your reference:

The c# code show as below:

using System;
namespace ExtAssembly
{
 public static class CalcFunctions
 {
 public static Int64 Add(Int64 a, Int64 b)
 {
 return a + b;
 }
 public static Int64 Subtract(Int64 a, Int64 b)
 {
 return a - b;
 }
 public static Int64 Multiply(Int64 a, Int64 b)
 {
 return a * b;
 }
 public static Double Divide(Int64 a, Int64 b)
 {
 return a / b;
 }
 }
}

And we can use the c# code in xslt as below:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform Jump " xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl userCSharp" version="1.0" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp Jump ">
 <xsl:output omit-xml-declaration="yes" media-type="application/text" method="text" version="1.0" />
 <xsl:param name="MethodName" />
 <xsl:param name="Parameters" />
 <xsl:template match="/">
 <xsl:value-of select ="userCSharp:Invoke($MethodName, substring-before(substring-after($Parameters, '('), ')'))" />
 </xsl:template>
 <msxsl:script language="C#" implements-prefix="userCSharp"> 
 <msxsl:assembly name="ExtAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" />
 <msxsl:using namespace="System.Reflection" />
 <msxsl:using namespace="System.Text.RegularExpressions" />
 <msxsl:using namespace="ExtAssembly" />
 <![CDATA[
 public object Invoke(string methodName, string methodParameters)
 {
 MatchCollection matches = new Regex("((?<=\")[^\"]*(?=\"(,|$)+)|(?<=,|^)[^,\"]*(?=,|$))").Matches(methodParameters); 
 ParameterInfo[] pars = typeof(CalcFunctions).GetMethod(methodName).GetParameters();
 object[] methodPars = new object[pars.Length]; 
 for (int i = 0; i < pars.Length; i++)
 {
 methodPars[i] = Convert.ChangeType(matches[i].Value, pars[i].ParameterType);
 }
 return typeof(CalcFunctions).GetMethod(methodName).Invoke(null, methodPars);
 }
 ]]>
</msxsl:script>
</xsl:stylesheet>

After that, we can call it in our logic app(provide it wih "MethodName" and "Parameters" in "Transform xml" action).

For further information, you can refer to this example.

Hope it would be helpful to your problem.

Upvotes: 1

Varun05
Varun05

Reputation: 387

Question is about how to use c# assembly with XSLT. Both XSLT AND C# are being placed inside Azure Integration Account i.e in Maps and Assembly artifact sections respectively

Upvotes: 0

Related Questions