Monday, January 30, 2023

C# 11.0: Newlines in String Interpolation Expressions

In this post let's have a look at another feature that is available with C# 11.0. And that is support for Newlines in String Interpolation Expressions.

Consider the following example code written in C# 10.0.
int age = 60;
string ageCategory = age switch
{
< 1 => "Infant",
< 12 => "Child",
< 17 => "Adolescent",
< 65 => "Adult",
_ => "Older adult"
};
string message = $"Based on the Age of {age}, you are a(n) {ageCategory}.";
Here it would have been nice if I can include the logic to determine the age category within the interpolated string. But Newlines inside a non-verbatim interpolated string are not supported in C# 10.0.

With C# 11.0, we now have the support for Newlines in String Interpolation Expressions. So I can simplify the code as follows.
int age = 60;
string message = $"Based on the Age of {age}, you are a(n) {age switch
{
< 1 => "Infant",
< 12 => "Child",
< 17 => "Adolescent",
< 65 => "Adult",
_ => "Older adult"
}}.";
Isn't it nice? There is no pleasure like having readable simplified code.

Read more of C# features here.
   What's new in C# 11

Happy Coding.

Regards,
Jaliya

Wednesday, January 25, 2023

Visual Studio 2022: Sending HTTP Requests with .http Files

Do you know Visual Studio has this nice feature to send HTTP Requests using .http files. It's actually pretty neat.

We just have to create a file with .http extension.
.http Files in Action
We can do all the methods and the output is displaying nicely.

Disclaimer: I am not sure when this feature got added, I am on Microsoft Visual Studio Enterprise 2022 (64-bit) - Version 17.5.0 Preview 4.0 and I don't have a specific extension installed in order to enable this functionality.

Hope this helps!

Happy Coding.

Regards,
Jaliya

Wednesday, January 18, 2023

Visual Studio 2022 Version 17.5 Preview 2: Text Visualizer Improvements

In this post let's explore some nice improvements that got added to Text Visualizer in Visual Studio. This got shipped with Visual Studio 2022 Version 17.5 Preview 2

I actually found this accidentally and it's so handy!

Say I have a string variable that holds a JWT token and I want to decode it for some debugging purpose. Usually what I would do is copy-pasting the token into https://jwt.io or https://jwt.ms or some external JWT decoding tool. What if we can see the decoded token without leaving Visual Studio? And that's exactly what the new Text Visualizer brings to the table.

Say I want to see the decoded token of the following.
Debugging a string
I need to click on the View button. And then I am presented with the following.
Text Visualizer
I can just select JWT Decode and just like that I can see the decoded token. 

Along that, you can see there are more options to manipulate the string such as Base64 Encode & DecodeUrl Encode & Decode etc.

Isn't that great? I just love this. Remember, if you want to try this out, you need to install the latest Visual Studio 2022 Preview.

Hope this helps.

Happy Coding.

Regards,
Jaliya

Tuesday, January 10, 2023

Azure Durable Functions in Azure Functions .NET Isolated Worker Process

Azure Durable Functions now supports .NET 7.0 running in the Isolated worker process. It's still in it's preview stage, but it's super exciting.

In this post, let's see how we can get ourselves started in Azure Durable Functions in Azure Functions .NET Isolated Worker Process.

First, after creating a Function App that targets .NET 7 Isolated functions worker, we need to install the NuGet package: Microsoft.Azure.Functions.Worker.Extensions.DurableTask. Make sure you have checked Included prerelease checkbox if you are trying to install the package through Visual Studio NuGet Package Manager.

Microsoft.Azure.Functions.Worker.Extensions.DurableTask
After installing the package, I have updated Function1.cs as follows adding a simple Durable Function.
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.DurableTask;
using Microsoft.Extensions.Logging;
 
namespace FunctionApp1;
 
public class Function1
{
    [Function(nameof(TriggerHello))]
    public async Task<HttpResponseData> TriggerHello([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
        [DurableClient] DurableClientContext durableContext,
        FunctionContext executionContext)
    {
        ILogger logger = executionContext.GetLogger(nameof(Function1));
 
        string instanceId = await durableContext.Client.ScheduleNewOrchestrationInstanceAsync(nameof(HelloPersonOrchestrator));
        logger.LogInformation("Created new orchestration with instance ID = {instanceId}", instanceId);
 
        return durableContext.CreateCheckStatusResponse(req, instanceId);
    }
 
    [Function(nameof(HelloPersonOrchestrator))]
    public static async Task<stringHelloPersonOrchestrator([OrchestrationTrigger] TaskOrchestrationContext context,
        FunctionContext executionContext)
    {
        ILogger logger = executionContext.GetLogger(nameof(HelloPersonOrchestrator));
 
        string result = await context.CallActivityAsync<string>(nameof(SayHello), "John Doe") + " ";
        result += await context.CallActivityAsync<string>(nameof(SayHello), "Jane Doe") + " ";
        result += await context.CallActivityAsync<string>(nameof(SayHello), "Joe Bloggs") + " ";
        result += await context.CallActivityAsync<string>(nameof(SayHello), "Fred Bloggs");
 
        logger.LogInformation("HelloPersonOrchestrator says: {output}", result);
 
        return result;
    }
 
    [Function(nameof(SayHello))]
    public static string SayHello([ActivityTrigger] string name, FunctionContext executionContext)
    {
        ILogger logger = executionContext.GetLogger(nameof(SayHello));
        logger.LogInformation("Saying hello to {name}", name);
        return $"Hello {name}!";
    }
}
And then we can call the HTTP  trigger to start the orchestration. And it works like a charm.
Output
Can't wait for this to go under GA.

Do try this out and if you see any issues, please do not hesitate to log an issue here: microsoft/durabletask-dotnet

Hope this helps.

Happy Coding.

Regards,
Jaliya