Moving from Node.js to .NET Core

Jim Wang

Jim

Here on Visual Studio App Center, our platform is built as a set of microservices, which has afforded teams to make language and platform choices that work best for them, and ultimately allowed us to move and iterate quickly. Over time, two distinct stacks have emerged: 

  • TypeScript/JavaScript running on Node.js, deployed to AKS (Azure Kubnernetes Service) 
  • .NET Framework (C#) running on Windows Service Fabric 

As we have continued to merge our teams and do more and more cross-team work, it’s become apparent that we should settle on a single choice and unify so that jumping between services is less jarring. The fact that a lot of our customers run non-Windows platforms, and our team runs a mix of Mac, PC, and Linux, led us to explore a new direction. 

Enter .NET Core 

We started investigating .NET Core as a solution because of the cross-platform abilities it offered, and the fact that it works great with VS Code, our cross-platform development solution. We felt like .NET Core allowed us to preserve this flexibility, but not continue diverging our code base, with some new services written in TypeScript, and others in C# on the .NET Framework. This allows us more flexibility when moving work between teams as well. We could have also gone with TypeScript but given the skill set of the majority of the team (C#) and the pain we have seen managing Node.js dependencies, we decided on .NET Core. We’ll discuss the dependency issue in more detail later in this post. For more information on .NET Core, see the docs. 

Our goal with this post isn’t to convince you that .NET Core is better than Node.js: we made our decision primarily based on the skill sets of our teamInstead, we want to share the things that we noticed as we have been starting the porting work, in the hope that if you’re embarking on a similar journey, this information will be helpful. If you’re curious about direct comparisons regarding performance, this site is a good resource that is constantly updated. The .NET Core entry on that list is called aspcoreAnother interesting post exploressaturating 10GbE at 7+ million request/s using .NET Core.  

Also, note that for the purposes of this post we aren’t looking at specific TypeScript features, and on our team, we continue to use TypeScript for new front-end development as there are many benefits to having concrete types on top of JavaScript. 

The Challenge 

We are not embarking on a re-write of all services, but aside from having all new services run on .NET Core targeting AKS, we are looking for opportunities to port over problematic code from either the .NET Framework or Node.js. In this blog post we’ll talk about going from Node.js to .NET Core, by exploring some key differences we noticed as we ported some targeted REST API endpoints. For our take-aways, we are mainly focused on the developer experience, and not necessarily on performance benchmarks or other orthogonal concerns. 

Key Take-Aways We Noticed 

Porting code into a strongly typed language takes time 

In our existing TypeScript code base, we take advantage of dynamic types frequently, and in the C# version of the code we had to define more concrete types. Additionally, the code that we were porting specifically dealt with bit masks, so translating those concepts between JavaScript and C# presented some pain for us. One of the main things we lost was easy parsing of JSON and dynamic object graphs. .NET Core includes JSON parsing out of the box, but it isn’t quite as native as what JavaScript provides. 

Another thing that came up was the need to create more mapping types instead of relying on dynamic mapping, which we didn’t have to handle as much since we were already in TypeScript, but if you are coming from a straight JavaScript project this is something else you will want to consider. 

All in all, if you embark on this journey, be prepared to invest the appropriate amount of time on model classes and other scaffolding that you will need for a successful transition. 

String comparison differences 

In JavaScript, when doing case insensitive string comparisons, we tend towards doing  toLowerCase()  prior to doing a ===, where we don’t care about case. Generally speaking, the equivalent C# code uses  string.Equals  and passes in a  StringComparison enum value to indicate how to handle issues of case sensitivity. A common case is scenarios where you do not care about case and locale, and for those you should use StringComparison.OrdinalIgnoreCase. Here are some useful references for other scenarios that may arise: 

A concrete example of porting where both examples return success under the same conditions: 

JavaScript:

Equivalent C#:

 

Fewer dependencies in .NET Core 

One thing that has been nice is to manage fewer dependencies (and a shallower dependency tree) in the .NET Core version of the code, and hopefully this means it will be easier to maintain. We have struggled with keeping dependencies up to date and managing many dependencies in our Node.js projects. Additionally, developers on our team prefer NuGet’s policy of “lowest matching version” rather than npm’s encouragement to use “highest matching patch” or “highest matching minor version” (using the ~syntax in package.json by default via npm i). We recognize this is a philosophical difference and opinions on this may vary. The pros of the NuGet approach are more control and explicitness, whereas the npm approach allows for theoretically non-breaking changes to be easily applied. 

Other Thoughts 

There isn’t one right answer, and the purpose of this post isn’t to convince you one way or another, just to shed some light on the process we have gone through recently as a team. As we discover and do more in .NET Core on AKS we will be sure to share along the way! 

Jim Wang
Jim Wang

Follow Jim   

16 Comments
Cyril Dumange
Cyril Dumange 2019-05-24 04:38:31
Don't know if I would have chosen the same. But one think make me tick. The easyness dev in netcore using VS Code. I tried it for a few days and I remember wanted to kill the c# extension that was regulary crasing. Do you have any interesting ressources on creating a stable c# dev env using vsCode ?
Avatar
Michael DeMond 2019-05-26 01:45:12
Great to hear that MSFT is abandoning its silly TypeScript/JavaScript fascination and finally recognizing where the real code is at with .NET Core.  We'll have a laugh about this unfortunate and embarrassing chapter in MSFT history at some point, I am sure.
Avatar
Tony Henrique 2019-05-26 10:55:46
This is very cool!When .NET 5 arrive, I think that the tendency is that more code will be written in .NET and also use some cross-platform Universal XAML that will run on Web, Mobile, Desktop and IoT.In my opinion, F# can absorb some JS philosophy: F# can evolve to be used with a complete dynamic, and also a strong typed model. Some steps indicate this, like the cool new F# Anonymous Record. Doing this, F# will be easy choice for javaScript and C# developers.
Avatar
Greg Veres 2019-05-26 21:17:12
I have been doing .Net (not core) with typescript on the front end for a few years now. You mentioned having more model classes. I assume you mean to handle the transition of data from the C# back end to the TS front end (or maybe between microservices). I have found using Typewriter with the appropriate template files provides an excellent way to create those mode classes in C# and have them available in TS. (https://frhagn.github.io/Typewriter/). I use an attribute on the classes I want to show up in TS and as soon as I save the C# file, the TS file is generated for me. It is a huge time saver and prevents so many "out of sync" errors!
Avatar
Musfiq Fahad Amin 2019-05-29 04:53:28
More maintainability as less dependancy, more control as more explicit package manager, drastic performance improvements and finally C#, one of the most well-designed languages out there. If these fulfill your project requirements and you've a competent team with enough time, why wouldn't you migrate?!
Avatar
Renato Fontes Tapia 2019-05-29 11:10:26
They are 2 completely different things. Why does this exist? 😂
Avatar
mirzadehmonireh63@gmail.com 2019-06-07 15:40:37
Great to hear that MSFT is abandoning its silly TypeScript/JavaScript fascination and finally recognizing where the real code is at with .NET Core.