OdeToCode IC Logo

Entry Points for ASP.NET 5 Commands

Monday, March 30, 2015

Inside the project.json file for an ASP.NET 5 application, you can find a commands section:

"commands": {
    "gen": "Microsoft.Framework.CodeGeneration",
    "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5004",
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5002"
},

You can execute these commands from a command prompt with the .NET Execution Environment (dnx, formerly named k), which will hunt for a standard entry point in the assembly referenced by the command value, and also pass along any parameters in the command value.

In other words, “dnx web” will look in the Microsoft.AspNet.Hosting assembly for a main entry point, and then pass the –server and –server.urls parameters into the entry point.

Typical commands you’ll see in new projects include commands to spin up the application in a web server, or execute database migrations.

You can also create your own command hosts using the “ASP.NET 5 Console Application” template, or, you can add a Program class with a Main method inside an existing web application.

public class Program
{
    public Program(ILibraryManager libraryManager)
    {
        foreach(var lib in libraryManager.GetLibraries())
        {
            Console.WriteLine(lib.Name);
        }
    }

    public void Main(string[] args)
    {
        var config = new Configuration()
                        .AddJsonFile("config.json")
                        .AddCommandLine(args);

        Console.WriteLine(config.Get("message"));

        foreach (var arg in args)
        {
            Console.WriteLine(arg);
        }

        Console.ReadLine();
    }
}

Notice how the Main method doesn’t require the static keyword, in fact the dnx will instantiate the Program class itself if possible, and even inject dependencies. You can ask for IServiceManifest to see all the services available at startup. In the above code, I’ve asked for an ILibraryManager, and I can use the manager to list out all the dependencies for the application.

The ASP.NET 5 configuration system works for this scenario, too. The above code sets up config.json as the first configuration source. If we set up the configuration file with a message property:

{
    "message":  "Hello!"
}

… then running the application will display the message “Hello!”.

We can override the message by specifying a command line value in the project.json file.

"commands": {
    "me" : "TestApp --message=Goodbye"
}

In this case, “me” is the name of the command and “TestApp” is the name of the web application. Running dnx me from the command line will display the string “Goodbye”. You can also override the command line parameters specified in the project commands by passing parameters to dnx. In other words, “dnx me --message=Allo!” will print the string “Allo!”

Although not the most exciting feature of ASP.NET 5, commands do open up some interesting possibilities for hosting and executing utility functions, data migrations, and scheduled tasks.