Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, 26 October 2024

Copy Blob Between Azure Storage Accounts with C# Using DefaultAzureCredential

 Copying a blob between Azure storage account without downloading the blob locally is realy simple with azcopy command. Now using Azure.Storage.Blobs we can copy blobs between Azure sotrages without downloading the blob locally, with C# code as well. Using DefaultAzureCredential for the copy operation is useful, when we use apps with managed identities, such as workload identity in AKS. Let's explore steps necessary to copy a blob from one storage account to another with C# console app.

How it works

When the example simple console app is executed it copies blob from sourse storage account to target storage account as shown below.


Monday, 6 May 2024

FormatterServices.GetUninitializedObject is Obsolete, What can we use instead?

The FormatterServices.GetUninitializedObject is obsolete and shows a warning as shown below if we try to use it in our code. The FormatterServices class is obsolete as per documentation.


What is the alternative for this? Let's find out with an example.

Saturday, 5 November 2022

Azure Redis Connectivity Checker App

Here is a simple .NET application to test connectivity to Azure Redis Cache. The application code is available in GitHub chamindac/RedisConnectionChecker. Refer the video for more details.

Saturday, 22 October 2022

Writing a json Easily and Elegently in in C# 11

 In the previous post "No More " Escaping Needed with C# 11" we have discussed, how the json can be writen in C# code, without having to escape each occurance of double quote, when we use the new C# 11 feature Raw String Literals. You might not want to hardcode the entire json in code, instead may want to use variables dynamically setting the json body content. Let's see how we can use String interpolation, in Raw String Leiterals to write json body elegently, and have variables dynamically set values in the josn.

Saturday, 15 October 2022

No More " Escaping Needed with C# 11

Writing json body in C# code as a string variable value to pass on to a method or service, is something you may hate as you need to escape every " that you use in your json specification, even though you could use @ (verbatim string literals) to write multi-line strings.  With new Raw String Literals in c# 11, we can write " as content of the strings wihtout having to escape each instance. Let's explore bit about escaping double qutes with Raw String Literals.

Saturday, 8 October 2022

Check The C# Language Version of a Project

 C# Language version used in your project is determined by the .NET Framework you are using. We can set specific language version for a project if required for exxampe if we need to use the preview features we can add in the <LangVersion>preview</LangVersion> .csproj file <PropertyGroup>. If the language version is not specifically set and if you want to check the language versio used by your project, the following explained step can be performed.

Sunday, 2 October 2022

Getting Started with GitHub Copilot with VS Code

GitHub Copilot is the AI pair programmer who will assit us in writing code efficiently, while we are working with VS Code, Visual Studio, JetBrains IDEs or Neovim. Let's explore how to get started with GitHub copilot with VS Code and C# using a console application.

Saturday, 2 July 2022

Enable Swagger UI with an .NET 6 API having Custom Routing

 In .NET APIs using swagger for API documentation is really useful to perform API testing and to describe API. We may need to set custom API routing, specially when w deploy the APIs as contianers to Kubernetes, to enable path based routing using ingress such as Nginx. Let's look at steps of setting up custom routing and getting swagger to work with custom routing using a .NET 6 API.

Sunday, 15 May 2022

Resolve C# Connection Issue "StackExchange.Redis.RedisConnectionException: No connection is active/available to service this operation: It was not possible to connect to the redis server(s). There was an authentication failure; check that passwords (or client certificates) are configured correctly." to Azure Redis cache Connection via Private Endpoint

 Using Azure Redis cache with C# throws an error message such as below while accessing the Redis cache via a private endpoint.

StackExchange.Redis.RedisConnectionException: 
No connection is active/available to service this operation: 
GET Zdddddddddddddddddddddddddd; It was not possible to connect to the redis server(s). 
There was an authentication failure; check that passwords (or client certificates) are 
configured correctly. ConnectTimeout, mc: 1/1/0, mgr: 10 of 10 available,
 clientName: DESKTOP-XXXXX, IOCP: (Busy=1,Free=999,Min=16,Max=1000), WORKER: 
(Busy=3,Free=32764,Min=16,Max=32767), v: 2.2.88.56325
 ---> StackExchange.Redis.RedisConnectionException: It was not possible to connect to the 
redis server(s). There was an authentication failure; check that passwords 
(or client certificates) are configured correctly. ConnectTimeout
   --- End of inner exception stack trace ---

Thursday, 15 October 2020

Resolving “System.IO.IOException: The response ended prematurely.” in HttpClient Request

Some times a silly mistake can waste lot of time of a developer. The exception “System.IO.IOException: The response ended prematurely.” while making an http client request to call an API from another web app has wasted considerable amount of my time diagnosing the issue. Therefore, thought worth sharing the experience.

Thursday, 18 May 2017

string.Format & { }

C# developers often use string.Format in handling string. string.Format the { and } is used as a special character to identify the parameters.

For example:

scrollDepth = “10”

string.Format("DOWN {0}",scrollDepth)  generates a string DOWN 10.

But what if a { should be used as a value in the string. Is there a way to include { in the string?

To escape a { you can {{. So if you want to get the resultant string as “{DOWN 10}” the syntax to use is

string.Format("{{DOWN {0}}}",scrollDepth)

Thursday, 5 June 2014

VS '14' CTP - How to Use 'Roslyn'

I provisioned a VM for VS '14' CTP in Azure and started testing on new 'Roslyn' powered syntax with C#. But it did not work and gave compiler errors.



Thanks to Anthony D. Green of MSFT managed to fix the issue. Here is how to do.

Adding experimental to .csproj solved the issue.



Now my code with new syntax works like a charm.



Awesome!!! VS Rocks for Ever!!!


Popular Posts