Mobilize Blog

Application Modernization

Recent Posts

Posts by Topic

see all

Updating an ASP.NET Core 2.2 Web Site to .NET Core 3.1 LTS

Posted by Mauricio Rojas on Dec, 12, 2019 @ 07:12

Find me on:

Scott Hanselman is one of my geek heros. So when I so his excellent post https://www.hanselman.com/blog/UpdatingAnASPNETCore22WebSiteToNETCore31LTS.aspx I said to myself "it is useless to resist"!!

So I took our legendary Salmon King Seafood App. Yep, our VB6 reference app that we had already taken to ASP.NET Core 2.2. And now with the guide of the Hanselman take it to the 3.1.

Ok. So the first thing is to edit the csproj file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <TypeScriptToolsVersion>2.5</TypeScriptToolsVersion>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
        <StartupObject>WebSite.Startup</StartupObject>
        <AssemblyName>SKS</AssemblyName>
        <ProjectGuid>c83435e8-3b9f-4b39-b67f-c83120cafc71</ProjectGuid>
        <Deterministic>False</Deterministic>
    </PropertyGroup>

 

Next, you need to change public void Configure(IApplicationBuilder app, IHostingEnvironment env) to public void Configure(IApplicationBuilder app,IWebHostEnvironment env). IHostingEnviroment was deprecated.

I think the biggest change is related to setting up your routes with the AddMVC. In ASPNet Core 2.2 you could write:

 app.UseMvc(routes =>
            {
                routes.MapRoute("DefaultApi", "api/{controller}/{id}");
            });

 

But on .NET Core 3.1 you have to adjust that code to something like this:

  app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("DefaultApi", "api/{controller}/{id}");
                endpoints.MapHealthChecks("/health");
            });

 

All these is detailed at the microsoft docs for upgrading to .NET Core 3.0

The MapHealthCheck is not mandatory. I just think is a good practice to add Health Check Support

Next a minor change: add the using Microsoft.Extensions.Hosting; this is necessary to allow the IsDevelopment extension method to work.

And that point everything compiled so I gave it a try, but I got a nasty exception:

System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.

Oh wow!! It seems this is change related to this AspNet Core Issue https://github.com/aspnet/AspNetCore/issues/7644

But that is easy to fix. You just add at the end of the ConfigureServices these lines:

            // If using IIS:
            services.Configure<IISServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;
            });
            // If using Kestrel:
            services.Configure<KestrelServerOptions>(options =>
                {
                    options.AllowSynchronousIO = true;
                });
            services.AddHealthChecks();

 

And... you are done!.

 

Now you can just run the app on .NET Core 3.1

You can check out the code on our github repo: https://github.com/MobilizeNet/SKSWebMap/

 

As you can see, once you modernized a legacy app, either VB6, WinForms, PowerBuilder or Delphi you get a more open flexible platform.

The Hanselman goes deep into other interesting stuff like publising to azure and using side loading. Check his post. He is really wise guy. 

 

Topics: Visual Basic Upgrade Companion, VB6, convert Windows to Web, .NET, C#, Web Application Development, .NET Core, webapps, NETCORE3.1