Migrating to .NET Core: Mission Complete

Migrating to .NET Core: Mission Complete

It’s been over 5 years since I started a greenfield project that initially was developed using Katana/Owin with ASP.NET Web API. Over the past 3 years, since .NET Core 2.0 was released and .NET Standard 2.0 was defined, I’ve been slowly chipping away at migrating to .NET Core. It’s been a long road but we’re now fully migrated and running in production with ASP.NET Core on .NET Core 3.1.

Migrating from .NET Framework to .NET Core

This blog post is in a series about migrating from .NET Framework to .NET Core.

Solution/Project Size

To give some sense of scale, our application consist of two primary entry points.

The first is an HTTP API that started out as a shelf hosted (not using IIS) Owin/Kata using ASP.NET WebAPI. Over time that morphed into using Nancy. During the migration, it ultimately moved to ASP.NET Core using the OWIN Middleware to still use Nancy.

The second entry point is a worker process that processes messages from a message broker. This is primarily for handling events and fire-and-forget commands. This has always been a console app.

To give a sense of scale, I ran NDepend over our solution to get lines of code metric, just to give some idea of the size.

Migrating to .NET Core

Journey: Migrating to .NET Core

I’ve given a full experience report on my YouTube channel. This covers pretty much everything I’ve done along with the process of getting our app running under .NET Core in production.

Questions or Feedback

If you’re thinking of doing a migration and have any questions, let me know in the YouTube comments, in the comments on this blog or on Twitter. I’d be glad to help if possible.

If you’ve done a migration and would like to share some tips, get in touch with me!



Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

Getting Started: Apache Kafka with .NET Core

Getting Started with Apache Kafka & .NET Core

If you’re interested in playing around with Apache Kafka with .NET Core, this post contains everything you need to get started.

I’ve been interested in Kafka for awhile and finally sat down and got everything configured using Docker, then created a .NET console app that contained a Producer and a Consumer.

Here’s my complete process of what that involved.

YouTube

Check out my YouTube channel where I created a video that accompanies this blog post.

Docker

The first thing you need is to pull down the latest Docker images of both Zookeeper and Kafka.

Before we create any contains, first create a new network that both contains are going to use.

Now you can create both Zookeeper and Kafka containers. Kafka needs to communicate with Zookeeper. All the port mappings are the standard ports listed in the Zookeeper and Kafka docs.

Producer

Now that we have Zookeeper and Kafka containers running, I created an empty .net core console app. I’m going to create a hosted service for both Producer and Consumer.

For the producer in this demo, I’m using the Confluent.Kafka NuGet Package.

For this example, I’m going to iterate 100 times and produce a string of “Hello World {i}” that is going to be sent to a topic called “demo”.

In the constructor I’m creating a new producer and specifying that Kafka is running on localhost:9092, which is our container.

The other thing to note is that this “demo” topic I’ve created manually ahead of time, not within this code sample.

Kafka UI

If you need to create a topic, you can do so using the command line tools, or if you prefer something visual you can check out Conduktor.

Consumer

For the consumer, I decided to try the kafka-sharp NuGet Package after I saw this tweet.

The consumer follows a similar type of pattern where in the Constructor I’m creating the connection to localhost:9092. In the StartAsync is where the magic happens of subscribing to the demo topic. The MessageReceived lambda is what will handle all messages received, where I’m just logging the message value.

Full Sample

For completeness, here’s the generic host and both Producer and Consumer hosted services above.

You can find the entire sample on my GitHub.

If you’re using Kafka, which library are you using? Are you using a event/message dispatcher on top of Kafka or using it directly. Let me know in the comments or on Twitter.

Related Links

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

NuGet PackageReference Versions SOLUTION Wide

NuGet PackageReference Versions SOLUTION Wide

If you have a solution with many projects, it can be a challenge to make sure each NuGet Package has the version across all of your projects. Each PackageReference across all csproj files must specify the same version of the NuGet Package. Here are a couple of ways for managing NuGet PackageRerence Versions across your entire solution.

YouTube

Check out my YouTube channel where I created a video that accompanies this blog post.

PackageReference

First, in order for this to work, you must be using the new SDK style csproj files. This is where your NuGet packages are referenced via the <PackageReference> element.

If you haven’t migrated to the SDK style csproj, check out my migration blog post.

Here’s an example of the eShopOnWeb.Infrastructure.csproj

In this solution, I also have another project (eShopOnWeb.Web.csproj) that is also referencing the same version of Microsoft.EntityFrameworkCore.SqlServer package.

These PackageReferences must always have the same version matching otherwise you’re going to run into issues.

Directory.Build.targets

Instead of making sure each csproj has the same version of a package, you can centralize this by using a file called Directory.Build.targets.

Start off by creating a file called Directory.Build.targets at the same level as your solution file (.sln). This file will contain the PackageReference along with the version. Here’s an example.

This looks very similar to a csproj and how PackageReference’s are used. However there’s one important distinction. Instead of Include attribute, it’s instead Update.

Now back to our csproj, we can now remove the version from the PackageReference

Solution Wide PackageReferences

Another cool use for Directory.Build.targets is to have PackageReferences that are included for every project in your solution. To do this, simply add a PackageReference just as you normally would in a csproj including the version.

In the example below, I’m adding Microsoft.CodeAnalysis to every project.

Note that for this package, we’re using the Include attribute.

Directory.Packages.props

The second way of this is using the new (as of .NET Core SDK 3.1.300) Directory.Packages.props.

Stuart Lang posted about this and how to use it. Check out his blog post Managing Package Versions Centrally.

If you’re using another method, let me know in the comments, on Twitter, or on the YouTube video.

Related Links

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.