Upgrading to ASP.NET Core 3.0

Background

I have spent the past few days converting some of my ASP.NET Core 2.2 projects to 3.0 which just came out 3 days ago. I'm using Razor, SignalR and Dapper in some of these projects and I knew the conversion won't be straight forward since a lot of the core libraries have undergone major re-work.

The easiest thing you can do to get started is to create a new project using one of the existing project templates in Visual Studio and compare its settings with your own project. This should give you a good idea on what to change in order to get your project going again. The official migration guide from Microsoft also has a lot of useful information.

It shouldn't take too long before most people work out all the necessary changes needed to start their projects again. So the main focus of this blog is to share some of the "less obvious" breaking changes I have encountered during my upgrade and provide their solutions. Hopefully you and other developers will find this blog useful and save some precious time when doing the upgrade.

1. Razor view run-time compilation not working after the upgrade

This is a major inconvenience for people who are using razor view in their projects. According to Microsoft the Razor view run-time compilation is removed from the ASP.NET Core shared library and moved to a separate NuGet package.

To solve the problem, first add the following NuGet package to your solution:
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation  

Then open Startup.cs and add the following line to the ConfigureServices function:
services.AddMvc().AddRazorRuntimeCompilation();

2. SignalR's communication between client side and server side is now type-safe

If you are using SignalR.NET in your project, you may get the following error message in the browser when invoking one of your SignalR hub functions after upgrading to ASP.NET Core 3.0:
Error: Failed to invoke 'yourFunctionName' due to an error on the server.

This is due to type mismatch between you client side code and server side code. 

I haven't found any official documentation from Microsoft yet but my solution is to simply make the type match between server and client.

3. WebAPI can no longer return dynamic type generated by Dapper

I have been using Dapper in one of my simple WebAPI to return List<dynamic>. This worked perfectly in ASP.NET core 2.2. 

As soon as I upgraded to ASP.NET Core 3.0 I got the following errors:
System.InvalidCastException: Unable to cast object of type '<GetEnumerator>d__9' to type 'System.Collections.IDictionaryEnumerator'. at System.Text.Json.JsonSerializer

This indicates that the new high performance System.Text.Json.JsonSerializer may not support this or there is a bug somewhere in the new system.

I don't have a solution to this but my work around is to create a solid class and Dapper will happily map the query result into the class. Luckily for me I only need to create a few simple classes to get my API going again.

Update (10/11/2019): this issue has been reported to Microsoft and hopefully it will be fixed in the next .NET Core release. The issue is caused by the new JsonSerializer does not support enumerator types that don't implement the IDictionaryEnumerator interface. This is why DapperRow failed because it returns IEnumerator<KeyValuePair<string, object>>.

You can see more details in the links below. There is also a unit test added for this issue in the CoreFX source code which you can find in the second link.
https://github.com/dotnet/corefx/issues/41329
https://github.com/ahsonkhan/corefx/blob/085ca53fd648694a1c6cd966c87032f8d5aed71b/src/System.Text.Json/tests/Serialization/Value.WriteTests.cs#L324-L332

Conclusions

Hopefully by now you have successfully upgraded your project to use the new ASP.NET Core 3.0. I'm sure better solutions will be found in the future and more problems will come up. If you can provide more insight to the above issues, please let me know. I will try my best to keep this blog updated. After all, what can be more fun than solving problems :-)

Happy upgrading and looking forward to your feedback!


Comments

Popular posts from this blog

New Browser Based Multiplayer Snake Game

Experimenting with Google Vision API