ASP.NET vNext MVC6 Part 2

In this part we will try to set up aspnet vNext startup and their dependencies setup and bit talk about package manager and version manager which is the major part of visual studio 2015 and aspnet vNext, and talk about self-host server, I have used beta 3.

Before we start, we need to talk about ‘K’

“Project K” is the codename for a set of new components powering ASP.NET vNext.

There are three main commands to be aware of:

  1. K – Used to bootstrap up the KRE – gets your app up and running.
  2. KVM – K Version Manager, for managing individual versions of the runtime.
  3. KPM – K Package Manager, for managing packages that your application depends upon.

There are similarities with node package manager (npm) and ruby version manager (rvm).
May be the Microsoft team will change these name closer to final release.

Let’s get up and running

To get started using K, we need to first get the version manager. As per the guidelines on the ASPNET vNext Github repository page, execute the following from an admin cmd prompt:

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('<a href="https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.ps1" title="https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.ps1" target="_blank">https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.ps1</a>'))"

Once kvm is installed we can use this to install the K runtime itself.

kvm install latest -p

The -p switch will persist this version of the runtime to your path. For the verification that kvm is install or not you can check here

C:\User\Jitendra\.k 

folder(Its my directory check your ) or you can open command prompt and run kvm.

And good thing is that actually you don’t need to go on command prompt every time you can use package manager console of visual studio, for this you need to this command on package manager console.

cd .\src\{your_project_name}

Our environment is now ready to go.

Startup.cs and project.json

This is one of the biggest changes of vNext has been introduced in day-to-day usage. These two files replace a whole host of cruft that was in the previous project systems that Visual Studio would have to manage for you.

  1. .sln solution files are gone
  2. .csproj files are gone
  3. global.asax is gone
  4. assembly.cs is gone
  5. web.config is gone
  6. packages.config is gone

(Note: There is a new kproj file, that is being introduced but that is a Visual Studio, it’s not something your project actually needs.

project.json

Add new valid project.json file with the following contents:

{ }

First, we’ll need a web server. vNext doesn’t just assume you’ll be running on IIS so we’ll bring our own. On Windows we can use WebListener to self-host our site. Run the following two commands:

kpm install Microsoft.AspNet.Hosting 1.0.0-beta2  
kpm install Microsoft.AspNet.Server.WebListener 1.0.0-beta2  

We’re using the K Package Manager to install two dependencies (with their versions at 1.0.0-beta3). These dependencies are NuGet packages, by default package source have api.nuget link which you need to disable from visual studio tool Choose Tools > Nuget Package Manager > Package Source

If you look at your project.json file now, you’ll notice a new dependencies object this is your references.

{
    "dependencies": {
        "Microsoft.AspNet.Hosting": "1.0.0-beta2",
        "Microsoft.AspNet.Server.WebListener": "1.0.0-beta2"
    }
}

You can just as easily edit this file yourself with a text editor. There is no magic going on here.

Aside from updating your project.json file, kpm install will also download the packages. They will come down from nuget.org and be stored in your %userprofile%.kpm\packages. You’ll no longer have manage many copies of the same NuGet packages used in all your projects sprinkled about your hard drive, they are all centralized in this one spot.

Commands

Commands are a way to get K to conveniently do our request. In this case, we need it to boot up the web server. Add the following snippet into your project.json file

"commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls <a href="http://localhost:5000" title="http://localhost:5000" target="_blank">http://localhost:5000</a>"
    }

We’re registering the command web (You can change the name web) which when called will execute Microsoft.AspNet.Hosting. From the NuGet package description:

ASP.NET 5 core hosting infrastructure and startup logic for web applications.

We’re passing in two parameters, the –server to use: Microsoft.AspNet.Server.WebListener

ASP.NET 5 self-host web server.

And the URL to listen on http://localhost:5000.

These are the two packages that we installed in the previous step.

Here’s what our final project.json file looks like now:

{
  "dependencies": {
    "Microsoft.AspNet.Hosting": "1.0.0-beta2",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta2"
  },
  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls <a href="http://localhost:5000" title="http://localhost:5000" target="_blank">http://localhost:5000</a>"
  }
}

Commands are used deeply in vNext. Entity Framework Migrations, MVC Code Generation, etc. all of which previously used UI in Visual Studio are now handled via commands. Again, Visual Studio will still offer UI for these things, but you’re no longer reliant on it.

Now run it!

k web 

This runs the web command we defined previously in project.json. And we get an exception: Startup class not found in assembly…

Startup.cs
This is the entry point for your application and what WebListener is trying to find to get your app booted. Basic example to return a result:

using Microsoft.AspNet.Builder;  
using Microsoft.AspNet.Http;

public class Startup  
{
    public void Configure(IApplicationBuilder application)
    {
        application.Run(context =&gt; context.Response.WriteAsync("Working Hello World"));
    }
}
 

The Startup class is special. The framework is hardwired to look for such a class and furthermore, it’s configured to call certain methods on that class. Configure is one such method.

Now run k web again and with any luck, it should report Started. Open a web browser and head to http://localhost:5000/ to see Working Hello World.

I will be working on to deploy the code as will.

Summary:
In this article I tried to explain how to setup aspnet vnext with visual studio 2015. I would like to have feedback and question from my blog reads, please post your comment and question in order to make it more useful.

One thought on “ASP.NET vNext MVC6 Part 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s