C# .NET – Generate error message when “async void” is used in code

The mentioned AsyncFixer extension and Nuget Package are super useful when dealing with async code. They both package a Roslyn analyzer that detects many async issues and provides an automatic fix for them as well in most cases. Using the .editorconfig in Visual Studio you can configure specific warnings as errors:
[*.cs] # AsyncFixer03: Fire-and-forget async-void methods or delegates dotnet_diagnostic.AsyncFixer03.severity = error
And you can set that straight from the Solution Explorer in case you’ve a

Source: c# – Generate error message when “async void” is used in code – Stack Overflow

Fluent Assertions Vs MS Test Assertions

I appreciate what fluent assertions have got to offer. Fluent Assertions have benefits of clear error messages, more readable test code and fewer lines of code needed for writing tests as iterated from the previous paragraph. The descriptive outcomes that Fluent Assertions offers also suits TDD and BDD testing methodologies.

Source: Fluent Assertions Vs MS Test Assertions

C# .net core how to get access to content file below bin\Debug\net6.0

In a .NET Core (now called .NET) application, you can access content files that are located below the bin\Debug\net6.0 (or bin\Release\net6.0 for release builds) directory using relative paths. The bin\Debug\net6.0 directory is the output directory where the compiled application and its associated files are located during development.

Here’s how you can access content files located below this directory:

  1. Set the Build Action for Content Files: First, make sure that the content files you want to access are marked with the appropriate build action. Right-click the file in Solution Explorer, go to Properties, and set the Build Action to “Content”. This ensures that the file gets copied to the output directory during the build process.
  2. Accessing Content Files: You can access content files using the System.IO namespace to manipulate file paths and read the content. Here’s an example of how you can read the content of a text file located below the bin\Debug\net6.0 directory:
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string basePath = AppDomain.CurrentDomain.BaseDirectory;
        string contentFilePath = Path.Combine(basePath, "myfile.txt");

        if (File.Exists(contentFilePath))
        {
            string content = File.ReadAllText(contentFilePath);
            Console.WriteLine("Content of the file:");
            Console.WriteLine(content);
        }
        else
        {
            Console.WriteLine("File not found.");
        }
    }
}

In this example, AppDomain.CurrentDomain.BaseDirectory gives you the path to the directory where your executable is located (bin\Debug\net6.0), and then you use Path.Combine to form the full path to the content file you want to access.

Remember that this approach works well during development, but in a production scenario, the location of the content files might differ, so you should consider different strategies for handling content files in production deployments.

Source: Chat GPT, https://chat.openai.com

How do I store session data in Server-side Blazor?

First to access browser Session Storage in Blazor apps, write a custom code or use a third party package. The accessed data can be stored in the localStorage and sessionStorage.  The localStorage is scoped to the user’s browser. If the user reloads the page or closes and reopens the browser, the state persists. Session storage is similar to the local storage but the data in the session storage will be cleared after the session.

Source: How do I store session data in Server-side Blazor?

dnSpy – .NET debugger and assembly editor

dnSpy is a debugger and .NET assembly editor. You can use it to edit and debug assemblies even if you don’t have any source code available. Main features: Debug .NET and Unity assemblies Edit .NET and Unity assemblies Light and dark themes

Debugger

  • Debug .NET Framework, .NET and Unity game assemblies, no source code required
  • Set breakpoints and step into any assembly
  • Locals, watch, autos windows
  • Variables windows support saving variables (eg. decrypted byte arrays) to disk or view them in the hex editor (memory window)
  • Object IDs
  • Multiple processes can be debugged at the same time
  • Break on module load
  • Tracepoints and conditional breakpoints
  • Export/import breakpoints and tracepoints
  • Call stack, threads, modules, processes windows
  • Break on thrown exceptions (1st chance)
  • Variables windows support evaluating C# / Visual Basic expressions
  • Dynamic modules can be debugged (but not dynamic methods due to CLR limitations)
  • Output window logs various debugging events, and it shows timestamps by default 🙂
  • Assemblies that decrypt themselves at runtime can be debugged, dnSpy will use the in-memory image. You can also force dnSpy to always use in-memory images instead of disk files.
  • Public API, you can write an extension or use the C# Interactive window to control the debugger

Source: dnSpy/dnSpy: .NET debugger and assembly editor

.NET Core API endpoint model validation

Example of simple method in an API controller for showing invalid posted model state.

[HttpPost("create")]
public ActionResult<MyModel> Create([FromBody] MyModel model)
{
    ValidateIncomingModel();
    return myBusiness.Create(model);
}

private void ValidateIncomingModel()
{
    if (!ControllerContext.ModelState.IsValid)
    {
        var errors = ControllerContext.ModelState.Keys
              .SelectMany(key => ControllerContext.ModelState[key].Errors
              .Select(x => key + ": " + x.ErrorMessage));
        var message = string.Join("\n", errors);
        throw new Exception($"Not valid incoming model\n {message}");
    }
}

 

ClosedXML – .NET library for Excel files

ClosedXML is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.

Source: ClosedXML
License: MIT / Open source project
Doc: https://closedxml.readthedocs.io/en/latest/index.html
Wiki: https://github.com/closedxml/closedxml/wiki

ClosedXML is a wrapper of the offical .NET Open XML SDK:
https://github.com/dotnet/Open-XML-SDK

How to Deserialize JSON to C# cherry-picking a small portion of JSON data

JSON deserialization in C# refers to the process of forming up .NET objects from a JSON string. Most of the time, this means creating strongly-typed POCOs. However, there are certain situations when we may prefer flexibility over type-inference. For example, cherry-picking a small portion of JSON data, dealing with external JSON data whose structure is largely unknown or changes very often, etc. Dynamic deserialization comes into play for such cases. This does not necessarily mean the use of language’s inbuilt dynamic keyword. There are other ways as well.We are going to see how we can do this using the native System.Text.Json library and the popular Newtonsoft.Json library.

Source: How to Deserialize JSON Into Dynamic Object in C# – Code Maze

Try the new System.Text.Json source generator – .NET Blog

In .NET 6.0, we are shipping a new C# source generator to help improve the performance of applications that use System.Text.Json. In this post, I’ll go over why we built it, how it works, and what benefits you can experience in your application.

Source: Try the new System.Text.Json source generator – .NET Blog

Task Async handling cancellation – Cancellation in Managed Threads | Microsoft Docs

How to handle cancellation of x number of running background tasks.

Source: Cancellation in Managed Threads | Microsoft Docs
Code examples: https://docs.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads#code-example