ASP.NET 5 beta5 in Visual Studio 2015

  • Tutorial
A few days ago, a new beta version of ASP.NET 5, beta5, was released. It turned out that in the new version there are quite a lot of innovations and migrating from the previous beta version is not so simple. Consider the main innovations and how to upgrade the standard ASP.NET 5 project template with Visual Studio to fully upgrade to the new version.
+ sorts
+ article-based test
+ video of the latest ASP.NET Community Standup



The main news about the new release is posted on the msdn web dev blog . The most interesting, in my opinion, innovations:
  • Support for nuget third version feeds - now downloading and checking packages is much faster.
  • Support for C # 6 syntax in Razor markup.
  • Improvements in JSON.NET - now the version of this library in runtime and packages may not coincide, and you can just serialize .NET objects in views, for example, Json.Serialize (Model)
  • In addition, new features in the attributes of routing, refinement of tag helpers, etc.

Well, in general, for me this is the first version, after picking which everything seemed stable enough to work with it further. The only thing that is not yet quite convenient \ understandable is the work with the Identity framework and several things related to the Entity framework.

Let's get down to business. What needs to be done in order for everything to start on a new beta.

Project assembly

In the new ASP.NET, the entire assembly is batch - and the runtime (one of the runtimes) and all the dependencies are nuget packages. Therefore, in order to upgrade to a new version, you need to download a new runtime and change all old versions of packages to new ones.

The problem with using the new beta is that in the current version of Visual Studio 2015, an ASP.NET 5 project is created with packages of the previous beta by default. And in order to upgrade to a new version, just updating all the packages is not enough, moreover, it just breaks all the functionality, since beta5 has changes that are not compatible with beta4. Tracking such changes is quite simple, they are all published in a special project on the github. But with the implementation of them in a real project, difficulties may arise. Therefore, I decided to create a minimal beta5 application, making changes to the default beta4 project and leave it as a template for the future.

Creating a project in Visual Studio 2015

If you still do not have Visual Studio 2015, the candidate release can be downloaded at
www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs.aspx
By the way, the Community edition version with all the important functionality is available . The release of this version is less than a month later - on July 20, but ASP.NET 5 will not be included in it.

With the creation of the project, everything is standard - ASP.NET Web application ->



-> Web site



Let's see what we have at this stage and what we can do. We have created an ASP.NET 5 project with beta4 packages, on that runtime which is defaulted to dnvm. Now in parts, starting from the end. Dnvm command ( https://github.com/aspnet/dnvm) Is a .NET version manager, a command-line utility with which you can control .NET versions. The utility is installed together with Visual Studio 2015, or you can install it separately, with which on different operating systems, the details are github.com/aspnet/home and docs.asp.net/en/latest/getting-started/index.html .

If you run dnvm list in the console, this command will show you all versions of the runtime that you currently have installed.



Also, the default version of the runtime will be registered in the global.json file and for each project you can select the runtime and platform.



There are 2 platform options - full .net or the new Core version, which is distributed via nuget and which can be used on different platforms. In general, the whole concept of cross-platform .NET now uses the name DNX - from dot net x-plattform. In general, DNX is SDK + Runtime, details are docs.asp.net/en/latest/dnx/overview.html .

You can start the project, either in the old way - Ctrl + F5 in the studio, or in the new one, through the console, using the dnx command. web. But before that, you need to run dnx restore to sip packages (not only nuget, but also npm, bower) and dnx build to build the project. By default, after
dnx restore
dnx build
dnx. web
a mini web server will rise (http: // localhost: 5000) which will host your project. Commands that start on dnx. spelled out in project.json:

  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",
    "gen": "Microsoft.Framework.CodeGeneration",
    "ef":  "EntityFramework.Commands"
  },

If there are no problems with starting the initial project using different methods, you can move on. Among the problems - there are situations when packages are pulled “poorly” or the .net version in dnvm is not the same as the package version, in this situation you need to register all versions the same (for example, beta4) and try to restart the project again.

Beta5 migration

First, we need to upgrade the .NET version. To do this, do the following in the console:

set DNX_FEED=https://www.nuget.org/api/v2
dnvm upgrade

Now back to the project and make changes to it:
  • In the global.json file, write the beta5 version.
  • In the project.json file, all beta4 is changed to beta5.
  • In the project properties, we also select beta5.

We are waiting for the packages to pick up. As a result, we get the error:

Errors in c:\Users\vkot\documents\visual studio 14\Projects\aspnet5\src\aspnet5\project.json
    Unable to locate Microsoft.Framework.ConfigurationModel.UserSecrets >= 1.0.0-beta5
    Unable to locate Microsoft.Framework.ConfigurationModel.Json >= 1.0.0-beta5

We look in the list of incompatible changes (Breaking changes)
github.com/aspnet/Announcements/issues .
Find github.com/aspnet/Announcements/issues/25 .
We remove the word Model in these packages in the project.json file.
Go back to the console, go to the project folder, wound:
dnu restore
dnu build

The results of the changes github.com/gbdrm/aspnet5/commit/f4a89eb92f8b144795bec8eece6d3003df995455

Restore should work fine, but the project has not yet been built. If Restore does not work or more than 2 thousand errors in the project, try closing the project, updating dnvm again and then opening the project. If everything is done correctly, at least Restore should work.

Further, there are several problems with compilation.
First one:

PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

just delete the name of the last parameter, it looks like this parameter has been renamed.

The second configuration problem, you can look in more detail at:
github.com/aspnet/Announcements/issues/33
github.com/aspnet/Announcements/issues/26

The next problem is changes in Entity Framework 7, can be found at: github.com/aspnet / Announcements / issues / 35
To fix it, delete the entire contents of the Migrations folder and try to regenerate it using the Entity Framework commands. After deleting the files, the build should go without errors. After a successful build, we type in the console:

dnx . ef migration add init

And the Entity Framework is rebuilding migrations.

Change Results github.com/gbdrm/aspnet5/commit/b1f7f56f6c6f91416e4d97b1e17a840c2b61741c

It would seem - cheers! Builds! But the fact that it builds does not mean that it works. We are trying to run the project. First, I get an error on the line
app.UseBrowserLink ();
This feature is useful, but not critical, because I just deleted it for now.

Further:

The type or namespace name 'IOptions' could not be found

This is another incompatible change: github.com/aspnet/Announcements/issues/27
Just change the name from one to another.
We start ... and everything works!
Results of changes: github.com/gbdrm/aspnet5/commit/40d744099a7d110117d4c17fd6499a68c719ddb4

Most likely, after all these changes everything will work for you, but some specific problems are possible.

Add new functionality

Let's add some simple feature to see a few more points. Add a page where any user can add a message. To do this, we will have to add a new table to the database - messages. Let's describe a simple class:

    public class Message
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public string Date { get; set; }
        public string Time { get; set; }
        public string Author { get; set; }
    }

And also add it to the database context.

public DbSet Messages { get; set; }

In order to launch the application, two more things are missing now - configure the context (this is also a beta5 innovation) and add + apply migration to add a new label. In order to configure the database, you need to add the implementation of the OnConfiguring method to the context.

protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(Startup.Configuration["Data:DefaultConnection:ConnectionString"]);
        }

For me at the moment, the easiest way is to make the Configuration property of the Startup class static in order to get the configuration out of it. You can probably find a more flexible solution.

To work with migrations, at the moment you can only use the console commands of the Entity Framework 7. To do this, we need to make sure that the project is building and go to the console in the project folder. First, let's try the EF commands:

dnx . ef migration list

This command should display all current migrations. We still have one - init. If everything worked well, add a new migration - adding a Messages label.

dnx . ef migration add Messages
dnx . ef migration apply

These two teams should add the Messages table to our database.

Change results - github.com/gbdrm/aspnet5/commit/518fd2e3b6ab1c763be15ca6ae78eece2a630540

Add functionality

If everything is ready with the database - it's time to add the functionality itself. Let's start with the controller. Add a new method to HomeController that will return a view with messages. And the presentation itself with messages. The code is of no value, but if you're interested, you can see it - github.com/gbdrm/aspnet5/commit/ff0bc33d1d656cfd2db04a851a0c7c930e0a045c

The demo project is ready.

results


First of all, I really liked it. I just liked working in fact with the new stack. For example, if you forgot to rebuild - it's okay, now this is not necessary. Everything became somehow quicker, more convenient. Of course there is much more raw stuff, but now you can almost put up with it, and before the release everything should be very good.

I had some problems with authorization on another machine, so I still do not understand why, but most likely due to the fact that many versions of packages and runtimes are instructed on it and somewhere something did not fit. But while I was dealing with this problem, I downloaded the sorts of several projects from the github and just plugged them in instead of the packages and it all worked right away without any problems. It’s unrealistic to look at the details directly in the code of the framework itself, spending only a couple of minutes to put sorts instead of nuget packets. In general, the socialization of ASP.NET developers is very pleasing. Open source, weekly public stand-ups, a bunch of examples on the github, there is still a public chat where ASP.NET developers hang out - jabbr.net/#/rooms/AspNetvNext , etc.

In general, if you are interested in the asp.net 5 theme, then you can and should dig. Everything for this is ready and moreover, everything is very interesting.

Sources: github.com/gbdrm/aspnet5
Demo + test: aspdotnet.azurewebsites.net

Also popular now: