首页 文章

我可以使用Microsoft Dotnet CLI构建ASP.NET Core Web应用程序吗?

提问于
浏览
3

我浏览了一些网站,发现.NET Core正在使用 .NET CLI (.NET命令行界面) . 我使用.NET CLI创建了一个控制台应用程序,它运行正常

但是当我使用Yeoman创建一个ASP.NET Core Web应用程序并运行命令“dotnet restore”时,我收到以下错误:

Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01与DNXCore不兼容,版本= v5.0 .

我也试过了

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

但得到同样的错误 .

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?

我已经通过了链接

https://github.com/dotnet/clihttp://dotnet.github.io/getting-started/

但是没有找到它支持ASP.NET Core web app的任何细节?

3 回答

  • 3

    我花了一个小时的时间来玩它,我得到了“欢迎页面” .

    正如我所怀疑的那样,你试图将dotnet-cli与你的dnx风格的项目一起使用,你使用了错误的nuget feed(官方的,而不是每晚的myget feed) .

    对于此演示项目,只需创建一个新文件夹并运行 dotnet new . 这将创建三个文件: NuGet.Configproject.jsonProgram.cs . 您可以删除后一个,只需从下面创建一个 Startup.cs .

    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: 截至目前仅支持 dnxcore 名字对象 .

    最后但并非最不重要的是, NuGet.Config 在我的案例中起作用:

    <?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>
    

    我没有't had the success with the two default feeds (api.nuget.org and dotnet-core) as it couldn'解决一些依赖 . 添加"cli-deps" Feed后,所有包都已解析并且 dotnet run 正常工作 . 它将侦听“http://localhost:5000”端口并提供欢迎页面 .

    您可能会收到有关多个入口点的错误消息,这是因为您在 Program.csStartup.cs 中都有 Main 方法 . 只需删除 Program.cs 即可 .

    这应该作为切入点 .

    dotnet-cli 截至目前还不支持命令(之前在 project.json 文件中定义的命令) .

  • 7

    简答

    对于ASP.NET Core rc1,我们使用 dnxdnu .

    对于ASP.NET Core rc2,我们使用 dotnet.

    更长的答案

    我浏览了一些网站,发现.NET Core正在使用.NET CLI(.NET命令行界面) . 我使用.NET CLI创建了一个控制台应用程序,它运行正常 .

    ASP.NET Core rc2也是一个控制台应用程序 . 也就是说,它是一个启动ASP.NET托管层的控制台应用程序 .

    但是当我使用Yeoman创建ASP.NET Core Web应用程序并运行命令“dotnet restore”时,我收到以下错误...

    我的感觉是你正在运行这些命令:

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

    这将创建一个ASP.NET Core rc1项目 . 您可以通过查看生成的project.json文件依赖项来查看此信息 . 他们都说 rc1-final . 结果:您无法使用 dotnet 命令行构建它们 . 您仍然必须使用 dnu builddnx <command> 公式 .

    ...我可以将.NET CLI用于Web应用程序[s],还是仅限于控制台应用程序?

    在ASP.NET Core rc1中, dnx.exe 调用 new WebHostBuilder() ,因此我们需要使用 dnx 来启动我们的Web应用程序 . 在ASP.NET Core rc2中,包含我们的Web应用程序的控制台应用程序自己调用 new WebHostBuilder() ,因此我们可以使用 dotnet 命令行界面来启动应用程序 . 所以...

    • 不,您不能将 dotnet CLI用于ASP.NET Core rc1 Web应用程序,因为 dnx.exe 加载了ASP.NET主机层 .

    • 是的,您可以将它用于ASP.NET Core rc2 Web应用程序,因为您的控制台应用程序会加载ASP.NET主机层本身 .

  • 0

    现在我不知道你的确切错误是什么,但这可能会有所帮助:

    基本上,您需要将适当的导入添加到project.json:

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

    (如此发布:https://github.com/aspnet/Home/issues/1265

    这些是我的错误:

    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.
    

    我使用来自http://editor.swagger.io/#/的Swagger Code生成器生成.Net Web App

    添加导入后, dotnet restore 工作没有问题 .

相关问题