2017-09-25 143 views
0

刚刚启动了一个新的.NET Core 2.0应用程序,但出现奇怪的突然行为,似乎无法找到任何东西。打的.Run().NET Core 2.0 BasePath错误

以下错误弹出我的BuildWebHost().

System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase(). 
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.<BindAddressAsync>d__7.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.<BindAsync>d__2.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.<BindAsync>d__0.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.<StartAsync>d__21`1.MoveNext() 
'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.0\System.Diagnostics.Tools.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Exception","time":"2017-09-25T13:26:29.6923656Z","tags":{"ai.application.ver":"1.0.0.0","ai.internal.sdkVersion":"aspnet5c:2.1.1"},"data":{"baseType":"ExceptionData","baseData":{"ver":2,"properties":{"{OriginalFormat}":"Unable to start Kestrel.","CategoryName":"Microsoft.AspNetCore.Server.Kestrel","Exception":"System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase().\r\n at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.<BindAddressAsync>d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.<BindAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.<BindAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.<StartAsync>d__21`1.MoveNext()","AspNetCoreEnvironment":"Development","DeveloperMode":"true"},"exceptions":[{"id":47609378,"typeName":"System.InvalidOperationException","message":"Unable to start Kestrel.","hasFullStack":true,"parsedStack":[{"level":0,"method":"Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder+<BindAddressAsync>d__7.MoveNext","assembly":"Microsoft.AspNetCore.Server.Kestrel.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"},{"level":1,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":2,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":3,"method":"Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder+AddressesStrategy+<BindAsync>d__2.MoveNext","assembly":"Microsoft.AspNetCore.Server.Kestrel.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"},{"level":4,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":5,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":6,"method":"Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder+<BindAsync>d__0.MoveNext","assembly":"Microsoft.AspNetCore.Server.Kestrel.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"},{"level":7,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":8,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":9,"method":"Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer+<StartAsync>d__21`1.MoveNext","assembly":"Microsoft.AspNetCore.Server.Kestrel.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"}]}],"severityLevel":"Critical"}}} 
The thread 0x1d7c has exited with code 0 (0x0). 
The thread 0x25c0 has exited with code 0 (0x0). 
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll 
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll 
A path base can only be configured using IApplicationBuilder.UsePathBase(). 

这里是我的Program.cs

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     BuildWebHost(args).Run(); 
    } 

    public static IWebHost BuildWebHost(string[] args) => 
     WebHost.CreateDefaultBuilder(args) 
      .UseStartup<Startup>() 
      .Build(); 
} 

这里是我的Startup.cs

public class Startup 
{ 
    public Startup(IConfiguration configuration) 
    { 
     Configuration = configuration; 
    } 

    public IConfiguration Configuration { get; set; } 

    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddMvc(); 

     //Swagger 
     services.AddSwaggerGen(c => 
     { 
      c.SwaggerDoc("v1", new Info 
      { 
       Title = "2Commit Blogpost API", 
       Version = "v1" 
      }); 
     }); 

     //Mediatr 
     services.AddScoped<IMediator, Mediator>(); 
     services.AddTransient<SingleInstanceFactory>(sp => sp.GetService); 
     services.AddTransient<MultiInstanceFactory>(sp => sp.GetServices); 
     services.AddMediatorHandlers(typeof(Startup).Assembly); 

     //MongoDB 
     services.Configure<MongoSettings>(s => 
     { 
      s.Database = Configuration.GetSection("MongoConnection:Database").Value; 
     }); 
     services.AddSingleton<IMongoClient, MongoClient>(client => new MongoClient(Configuration.GetSection("MongoConnection:ConnectionString").Value)); 

     //BL 
     services.AddTransient<IUserService, UserService>(); 

     //DAL 
     services.AddTransient<IRepository, MongoRepository>(); 
    } 

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
    { 
     //Swagger 
     app.UseSwagger(); 
     app.UseSwaggerUI(c => 
     { 
      c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlogPost API"); 
     }); 

     app.UseMvc(); 
    } 
} 

灿” t似乎把我的头围绕着这个!

更新1:

运行在命令行中我的项目时,它运行IIS槽快速,没有错误时使用错误,只弹出。

这里是我的launchSettings

{ 
    "iisSettings": { 
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": { 
     "applicationUrl": "http://localhost:51592/swagger", 
     "sslPort": 0 
    } 
    }, 
    "profiles": { 
    "IIS Express": { 
     "commandName": "IISExpress", 
     "launchBrowser": true, 
     "environmentVariables": { 
     "ASPNETCORE_ENVIRONMENT": "Development" 
     } 
    }, 
    "API": { 
     "commandName": "Project", 
     "launchBrowser": true, 
     "environmentVariables": { 
     "ASPNETCORE_ENVIRONMENT": "Development" 
     }, 
     "applicationUrl": "http://localhost:5000/swagger" 
    } 
    } 
} 
+0

虽然没有关系。您应该在BL中拥有一个等效的配置服务,并将引用转换为DAL。使用当前的设置,您可以直接从上层访问DAL,这可以打开BL层的潜力。理想情况下,顶层不应该知道有关DAL的任何信息。 – bilpor

+0

这实际上是我现在困扰了我的一段时间,但没有花时间去考虑它。你有一个具体的例子,我应该如何将我的BL <-> DAL的东西变成m BL? – TanguyB

+0

我这样做,我会尽力挖掘出来.....我没有在我目前所在的网站上。但我会告诉你我是如何做到这一点的更新。 – bilpor

回答

4

OK,在1.X你可以在你的launchOptions从命令行运行时,只需添加到您的applicationUrl一个基本路径,现在只有使用IIS时的作品。

在我launchSettings,改变了我的CLI配置文件使用

"applicationUrl": "http://localhost:5000/swagger"

"applicationUrl": "http://localhost:5000"

的伎俩。

+0

您也可以继续从http:// localhost:5000/swagger进行访问,方法是进行上述更改,然后将以下内容添加到Startup.cs的配置部分: app.UsePathBase(“/ swagger”)。UseMvc (); –

+0

好吧谢谢你:) – TanguyB