c# - .NET Core 2.0 带路由的最小 WebHost

标签 c# .net asp.net-core asp.net-core-mvc hosting

我正在尝试创建一个针对 netcoreapp2.0 的绝对最小的 WebHost。 我一直在引用这个但没有成功:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x

using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Http;

namespace netcore_pi_project
{
    class Program
    {

        public static void Main(string[] args)
        {
            BuildWebHost(args);

            using (var host = WebHost.Start(router => router
                .MapGet("volu", (req, res, data) => 
                    res.WriteAsync($"ps aux".Bash()))
                .MapGet("vold", (req, res, data) => 
                    res.WriteAsync($"ps aux".Bash()))))
            {
                Console.WriteLine("Core Volume project running..");
                host.WaitForShutdown();
            }
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseUrls("http://localhost:5000")
                .Build();
    }
}

此构建但不会运行:

Unhandled Exception: System.ArgumentException: A valid non-empty application name must be provided. Parameter name: applicationName at Microsoft.AspNetCore.Hosting.Internal.HostingEnvironmentExtensions.Initialize(IHostingEnvironment hostingEnvironment, String applicationName, String contentRootPath, WebHostOptions options) at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors) at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at netcore_pi_project.Program.BuildWebHost(String[] args) in /Users/owe-macpro/projects/netcore-pi-project/Program.cs:line 31 at netcore_pi_project.Program.Main(String[] args) in /Users/owe-macpro/projects/netcore-pi-project/Program.cs:line 17

最佳答案

您问题的答案在 same page you linked 上:

Troubleshooting System.ArgumentException

Applies to ASP.NET Core 2.0 Only

If you build the host by injecting IStartup directly into the dependency injection container rather than calling UseStartup or Configure, you may encounter the following error: Unhandled Exception: System.ArgumentException: A valid non-empty application name must be provided.

This occurs because the applicationName(ApplicationKey) (the current assembly) is required to scan for HostingStartupAttributes. If you manually inject IStartup into the dependency injection container, add the following call to your WebHostBuilder with the assembly name specified:

WebHost.CreateDefaultBuilder(args)
    .UseSetting("applicationName", "<Assembly Name>")
    ...

Alternatively, add a dummy Configure to your WebHostBuilder, which sets the applicationName(ApplicationKey) automatically:

WebHost.CreateDefaultBuilder(args)
    .Configure(_ => { })
    ...

NOTE: This is only required with the ASP.NET Core 2.0 release and only when you don't call UseStartup or Configure.

关于c# - .NET Core 2.0 带路由的最小 WebHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46575387/

相关文章:

c# - 在 AspNET Core 中使用 DI

c# - LinkedIn 共享内容选项,如 facebook 请求对话框

c# - 删除应用程序设置条目

asp.net-core - Modelbinder 在返回 null 时默认为 0 而不是 Controller 中设置的值

c# - 运行 SQL 命令并返回消息

.net - 在 .net 中绘制科学数据

javascript - 来自 AJAX 的 .NET Core Controller 输入始终为 null

c# - Release模式下的调试符号

c# - UWP BitmapImage 到流

c# - net.core 应用程序的远程调试不起作用