Banketeshvar Narayan
Banketeshvar Narayan

Reputation: 3899

Can I build ASP.NET Core web application using Microsoft Dotnet CLI?

I have gone through some websites and found that .NET Core is using .NET CLI (.NET Command Line Interface). I have create a console application using .NET CLI and it is working fine

But when I am creating an ASP.NET Core web application using Yeoman and running the command “dotnet restore” then I am getting the following error:

Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01 is not compatible with DNXCore,Version=v5.0.

I also tried

dotnet restore -s https://api.nuget.org/v3/index.json

But getting the same error.

If I run “dnu restore” then it is working fine so my question is can I use .NET CLI for web application too or it is only limited to console application?

I have gone through the links

https://github.com/dotnet/cli & http://dotnet.github.io/getting-started/

but did not find any details that it supports ASP.NET Core web app or not?

Upvotes: 3

Views: 4887

Answers (3)

Andrei
Andrei

Reputation: 2332

Now I don't know what your exact error is, but this might be helpful:

Basically, you need to add the appropriate imports to your project.json:

    "frameworks": {
        "dnxcore50": { 
            "imports": "portable-net451+win8"
        }
    }

(As posted here: https://github.com/aspnet/Home/issues/1265)

These were my errors:

Errors in /webserver/project.json
    Package Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01 is not compatible with dnxcore50 (DNXCore,Version=v5.0). Package Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01 supports:
      - net45 (.NETFramework,Version=v4.5)
      - portable-net45+win8 (.NETPortable,Version=v0.0,Profile=Profile7)
    Package Microsoft.CodeAnalysis.Common 1.1.0-rc1-20151109-01 is not compatible with dnxcore50 (DNXCore,Version=v5.0). Package Microsoft.CodeAnalysis.Common 1.1.0-rc1-20151109-01 supports:
      - net45 (.NETFramework,Version=v4.5)
      - portable-net45+win8 (.NETPortable,Version=v0.0,Profile=Profile7)
    One or more packages are incompatible with DNXCore,Version=v5.0.

I generated the .Net Web App using the Swagger Code generator from http://editor.swagger.io/#/

After adding the imports, dotnet restore worked without problems.

Upvotes: 0

Shaun Luttin
Shaun Luttin

Reputation: 141492

Short Answer

For ASP.NET Core rc1, we use dnx and dnu.

For ASP.NET Core rc2, we use dotnet.

Longer Answer

I have gone through some websites and found that .NET Core is using .NET CLI (.NET Command Line Interface). I have create a console application using .NET CLI and it is working fine.

ASP.NET Core rc2 is also a console application. That is, it is a console application that starts the ASP.NET hosting layer.

But when I am creating an ASP.NET Core web application using Yeoman and running the command “dotnet restore” then I am getting the following error...

My sense is that you are running these commands:

yo aspnet
? What type of application do you want to create? Web Application
? What's the name of your ASP.NET application? MyApp

This creates an ASP.NET Core rc1 project. You can see this by looking at the resultant project.json file dependencies. They all say rc1-final. Result: you cannot build these with the dotnet command line. You still have to use the dnu build and the dnx <command> formulas.

...can I use .NET CLI for web application[s] too or it is only limited to console application?

In ASP.NET Core rc1, the dnx.exe made a call to new WebHostBuilder(), so we need to use dnx to start our web application. In ASP.NET Core rc2, the console application that contains our web app makes its own call to new WebHostBuilder(), so we can use the dotnet command line interface to start the app. So...

  • No you cannot use the dotnet CLI for ASP.NET Core rc1 Web Applications, because dnx.exe loads the ASP.NET Hosting Layer.
  • Yes you can use it for ASP.NET Core rc2 Web Applications, because your console application loads the ASP.NET Hosting Layer itself.

Upvotes: 3

Tseng
Tseng

Reputation: 64150

I've spent a good hour on toying around with it and I got the "Welcome page" running with it.

As I suspected, you tried to use dotnet-cli with your dnx styled project and you you used the wrong nuget feed (the official one, rather than the nightly myget feeds).

For this demo project, just create a new folder and run dotnet new. This creates three files: NuGet.Config, project.json and Program.cs. You can delete the later one and just create a Startup.cs from below.

Startup.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace AspNetCoreCliDemo
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseIISPlatformHandler();
            app.UseWelcomePage();
        }

        // This doesn't work right now, as it can't resolve WebApplication type
        //public static void Main(string[] args) => WebApplication.Run<Startup>(args);

        // Entry point for the application.
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                        .UseDefaultConfiguration(args)
                        .UseServer("Microsoft.AspNetCore.Server.Kestrel")
                        .UseIISPlatformHandlerUrl()
                        .UseStartup<Startup>()
                        .Build();

            host.Run();
        }
    }
}

project.json:

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "NETStandard.Library": "1.0.0-rc2-*",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-*",
    "Microsoft.AspNetCore.IISPlatformHandler": "1.0.0-rc2-*",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-*"
  },

  "frameworks": {
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

Note: Only dnxcore moniker is supported as of now.

Last but not least, the NuGet.Config that worked in my case:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="cli-deps" value="https://dotnet.myget.org/F/cli-deps/api/v3/index.json" />
    <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
    <add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

I didn't had the success with the two default feeds (api.nuget.org and dotnet-core) as it couldn't resolve a few dependencies. After adding the "cli-deps" feed, all packages were resolved and dotnet run worked. It will listen on the "http://localhost:5000" port and serve the welcome page.

You may get an error message about having more than one entry point, that's because you got a Main method in both Program.cs and Startup.cs. Just delete the Program.cs.

This should serve as an entry point.

dotnet-cli as of now doesn't support commands yet (ones formerly defined in the project.json file).

Upvotes: 6

Related Questions