X

.NET

Global usings in C# 10

One of the new features in .NET 6 is support for global using declarations. We can define using declarations as global and they work all over the project so we don’t have to define them in other files anymore. Here’s how it works.

Cost of exceptions

Hopefully the era of leprosy and corona is over for this time and it’s time to get back to blogging. Exceptions are powerful feature of object-oriented languages as far as they are used like they are thought to use – throw exception only when something really unexpected happens. This advice should be taken seriously – here’s why.

Top-level programs in C# 9.0

C# 9.0 comes with nice new feature called top-level programs. It’s something that teachers of beginner classes will love for sure. Imagine – you start teaching C# with only two lines of code on screen. All missing code is generated by compiler. This blog post introduces top-level programs and shows some secrets of new C# compiler.

Logging to Notepad window from ASP.NET Core

Something funny to end this week. When checking my Twitter feeds I found a kinky tweet about logging .NET Core messages to Notepad window. Yes, you heard right – .NET Core logger for Notepad. It’s not real, you want to say, but it is. And here’s how it works :)

Xamarin Forms turns to .NET MAUI – single project and code-base dev experience

Yesterday Microsoft announced .NET Multi-platform App UI (MAUI) – the effort to turn Xamarin Forms apps use single cross-platform code-base targeting multiple platforms. Demos from Build conference yesterday gave clear signal – it’s not just an experiment but real deal. They really had single project running on Windows desktop and iPhone. This blog post is short overview of what’s coming.

Using data from MSSQL linked server with EF Core

My previous blog post Querying MySQL from SQL Server using linked server introduced how to link MySQL database to MSSQL and how to write queries that gather data from local and linked server. This blog post demonstrates how to get data mixed from those sources to Entity Framework Core.

Implementing repository querying interface in EF Core DbContext

My last bold statement was that we don’t need custom unit of work and repository classes with Entity Framework Core. One issue remained unsolved and it was querying part of repositories (yeah, those custom querying methods). After some experiments to get querying interface of repositories to DbContext I worked out something that I kind of like. I managed to make DbContext to work like classic unit of work that hosts repository instances. Here’s my experiment and the solution I worked out.

Behind the compiler: 20 examples of C# code before and after compiling

Over years I have written many blog posts about C# and .NET that demonstrate also how things work internally and what C# compiler produces from the code we write. I have called these chapters usually as “Behind the compiler”. This post is growing list of my writings covering interesting findings about C# compiler work.

Execute raw SQL commands in Entity Framework Core

I have never seen a real-life project where object-relational mapper generates 100% of needed SQL. There have always been those special cases when custom SQL commands are needed. In this post I will demonstrate how to run custom SQL commands in Entity Framework Commands and how to read data from database without DbSet and query types.

Using Windows Forms DataGridView in .NET Core 3.1

Windows Forms is coming to .NET Core and last version of Visual Studio 2019 Preview comes with form designer built in. There are basic controls available in toolbox but not all out-of-box controls have made their way there. One of these is DataGridView. Although not shown in toolbox, DataGridView still works like expected. Here’s how I made it work.

No need for repositories and unit of work with Entity Framework Core

After working again on codebase where Entity Framework Core was used through repository and unit of work patterns I decided to write eye-opener post for next (and maybe even current and previous) generations about what Entity Framework has to offer in the light of these to patterns. In many cases we don’t have to move away from database context approach but stick with it even more than we first planned. Here’s how many useless repositories and units of work born and here’s also how to avoid these and go with implementations offered by Entity Framework Core.

Generalize file access for ASP.NET Core applications using IFileClient implementations

Once upon a time I worked out simple file client generalization so my applications can keep files on local machine or somewhere on the cloud without changes in application code. This blog post shows how to generalize file access in web applications and provides implementations for local and cloud file clients.

All about IDisposable

So, what is IDisposable and what is disposing of objects? This is something I teach to developers classes before they are going to their first school practice at some company. One of important concepts is disposing. Here’s my story about it.

How many .NET Core SDK-s you REALLY have?

Do you have lightweight laptop with not very big laptop where you run Visual Studio? Do you need sometimes more space? You can make some free space if you remove .NET Core versions installed over time by updates and previews. Here’s how to do it.

Using Roslyn to build object to object mapper

Back in time I wrote series of posts about how I built simple object to object mapper. It was nine years ago and from them more things have changed. Today I added one new implementation of mapper and this one is using Roslyn compiler services to generate dynamic code for mappings.

Reading Windows and Linux memory metrics with .NET Core

Until .NET Core gets its own cross-platform system metrics support I needed something simple to get system memory metrics of servers where ASP.NET Core application is running. I wasn’t able to find a nice solution but I still worked out something to get system memory metrics with .NET Core on Windows and Linux.

Self-contained executable with .NET Core 3.0 on Windows, Linux and Raspberry

.NET Core 3.0 comes with support for self-contained executables. It means we can publish applications as a single executable for specified platform. Also trimming – removing of unused code from assemblies – is supported. This blog post demonstrates how to build self-contained executables using .NET Core.

Dependency Injection in Azure Functions

Azure Functions V2 supports ASP.NET Core like dependency injection. It is specially good if we write functions that are wrappers for shared libraries and components we are also using in web and other applications of our solution. This blog post shows how to use dependency injection in Azure Functions.

HttpClient: How to remove charset from Content-Type header

I was writing client library for one online service and faced situation where I had to remove charset definition from Content-Type header. It was like content type is application/json or response is 415 “Unsupported media type”. I was using HttpClient class to communicate with service and without additional efforts charset doesn’t go away. Here is how I got charset definition away from Content-Type header.

Serializing objects to URL encoded form data

While messing with dictionaries to create form data for FormUrlEncodedContent so I can send data to server using HTTP client, I started thinking about easier and cleaner way to do it. I was writing integration tests and I wanted to re-use some model classes instead of dictionaries. Here’s how to do it. Sample of integration test is incluced.

Writing to CSV-file from multiple threads

I was writing document and metadata exporter that reads data from SharePoint and writes it to multiple files. I needed to boost up performance of my exporter and I went with multiple threads pumping out the data from SharePoint. One problem I faced – writing metadata to CSV-files from multiple threads in parallel. This blog post shows how to do it using concurrent queue.