Posts about .NET

Keyed dependency injection services in .NET 8

November 3rd 2023 .NET Dependency Injection

The dependency injection framework that is built into the modern .NET works well enough to satisfy the needs of most projects. However, it's not as feature-rich as the most popular 3rd party libraries. One of the features that was missing before .NET 8 was the ability to register multiple implementations of the same interface and have the ability to specify which one should be injected.

Exclude setup from verifying in Moq

September 29th 2023 Moq Unit Testing .NET

Mocking frameworks can be used not only to mock methods, but also to verify whether those mocked methods have actually been called. The latter is sometimes called interaction testing and is particularly useful when you want to test that specific calls to an external API have been called. Of course, Moq also supports such setup verification.

Test DI service registration

September 22nd 2023 Dependency Injection Unit Testing .NET

In large projects, service registration code for dependency injection can grow rather large. Forgetting to register a newly added dependency will break your application at the point of instantiating a class (transitively) requiring it. It's even more likely that this will happen to you if you need to register a dependency in multiple places (e.g., for multiple projects) and don't have tests for every one of them.

BinaryFormatter serialization disabled in .NET 5

August 25th 2023 .NET

During the upgrade of an ASP.NET Core Web API application from .NET Core 3.1 to .NET 6 I stumbled across a breaking change that only manifested itself at run time despite a pretty good test coverage.

Codespaces config for .NET Angular project

After creating a working dev container configuration for an ASP.NET Core with Angular project, I wanted to also try it out with GitHub Codespaces. I had to do additional changes to the configuration to get it working.

Updating Azure Functions runtime

August 11th 2023 Azure .NET

I was recently involved in updating a bunch of .NET projects deployed to Azure from older versions to .NET 6. Among them, there was also an Azure Function app. To my surprise, unlike Azure Web App services, not everything could be reconfigured through Azure Portal.

Dev container config for .NET Angular project

For simple projects with a single technology stack, you're likely to find a dev container template preconfigured with everything you need. That's what happened to me when I created my first dev container. For larger projects with more than one technology stack, more configuration will be needed.

Manually deploying a .NET AWS Lambda function

July 21st 2023 .NET AWS

I couldn't find a complete guide for deploying an AWS Lambda function in .NET from a local machine. I'm publishing my notes on the subject for future reference and anyone else interested.

Priority of JSON converter attributes

July 14th 2023 .NET Serialization

After I learned about JsonStringEnumMemberConverter, I wanted to use it for an enum in a large project I was working with, but to my surprise it didn't seem to have any effect on the serialization.

Norwegian language code change

July 7th 2023 .NET

As I was recently updating an old .NET Core 3.1 project to .NET 6, I encountered an unexpected breaking change. I couldn't find any documentation about it, even after I already knew about it. Fortunately, I noticed it before deploying the application to production thanks to the test coverage.

Custom enum value name in System.Text.Json

June 23rd 2023 .NET Serialization

When serializing enums to JSON, it's usually better to serialize them as descriptive strings than incomprehensible magic numbers. Most of the time, it's good enough to simply use the names of enum members for this purpose. However, these must follow the rules for identifier names in C#, so they might be too restrictive if you need to use specific string values because of interoperability with other systems.

Minimum MSTest versions for VS 17.6

After I updated my copy of Visual Studio 2022 to the latest version 17.6, I couldn't run the tests from the Test Explorer anymore that worked just fine in version 17.5. I had similar experience in the past, so I immediately suspected that the problems were related to the versions of test-related NuGet packages in my project.

Deployment directory change in MSTest v3

June 9th 2023 .NET Unit Testing

When I updated MSTest from v2 to v3 in one of my projects, some tests started failing. It was because of a breaking change in MS Test V3 that caused the TestContext.DeploymentDirectory property to return a different path.

Troubleshooting a XAML error in WPF

September 23rd 2022 XAML WPF .NET

I recently helped troubleshoot a WPF application that was causing an unhandled exception. I thought the process of identifying and fixing the problem might be useful to others and decided to describe it in this post.

Type issues with data driven tests in MSTest

September 2nd 2022 .NET Unit Testing

Data-driven tests are great for repeating the same unit tests with many different inputs. However, a test from a project I worked on failed on multiple test cases because a double value was incorrectly handled as a decimal.

Vulnerabilities in NuGet packages

July 1st 2022 NuGet .NET

The recently disclosed vulnerability in Newtonsoft.Json prompted me to take a closer look at the tools available in the .NET ecosystem for identifying referenced packages with known vulnerabilities.

Testing protected methods

June 24th 2022 Unit Testing .NET

In most cases, you do not want to write tests for non-public methods, because that would make the tests dependent on the internal implementation of a class. But there are always exceptions.

Ignored failing assertions in mock callbacks

June 17th 2022 Moq Unit Testing .NET

When you write unit tests, make sure not only that they succeed if the tested code works as expected, but also that they fail if the code does not work as expected. Otherwise, these tests will give you a false sense of confidence in your code.

Code coverage in .NET 6

April 29th 2022 Unit Testing .NET

Since .NET 5, the coverlet.collector NuGet package is pre-installed in the test project templates, which can generate code coverage reports when the tests are executed. Let us take a look at how you can use this in your code editor.

Data driven tests by convention

April 22nd 2022 Unit Testing .NET

It pays in the long run to learn about the various capabilities of unit testing frameworks and use them to make unit testing code more maintainable. Let us go through the process of refactoring a set of copy-pasted tests into a single parameterized, i.e. data-driven test.

Using async void in xUnit tests

April 15th 2022 Unit Testing .NET

When I recently needed to update some existing unit tests, I noticed that many asynchronous tests were using async void in their signature. My first instinct was to fix them by using async Task instead, because that surely meant they were broken and would not detect failures correctly. But before I did that, I experimented a bit, and as far as I could tell, the tests worked as expected, correctly detecting failed assertions and unexpected exceptions. I decided to do some more research on the subject.

Build flags in Visual Studio

March 11th 2022 Visual Studio .NET

The project properties window has been redesigned in Visual Studio 2022. But not only the appearance has changed. For at least some options, the effect of the changes in the project properties window has also changed. Let us take a look at how the behavior of the Allow unsafe code build option has changed.

Generic host based console application

March 4th 2022 .NET Dependency Injection

The new console application template in .NET 6 is as minimal as possible thanks to top-level statements. That's great for simple applications, but what if you want to create a large console application with a sophisticated command-line interface?

Throwing null argument exceptions

December 24th 2021 C# .NET

NullReferenceException is probably one of the most common exceptions in .NET. It is thrown whenever you try to access a member of a reference type that is currently set to the value null. With the nullable reference types introduced in C# 8, the compiler can now warn you if you try to do this, so you can add a null check to prevent the exception from being thrown. Null checking code is simple, but very repetitive and potentially error-prone.

Programmatic web page login with cookies

November 12th 2021 .NET

.NET makes it really easy to scrape some data from a public website. You can use HttpClient to download the web page. The best library for parsing the HTML is probably AngleSharp, but that's not the topic of this post. Instead, I'll focus on what to do if the web page is not public and you need to log in first. Typically, you will then need to submit the login form programmatically and use the cookies from the response in future requests.

How can a POST request become a GET?

August 13th 2021 HTTP .NET

Recently I solved a problem with a POST request that failed with a 405 error (Method Not Allowed), even though there was only a POST endpoint at the requested URL. After solving the problem, I found it interesting enough to reproduce it and wrote a post about it.

What code are your unit tests testing?

April 30th 2021 Unit Testing .NET EF Core

Mocks can be a helpful tool for replacing external dependencies in unit tests. However, caution is required when you embark on that route or you could end up with tests that don't really test your code under test.

Configuring environments in .NET console app

March 5th 2021 .NET

ASP.NET Core applications are already set up with support for reading configuration files and switching between different environments. You get none of that when you create a new .NET console application. Fortunately, you can still take advantage of the same NuGet packages add similar support with minimum amount of setup code.

Polymorphism with New JSON Serialization

September 25th 2020 .NET Serialization

With the new System.Text.Json built into .NET Core, JSON serialization can now be performed without the Json.NET library. However, there are differences between the two libraries once you go beyond the basics. For example, support for serializing and deserializing polymorphic objects is limited in the new library.

Compiling and Executing Code in a C# App

August 2nd 2019 Roslyn .NET .NET Framework

Since the release of Roslyn, the complete C# compiler pipeline is available as a NuGet package and we can include it in our own application. I was wondering how difficult it would be to use it to compile some C# source code into an executable and run it.

Slides and Code from NT Conference 2019

Yesterday the NT Conference 2019 concluded in Portoro┼ż. This year I had three sessions there. On Tuesday, I talked about the new language features in C# 8. On Wednesday, I repeated my session about application architecture from this year's Global Azure Bootcamp. My final session on Thursday was about global tools in .NET Core.

My Book About C# and .NET Is Available

February 1st 2019 Book C# .NET Framework .NET

Since this week, The Absolutely Awesome Book on C# and .NET is finally available for order in its final form: in all eBook formats and with the accompanying source code.

No Support for Tuples in Expression Trees

December 7th 2018 C# .NET .NET Framework EF Core

Tuples, as added to C# 7, can be a nice alternative to anonymous types in LINQ when you only want to return a subset of values from the queried type. Before tuples, this was only possible by creating an anonymous type in the Select method. Now you can create a tuple instead. However, if you try to do that with EF Core, the code won't compile. How come?

Cyclomatic Complexity in .NET Core

Cyclomatic complexity is a simple code metric, commonly used to identify complex methods which are difficult to maintain and therefore good candidates for refactoring. It is one of the five code metrics built into Visual Studio 2017, but it isn't available for .NET Core and .NET Standard projects. Let's look at third party extensions which you can use instead.

My Sessions at NT Conference 2018

May 25th 2018 C# Unit Testing .NET Speaking

It's the week of NT Conference 2018. I had two sessions this year. On Tuesday, I talked about a selection of common C# gotchas which can surprise even an experienced C# developer. In my second session, I explained the benefits of continuous testing and showed how to configure it for a .NET Core project in Visual Studio 2017 and Visual Studio Code.

Resources from Global Azure Bootcamp 2018

It's April again and last Saturday it was time for the annual Global Azure Bootcamp event. The Slovenian one was taking place at the local Microsoft offices. In my session, I explained how to configure Visual Studio Code to improve the experience of .NET Core development as much as possible.

Slides and Code from My SLODUG Session

June 30th 2017 .NET .NET Standard Speaking

The last Slovenian Developers User Group meeting before the summer break consisted of the two most popular sessions from Microsoft NT Conference, as voted by the user group members.

Slides and Code from My NT Conference Session

May 19th 2017 .NET .NET Standard Speaking

At the beginning of this week the traditional NT Conference was taking place in Portoro┼ż. I had my only session this year on Monday, where I talked about .NET Core and .NET Standard.

Slides and Code from My Sessions at Cancel Conference

September 17th 2016 Async C# .NET Speaking

This week the second community organized Cancel conference was taking place in Ljubljana. It spanned over two days. Thursday was the main conference day with 20 sessions grouped in 4 tracks. On Wednesday afternoon preconf was organized at the premises of Microsoft Slovenia. I had my sessions on both days.