Writing a custom OutputFormatter to return an Excel (.xlsx) from an action in ASP.NET Core API

As most of us, I already know my next week work schedule. It includes writing some actions in respond to a requirement for excel exports. It’s nothing new and its’s relatively easy with libraries like NPOI: you just create your excel on the fly and return a FileResult.

Read how to create an excel on the fly inside your action here: http://www.talkingdotnet.com/import-export-excel-asp-net-core-2-razor-pages/

And that could be the end of story for that requirement, but while walking home for the weekend I remembered about the XmlSerializerOutputFormatter I read on Microsoft Docs. The idea was to write my own ExcelOutputFormatter to respond to client requests with an Excel instead of a JSON.

Read More »Writing a custom OutputFormatter to return an Excel (.xlsx) from an action in ASP.NET Core API

Publishing ASP.NET Core: An error occurred while starting the application

Have you seen this error?
An error occurred while starting the application.
.NET Core <version> | Microsoft.AspNetCore.Hosting <version> | Microsoft Windows <version> | Need help?

It looks like this in your browser:

Besides that one hint, that the error is while starting, it’s not very descriptive, right?

Althouth there many things that could go wrong and the proper way is to actually troubleshoot the error, I end up googling about this for over an hour! In my search I found numerous articles but it turned out that for me the solution was simple!
Read More »Publishing ASP.NET Core: An error occurred while starting the application

Parameterized Raw SQL Queries with Entity Framework: How to Unit Test with SQLite

Entity Framework Core allows you to drop down to raw SQL queries when working with a relational database. This can be
useful, because raw SQL queries can return entity types or, starting with EF Core 2.1, query types that are part of
your model, but it could also be a problem when it comes down to Unit Testing.

Read More »Parameterized Raw SQL Queries with Entity Framework: How to Unit Test with SQLite

Thread synchronisation with SemaphoreSlim and async delegate calls

Although running a parallel foreach that makes async calls is a pretty usual thing, I found my self in a very complicated situation, when async delegate calls got in the way!

The situation arise while I was trying to write a MemoryCache manager for my application (the manager was actually a wrapper of MemoryCache Class).

The following gist file, is a basic structure of what I was trying to do: Ask MemoryCacheManager for an item with AddOrGetExisting by passing a key to find the object and a generic delegate Func task that actually returns the object.
Read More »Thread synchronisation with SemaphoreSlim and async delegate calls