Logic Apps -Working with Cache and Enterprise Messaging in Logic Apps
Introduction ** **
Logic App is Ipaas offering from Microsoft to run workflow and integration layer in cloud (Microsoft azure). With digital transformation and evolution of technology in terms of multiple PaaS and SaaS offerings, organization requires a robust Integration layer to extent its technical capability.
On design principles Logic App makes a good choice to avoid point to point integration (in cloud or on premise) whether systems are running on cloud or on premise. A better integration story is the key enabler for the business transformation. The Architecture should be designed by keeping an eye on tomorrow.
In this article, we will go through the process of implementing caching in Logic Apps with help of Azure functions. Azure function provides server less computing environment in the cloud. With Azure functions, you can run any piece of code in language of choice and pay based on the consumption. With Azure function, you get rid of provisioning and maintaining servers
Solution Design:
In this solution, we are developing interface for fictions company Millennium Car Service which is sending purchase Order in FLAT FILE to motor parts inventory company.Motor Parts inventory company has exposed a WebAPI to get the order request from the third parties and process them as received. All the purchase Order on the same day should have constant guid associated along with the purchase details. In this sample, we will be using azure function to store the GUID in cache and use the cached value within the logic App workflow.
The workflow action being listed below
- Millennium car service send the purchase Order to the blob container in CSV format through web Application
- Logic App polls the blob storage container in each 5 minute to look for a new purchase order from Millennium car service.
- Once purchase Order is received Logic Apps converts the CSV into xml using Flat File decode component available with Logic Apps and integration account.
- After converting the flat file into xml, the Logic Apps generates the GUID and calls the Azure function to store the generated Id in cache.
- The GUID is stored in cache for 24 hours and it get recycled with new guid after elapse of time
- Logic Apps submit the purchase Order along with the GUID to the HTTP endpoint.
Prerequisite
To work with the scenario, you need to have a valid subscription to Microsoft Azure.
You can register for free subscription at link: https://azure.microsoft.com/en-us/free/
Azure Blob Storage and Container
Azure Blob storage can store any type of unstructured data. You can store a variety of data like Image, documents,.txt etc. Reading and writing data to blob storage is being nicely described in Microsoft Documentation https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-how-to-use-blobs
For this tutorial, we will create blob storage in Azure Portal by following below steps:
- Login into Azure portal https://portal.azure.com/ with your valid MSDN subscription.
- Click on Add -> New -> Storage ->Storage Account
- On Create storage blade type in the storage name in lowercase and select account kind to blob storage and select the resource group in with you want to run your blob storage. Click on OK to create a new instance of blob storage under your resource group.
- Navigate to the newly created blob storage within the resource group and click container to create a new container within the blob storage. Enter the container name in lowercase and select type to from the list of a container, private or blob. Once details are entered successfully click on create button on the downside
- Copy the storage details like blob storage name and primary Key for the which will be used within the Logic app for API connection. Verify the storage account once created successfully
Azure Functions App and Functions
Introduction section describe Azure function to provide a server less environment to execute the custom code in cloud. This section will walk through the necessary steps to create Azure Function App and functions within function app which will be used with the Logic Apps.
- Login into Azure portal https://portal.azure.com/ with your valid MSDN subscription.
- Click on Add and in Search Box type function App.
- From the Filter result select Function App and click on Create.
- Enter the function app details like function app name, select subscription, resource group and Click on Create to create a new instance of function app.
- Within the Function App create select language C# and create two webhook function GetCachedKey function and SetCacheKey Function
- Create a bin directory within the function and copy System.Runtime.Caching.dll into the function App through Kuddu control by clicking the function App settings
- Start by creating the folder by either clicking the large plus-sign at the top, or by typing the following into the console MKDIR bin
- Then copy the caching DLL from the Windows directory on the machine by typing/pasting this string:
copy D:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.Caching.dll d:\Home\bin
- Copy the code listed below within **GetCaheKeyfunction **
#r "Newtonsoft.Json"
#r "System.Runtime.Caching.dll"
using System;
using System.Net;
using Newtonsoft.Json;
using System.Runtime.Caching;
public static async Task<string> Run(HttpRequestMessage req, TraceWriter log)
{
string inputData = await req.Content.ReadAsStringAsync();
string tempmessage =ExtractjsonMsg(inputData);
log.Info(tempmessage);
string tempmessage1 =GetCacheValue(tempmessage);
log.Info(tempmessage1);
return tempmessage1;
}
public static string GetCacheValue(string CacheK)
{
var cache = MemoryCache.Default;
if (cache.Contains(CacheK))
{
string val= cache.Get(CacheK).ToString();
return val;
}
else
{
return "key not found";
}
}
public static string ExtractjsonMsg(string reqContent)
{
string temp = reqContent.Replace("{\"InputData\":\"", string.Empty);
string temp1 = temp.Replace("\\\"", "\"");
return temp1.Remove(temp1.Length - 2, 2);
}
- Like the previous function create another function “SaveCacheKeyFunction”to save the Guid to the cache. Add bin folder and copy the caching dll to the bin folder and Copy the code listed below within SetCaheKeyfunction
#r "Newtonsoft.Json"
#r "System.Runtime.Caching.dll"
using System;
using System.Net;
using Newtonsoft.Json;
using System.Runtime.Caching;
public static async Task<string> Run(HttpRequestMessage req, TraceWriter log)
{
string inputData = await req.Content.ReadAsStringAsync();
string tempmessage =ExtractMsg(inputData);
string tempmessage1 =SaveCacheValue(tempmessage);
if (tempmessage1 == string.Empty || tempmessage1 == "")
{
return string.Empty;
}
else
{
return tempmessage1;
}
}
public static string SaveCacheValue(string token)
{
string CacheKey = "GuidValue";
ObjectCache cache = MemoryCache.Default;
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(24.0);
cache.Add(CacheKey, token, cacheItemPolicy);
return token;
}
public static string ExtractMsg(string reqContent)
{
string temp = reqContent.Replace("{\"InputData\":\"", string.Empty);
string temp1 = temp.Replace("\\\"", "\"");
return temp1.Remove(temp1.Length - 2, 2);
}
Integration Account
Integration account is used to store Schema, Maps, certificate parties and Agreement. Integration account is key enabler to work with Enterprise messaging system. To know more about integration account look at MSDN documentation at https://docs.microsoft.com/en-us/azure/app-service-logic/app-service-logic-enterprise-integration-accounts
For this solution create Integration account named “technetwiki”to store Flat File Schema and any mapper if required follow the steps listed below documented to create a Integration account https://docs.microsoft.com/en-us/azure/app-service-logic/app-service-logic-enterprise-integration-accounts
The sample Purchase Order Flat File Looks from the Millennium car service consists of Motor parts, number of parts required and the location where parts need to be delivered. sample request shown below
Bearings,12,auckland
Bumper,05,wellington
Radiator,06,christchurch
Radiator,04,wellington
To convert the FLAT FILE into corresponding XML format we need to use FLAT FILE schema wizard shipped with Enterprise Integration Pack available for download at https://www.microsoft.com/en-us/download/details.aspx?id=53016 .If you have valid BizTalk template installed on your machine you can also work with FLAT FILE schema generation wizard to generate corresponding XSD from the sample the Flat file shared .
- Add the generated Flat file Schema to the Integration account created in the previous section. Populate the schema name to PurchaseOrderFF. Click on OK to complete the process of schema upload
- Click on the overview section of the Integration account to verify the schema , maps or certificates are installed correctly.
Logic App workflow for Caching and Enterprise Messaging
The next step is to create workflow in the Logic App to implement caching and use of flat file decode component to translate flat files in xml format . In this Logic App we will be only working with xml as content type . You can use @json function to convert the xml payload into json format at any position in workflow.
Step by Step process to Create the Logic App workflow:
- In Azure Portal Add New Logic App and populate the required Field like name, resource group, Location
- From the List of the template within the Logic App, select Blank Logic App template to create the Logic App from scratch.
- Go to Logic App design view and Add recurrence trigger and set the polling interval to 5 min. Recurrence trigger starts a new instance of the logic app after the specified interval and saves the Logic App.
- Within the Logic App blade select and save the Integration account “technetwiki” in which schema purchase order Flat file schema is uploaded. This is required for the Flat file to XML conversion within the Logic Apps.
[
](resources/1643.17.png)
- Within the Microsoft Managed connector select flat file decode and on schema type PurchaseOrderFF.xsd as schema name and content of blob as content.
- Select GetCacheKeyFunction from function app and pass a JSON content in name value pair to search for cached value. Add a condition action to a value returned from the GetCacheKeyFunction.
- Key will not be present in first run as cache will be empty, call to GetCacheKeyFunction will return “Key not found” response and Logic App will fire another function call to SetCacheKeyFunction function within the Azure function app. This function will save the guid in cache to be used in further run ,The statement under if condition will be executed in first run .
- Next within the Logic App workflow we have foreach action which will iterate through each of the Order xml node and host it to the HTTP endpoint along with the constant guid as header for a day transaction . The Logic App expression Xpath function is used to iterate through the xml node . To learn more about Logic App expression language refer MSDN link /en-us/rest/api/logic/definition-language
- The First Logic App run will go through the true part of condition statement were token is being saved in the memory cache . The run detail is shown below
- The second run will take the header value from the cache and is being passed through the HTTP post . The second run flow is shown in below .You can verify that the second run has shipped the Settoken function app call as it has got the ket from the cache which is not null.
- In this sample, request bin is being used to post the message. With request bin we can easily verify the message which is being send the the http endpoint . As this is de-batching case each logic app run will post multiple purchase order based on the FLAT FILE record .
See Also
An important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki. Another important place to find Logic App related articles is the TechNet Wiki itself. The best entry point is Logic App Resources on the TechNet Wiki.
·