« Back to home

Migrating to ASP .NET Core RC2

Daj się poznać

After waiting for some time, I was happy when ASP.NET Core RC2 was recently released. This version should have debuted at the beginning of 2016, so imagine my anticipation and excitement to install it on my Linux machine and upgrade my project to this version.

To install this version of ASP.NET Core I started on this page and then I chose Linux -> Ubuntu 14.04. I followed the step-by-step instructions provided and after a while I could run the command dotnet -h in my terminal, and I saw this:

dotnet -h

Now I could start to upgrade my project. There were pages where I could find instructions on how to adjust the project to the new version of ASP. On this page I found out how to migrate from dnx to new the .NET Core CLI and on this page I found out how to upgrade my project to the RC2 version.

The first steps of the upgrade were rather simple and mechanical. I had to change the dependencies in project.json. All packages with names that started with Microsoft.AspNet. now have names starting with: Microsoft.AspNetCore.. I also had to change the version of the packages. My dependencies section looked like this:

"dependencies": {
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview1-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Binder": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.NETCore.App": {
        "type": "platform",
        "version": "1.0.0-*"
    }
},

Another change is that a new package Microsoft.NETCore.App appeared among the dependencies, and it was a runtime for the application.

I could remove the dependency Microsoft.AspNet.Mvc.TagHelpers because now it is included in the package Microsoft.AspNetCore.Mvc.

I also had to fix the namespaces. I had to change each namespace which started with Microsoft.AspNet. to Microsoft.AspNetCore..

The changes where required also in the structure of project.json. Some properties are no longer valid, for example: commands. I must also specify the frameworks section and it looked like this:

"frameworks": {
    "netcoreapp1.0": {
        "imports": [
            "dotnet5.6",
            "dnxcore50",
            "portable-net45+win8"
        ]
    }
},

I also added the section buildOptions with the following properties:

"buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
},

After upgrading to RC2, the ASP application became like an ordinary console application. That’s why I had to create a class with the main method where I started my ASP app. This class looked like this:

public class Program
{  // Entry point for the application.
    public static void Main(string[] args) 
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())       
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();   
    }         
}  

And I removed this line of code from Startup.cs

// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);

I also had to remove the line:

app.UseIISPlatformHandler();

from the Startup.Configure method because now IIS is configured in Program.Main method.

After these steps, I could finally run the command:

dotnet restore

And then I was able to build my app with the command dotnet build but I received two errors:

/home/piotr/Repositories/notifier/src/web/Startup.cs(26,46): error CS1061: 'IConfigurationRoot' does not contain a definition for 'Get' and no extension method 'Get' accepting a first argument of type 'IConfigurationRoot' could be found (are you missing a using directive or an assembly reference?)
/home/piotr/Repositories/notifier/src/web/Startup.cs(27,22): error CS1061: 'IServiceCollection' does not contain a definition for 'AddInstance' and no extension method 'AddInstance' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Compilation failed.
    4 Warning(s)
    2 Error(s)

It seems like the extension methods Get and AddInstance no longer exist. A bit of research led to a solution for how to fix the lack of AddInstance extension method. Now it is called AddSingleton, but I couldn’t find a replacement for Get<> extension method so I changed my code a bit and now it looks like this:

var authSettings = new AuthSettings(); 
var authSection = Configuration.GetSection("AuthSettings");
authSettings.ClientId = authSection["ClientId"];
authSettings.Authority = authSection["Authority"];
services.AddSingleton(authSettings);

After this, my code compiled and I could run it with the command:

env ASPNETCORE_ENVIRONMENT="Development" dotnet run

The result of this command was another error:

Unhandled Exception: System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional.
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Notifier.Web.Startup..ctor(IHostingEnvironment env)

It looked like my application didn’t have a setup base path so it didn’t know where to look for appsettings.json. In the new version of ASP, you specify the app basepath by setting it in the ConfigurationBuilder with a code like this:

var builder = new ConfigurationBuilder()
                .SetBasePath(hostingEnvironment.ContentRootPath);

As you may notice, the new version of ASP also changed the names of the environmental variables. To set the runtime environment of an application you now use the variable ASPNETCORE_ENVIRONMENT.

After setting up the base path, I ran the application again and it ran without errors. I navigated to the localhost:5000 and saw this screen:

Final result

I think that this is proof that I managed to upgrade my ASP.NET Core application to the RC2 version. I must honestly say that this process was very boring and took me longer than I expected. I had to change the way I got the settings from the configuration file. I don’t know why I couldn’t get access to the extension method Get<> on the IConfigurationRoot. I’m not sure if it was removed or renamed, but I didn’t manage to find a replacement for this method. When I wrote this post, the RC2 version was fresh out the oven (3 days to be exact) so I am sure that I’ll find more information about this missing method soon.

By the way, this is my last technical post in the Daj się poznać contest series. In the next post, I’ll write a summary of my work for this contest.

Related posts:

Comments

comments powered by Disqus