Senior Software Consultant C#, DevOps, Cloud


You don’t need a IDesignTimeDbContextFactory

If you have ever gotten the error message, that tells you to add IDesignTimeDbContextFactory to your project, when doing EF Core migrations. This particular one: Unable to create an object of type ‘MyContext’. Add an implementation of ‘IDesignTimeDbContextFactory’ to the project, or see for additional patterns supported at design time. You will probably be scratching your head, asking some questions and maybe even swear a little. Because the console will tell you to implemnt a IDesignTimeDbContextFactory so that is But don’t fret, you don’t need one. You can avoid it by having making sure of the following: The entire solution must be buildable and be able to run! If you have a project that cannot build, you can exclude it with the build configuration manager. Default constructor For the system to be able to build migrations from your context, it needs to be able to instantiate it with a default constructor! Implement the correct overloaded constructor! DbContextOptions<TContext> instead of DbContextOptions. ASP.NET Core If you are running an ASP.NET Core application, and you use the ServiceProvider (IoC) that is built in, then you must make sure that your DbContext and its’ dependencies are registered. You must also make sure that you are using the DbContextOptions<TContext> overloaded constructor and not the DbContextOptions overloaded constructor. When do I need a IDesignTimeDbContextFactory? When your DbContext exists in another project, like a shared library. Meaning that is has no context to run from. EF Core migrations works by actually running your code, if that is not possible, then you need to...

Nuget + XML docs + Swagger 0

Add NuGet package XML documentation to Swagger

Copy XML documentation from NuGet package to project build folder! At my current client, we are building an API that is put together by re-usable “API parts”, eg. ASP.NET Core Application Parts, which works wonders by the way. We can have multiple parts of the API split into small NuGet packages that can be re-used in other systems, that way we only have to implement system specific code, the rest is reused, including documentation etc. Talking about documentation, brings us directly to the issue. We use SwashBuckle to generate our Swagger definition and Swagger UI, and Swashbuckle requires XML documentation, to be able to include documentation from our Controllers and models. I thought we could probably just add a checkbox “Add XML documentation from NuGet package, on build”, but… Unfortunately not. After a lot of research, and looking at NuGets github issues stating this problem I came up with a solution that works both at build time and at publish time (which apparently is handled differently). The Setup We have the following projects: RestAPI (Root website, that will contain multiple API packages) ASP.NET Core website Contains Swagger page System specific API Service implementations BasicAPI (NuGet package) ASP.NET Core website Contains API endpoints, Models, Service interfaces etc. Public XML documentation for its own API and Models .. more NuGet packages to come Include XML documentation, when building NuGet package with .csproj file Our application is very simple, so we have chosen to pack nuget packages with our .csproj file instead of the more explicit .nuspec file. Including...

C# 8 records 3

Immutable Records could be added in C# 8.x

There are talks about this feature being postponed to a minor version after 8.0 is released, as it might not be ready for the major C# 8.0 release. The features for C# 8.0 still hasn’t been decided yet, so that means this information is still subject to change! Since Microsoft rebuilt the C# compiler into what is now the Roslyn compiler, the language teams’ feature implementation speed has been phenomenal, they are cranking features out in record time.  Pro C# 7: With .NET and .NET Core Microsoft is committed to continuously update the C# language. They have even given us incremental updates of features that were not yet ready for the major versions release date and subsequently added it in minor language updates, such as C# 7.1, 7.2 and 7.3. Their speed is incredible and it doesn’t seem that long ago that we were introduced to C# 7, and many developers might not even have started using its features. If you haven’t caught up on C# 7, you can take a look at this book: Pro C# 7: With .NET and .NET Core But alas! C# 8 is on its way and has been for some time now. The problem If you have some years under your belt as a software developer, you know the code smell of passing primitives, like ints and strings around your system. Value Objects keep encapsulation in place Consider the following code: Here we are notifying a class that the user state has changed. The user state is spread across...


Hunting .NET memory leaks with Windbg

Recently a client called me about an issue where one of their production servers would run out of memory, every other week. The application in question was a .NET Framework 4.5 Windows service, that runs in an Azure VM, and ever so often it would become unstable and start causing trouble. I have previously helped this client set up an ELK stack, so it was quick for me to go into Kibana, look at metricbeat data, and see that their server indeed slowly was eating up memory over time. And every time the application was restarted, the memory would return to normal, and slowly creep upwards again. As you can clearly see, the application uses gradually more and more memory over time. Every time the line drops, was a restart of the server, where it went back to normal operation at about 150 MB. When they initially called me, they had just restarted the application, so I had no real practical way of finding out what caused the memory right there and then, but I logged on to the server and created a memory dump for the recently restarted application, so I would have a baseline for how it looks during normal operation. Creating a memory dump file The best way to figure out what is causing a memory leak, is to analyse the memory of the running application. To do that, we need to make a “memory dump”, and thankfully on Windows this is straight forward. Open Task Manager. Go to the Processes(older) or...

Software consultant 1

10 tips and tricks for solving software problems

As software consultants, we are often faced with bugs or problems the client cannot fix themselves. We are expected to quickly figure out the problem, and propose or implement a solution. Through my career I have developed a sort of mindset to get to the bottom of issues without wasting time hunting deadends or non-issues, and deliver results and value more efficiently. Here is my list of 10 tips for how I go about solving software problems 1. Focus A common problem I saw in myself, and I see very often in software developers, is a lack of focus. If you are trying to solve too many issues at once, you will probably fail at all of them. The first thing you must do, is figure out what you are actually trying to solve, and then stay on target. Usually my clients, have some sort description of what should be happening, and what isn’t happening. The description of the issues are often vague, but can be boiled down to tangible issues. As developers, we have a tendency to hunt any bug we find. If for example you are dealing with communication issues between two services, it might not be relevant to your current task, to start debugging the datalayer or refactoring a calculation algorithm. A side effect to lack of focus, is that your git history becomes unclear and kind of a mess. All of a sudden you have two or more half-done fixes staged, which means that either you can’t commit the first fix,...

Redis connection lost 0

Redis connection issues

At my current client, we have been dealing with an ongoing problem while scaling their cloud systems to the ever increasing customer-base. As it is with any software that needs to scale, we’ve been seeing and solving scalability problems along the way. Anyone who has ever dealt with scaling a system to accommodate a lot of concurrent users, knows that issues will be showing up places you aren’t expecting. While scaling the system, we’ve made it more resilient and fault tolerant, and also learned to mitigate many of the problems as they are arising. We’ve improved logging and metrics, so we know exactly what is happening, and can see stability problems before they affect the end-users. Redis loses connection, and will not reconnect In our system, we use StackExchange.Redis(v1.2.6) to communicate with our Redis server. The Redis instance handles all cached data and all communication between services. It is a vital part of our architecture, and currently the architecture is heavily dependent on this connection. The odd thing is that we keep losing connection to it, but Redis is not even breaking a sweat. StackExchange.Redis is supposed to be able to recover from outages, and in most cases it does. But every once in a blue moon, it doesn’t. It think it does, and it thinks it is connected, but in reality all subsequent calls fail, and the pub/sub connection is completely dead, until the application is restarted. We have multiple services, and it will always just be one of them that goes down, every...


Re-use Controllers, Views and Tag-helpers in ASP.NET Core

I recently ran into a situation, where I needed to implement the same functionality, the same views and even the same tag-helpers for different customers. Like most other developers I hate copy-pasting code that should be shared across multiple projects. I had come across a similar need a few years back, but remembered that it was cumbersome in ASP.NET 3-5, to achieve this, and the solutions back then didn’t feel right. But since my recent switch to dotnet core, I knew there was a chance that Microsoft had a prepared an early christmas present for me. So I started researching my options, searching for titles similar to this blog post, and finally found a solution on the third place on google (Yes I was that desperate). Introducing Application Parts / App Parts With the introduction of ASP.NET Core v1.0, MVC came with a new concept called “Application Parts”, which is how the features and resources is populated into a ASP.NET Core MVC application. MVC loads it’s features from different application parts, which is a new type in ASP.NET Core. Specifically for my use-case, it can load the parts of an application from another Assembly (read: dll file). By creating an AssemblyPart object, you can discover controllers, views and tag helpers from a shared library. Adding controllers from a shared project Consider that I have a basic MVC site called “MyWebApp”, and another project called “SharedModule”, of which MyWebApp has a reference to. The SharedModule is created as an Empty MVC site, and then later populated...


Hello world!

Finally, I created the blog. My old website on this same domain, was a static html file, telling people who I was, and what I do. Not only was that 3 years out of date, and I had since changed jobs, and become a consultant. But it provided no value for anyone.   Like most other developers, I run into problems and find solutions to those problems, but I am not sharing that knowledge with the world, which is a shame. Through my career I have functioned as a tutor and then later a teacher at the local academy, but I often find myself in mentoring positions, such as the one I am in now. I currently work as a Senior Software Consultant, and one of my many duties, is to mentor and learn the junior developers how to write good code and build a sustainable architecture. In general I love to share, but stackoverflow is not always the right place for general knowledge sharing, even though I love that platform. So here goes nothing. Maybe I will be the next big blogger, or my blog will die in silence, like so many others 😀