Passing Complex Objects to .NET Framework Custom Code in Azure Logic Apps (Standard)

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Community Hub.

I recently was asked the question; how do I pass in a complex object to custom code? In this post, we are going to explore this topic.

 

When you create a new local function, we will provision sample code that you can use to get started. Within this sample code, we include primitive types as inputs to the function and then return a complex Weather object. When the function returns context back to the workflow, we have access to all the properties from that complex object in our workflow as dynamic content.

 

KentWeareMSFT_0-1701802342699.png

 

So the question, is what if I want to pass a complex object into my function? Do I have to perform any type casting? The answer is no.

I have updated the sample to include a complex object called WeatherRequest. I can now access any properties in this complex object by referencing objectname.property.

 

[FunctionName("GetWeather")] public Task<Weather> Run([WorkflowActionTrigger] WeatherRequest weatherRequest) { this.logger.LogInformation("Starting GetWeather with Zip Code: " + weatherRequest.ZipCode + " and Scale: " + weatherRequest.TemperatureScale); // Generate random temperature within a range based on the temperature scale Random rnd = new Random(); var currentTemp = weatherRequest.TemperatureScale == "Celsius" ? rnd.Next(1, 30) : rnd.Next(40, 90); var lowTemp = currentTemp - 10; var highTemp = currentTemp + 10; // Create a Weather object with the temperature information var weather = new Weather() { ZipCode = weatherRequest.ZipCode, CurrentWeather = $"The current weather is {currentTemp} {weatherRequest.TemperatureScale}", DayLow = $"The low for the day is {lowTemp} {weatherRequest.TemperatureScale}", DayHigh = $"The high for the day is {highTemp} {weatherRequest.TemperatureScale}" }; return Task.FromResult(weather); }

 

 

WeatherRequest class

 

public class WeatherRequest { public int ZipCode { get; set; } public string TemperatureScale { get; set; } }

 

 

I can now compile this code and the assembly and dependencies will be bin placed into the workflow project. I will now update my workflow to pass in a JSON object that matches the format of my complex object. Using the json2csharp website can help with converting JSON objects into C# code.

 

In my workflow, I will add a Compose action that I can use to create a JSON object.

KentWeareMSFT_1-1701802795676.png

 

I can then pass my Compose action into the WeatherRequest input parameter.

KentWeareMSFT_2-1701802819940.png

 

Our code will now execute and return a response back to our workflow.

 

Video Content

If you would like to see a live demo of this in action, please refer to the following video.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.