Skip to main content

Migrate existing ASP.Net Core 2.0 application to ASP.Net Core 2.1

ASP.NET Core 2.1 preview 1 is already out and available for everyone. Read this post to find out how to get started with it. ASP.NET Core 2.1 comes with lots of new features like HTTPS by default, HTTPClientFactory, SignalR, and others. To know all the new features, you can read this post. So now it’s a good time to migrate existing ASP.NET Core 2.0 application to ASP.NET Core 2.1.

Migrate existing ASP.Net Core 2.0 application to ASP.Net Core 2.1

As there are no major code breaking changes in ASP.NET Core 2.1, the migration process is straightforward. Follow these steps to migrate an existing ASP.NET Core 2.0 application to 2.1 preview 1:

  • Update the target framework to .NET Core 2.1. This can be done in 2 ways. Right click on Project and then Properties -> Application tab -> Target framework -> select .Net Core 2.1 as below:

    Migrate existing .Net Core application to .Net Core 2.1

    Or
    You can edit the .csproj file and set target framework. Like,

    <PropertyGroup>
       <TargetFramework>netcoreapp2.1</TargetFramework>
    </PropertyGroup>
    
  • Next, we need to update the package reference. Remember, ASP.NET Core 2.1 introduces a new meta-package for use by applications: Microsoft.AspNetCore.App. So the 2.1 projects must target to this meta-package, rather than the existing meta-package Microsoft.AspNetCore.All. Your existing .csproj may look like this,
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
      </ItemGroup>
    
      <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
      </ItemGroup>
    

    We need to update the versions of the packages for any Microsoft.AspNetCore, Microsoft.Extensions, and Microsoft.EntityFrameworkCore packages to 2.1.0. Also, update the versions of the various <DotNetCliToolReference> elements for any Microsoft.VisualStudio, and Microsoft.EntityFrameworkCore packages to 2.1.0.

      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0" />
      </ItemGroup>
    
      <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.1.0" />
      </ItemGroup>
    

That’s it. Your ASP.NET Core 2.0 application is now ready to build and run against 2.1.0-preview1. Please note that your project might require some additional steps depending on the packages referenced.




Here, we are not utilizing any of the new features and code-based idioms recommended in ASP.NET Core 2.1. Let’s implement HTTPS by default and new code changes in Program.cs file.

  • In ASP.NET Core 2.1, BuildWebHost is changed to CreateWebHostBuilder. Here is your ASP.NET Core 2.0 Program.cs file code,
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }
    
        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
    

    Below is the code of Program.cs in ASP.NET Core 2.1.

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
           WebHost.CreateDefaultBuilder(args)
               .UseStartup<Startup>();
    }
    

    Update the Program.cs code to match with ASP.NET Core 2.1 code-based idioms. If you want to skip this, it’s fine as things still work.

  • To enable HTTPS, we need to add 2 middlewares to Configure method. So open Startup.cs class and navigate to Configure method. Add a call to add the HSTS middleware after the exception handler middleware: app.UseHsts(); and then add a call to add the HTTPS redirection middleware before the static files middleware: app.UseHttpsRedirection();. Like,
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }
    
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
    
        app.UseAuthentication();
        app.UseMvc();
    }
    

    The HSTS protocol enforces clients (web browsers or other complying user agents) to interact with the server via HTTPS connections, and never via the HTTP protocol. HSTS protocol is typically used in non-development scenarios, therefore the UseHSTS() middleware is added in the else part of the code.

    Next, we need to enable SSL for the project. To do that, right click on the project and then Properties -> Debug tab -> check the “Enable SSL” checkbox and save the changes. Also, add an environment variable named ASPNETCORE_HTTPS_PORT and set its value to the port number displayed in the URL against the “Enable SSL” checkbox.

    Enable SSL for ASP.NET Core 2.1

    Lastly, Open the launchSettings.json file. This file is located under Properties. In the launchSettings.json, add a new property “sslPort” in the “iisSettings”/”iisExpress” section. The value will be the SSL port number set to the ASPNETCORE_HTTPS_PORT environment variable. Like,

     "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:57905/",
          "sslPort": 44395
        }
     }
    
    

    That’s it. These configurations value will be read by the HTTPS redirect middleware to ensure non-HTTPS requests are redirected to the correct port.

Conclusion

To conclude, this post shows the steps to migrate existing ASP.NET Core 2.0 application to ASP.NET Core 2.1. It’s a very straightforward process as no major code breaking changes introduced in ASP.NET Core 2.1. The post also shows how you can enable HTTPS for the ASP.NET Core application.

Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

PS: If you found this content valuable and want to return the favour, then Buy Me A Coffee

16 thoughts to “Migrate existing ASP.Net Core 2.0 application to ASP.Net Core 2.1”

  1. After following all above steps -I’m able to run the project but after some time getting 503- service unavailable while calling Web API from service.
    Its looks quite strange.

  2. You don’t mentioned parts about:
    * Microsoft.EntityFrameworkCore
    * Microsoft.EntityFrameworkCore.Relational

  3. Hi Tony,
    great posting that helped me a lot!

    At the end of your posting your Startup.cs still contains a call of app.UseBrowserLink();
    In my project, I had to remove that line, because it threw errors:
    CS1061 “IApplicationBuilder” does not contain a definition for “UseBrowserLink”, …
    Also, there is no trace of UseBrowserLink() in the Startup.cs of the test project that I created with .NET Core 2.1.

    I am using the very new Preview 2 version. Is this a change of that new version? I could not find anything about it on the web.

    1. Hi JPSY,

      Thank you for stopping here and putting your comments. I am glad that it helped you and thank you for buying me the coffee. Yes, you are right. The UseBrowserLink is no longer available. I don’t know when it is got removed as when I migrated one of my app, it was building successfully.

      Thanks for pointing out. I have also updated the post.

    1. I think Yes. From the microsoft blog,

      “EF Core now contains the necessary building blocks for anyone to write entity classes that can load their navigation properties on demand. We have also created a new package, Microsoft.EntityFrameworkCore.Proxies, that leverages those building blocks to produce lazy loading proxy classes based on minimally modified entity classes. In order to use these lazy loading proxies, you only need navigation properties in your entities to be virtual.”

      https://blogs.msdn.microsoft.com/dotnet/2018/02/02/entity-framework-core-2-1-roadmap/

  4. How do we update our code to work with Microsoft’s recommended method to seed test data in Program.cs? They had a nice sample application showing how this would work in core 2.0, but it is now broken in 2.1. Have a look at the tutorial https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro

    The seeding method Is about half-way down the page. The issue I’m getting is that after updating my code from using BuildWebHost (2.0) to CreateWebHostBuilder (2.1), I can’t find a way to set the scope variable to use the CreateScope() method. Any thoughts?

    1. Hi Tony,

      You can implement this in following way.

      var host = CreateWebHostBuilder(args).Build();
      using (var scope = host.Services.CreateScope())
      {
      //Code goes here..
      }
      host.Run();

      You will also have to add a reference of “Microsoft.Extensions.DependencyInjection”.

      Let me know if this works..

Leave a Reply

Your email address will not be published. Required fields are marked *