运行dnx kestrel时,出现下面的错误:
System.InvalidOperationException: No service for type 'Microsoft.Dnx.Runtime.IApplicationEnvironment' has been registered. at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.AspNet.Hosting.Program.Main(String[] args) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Dnx.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider serviceProvider) at Microsoft.Dnx.ApplicationHost.Program.<>c__DisplayClass3_0.<ExecuteMain>b__0() at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute()
请问如何解决这个问题?
删除 ~/.dnx/packages 中所有的包包,dnu restore之后,继续运行dnu kestrel,错误变为:
The type or namespace name 'IApplicationEnvironment' could not be found (are you missing a using directive or an assembly reference?)
在 Startup.cs 中添加 Microsoft.Extensions.PlatformAbstractions 的命名空间之后,问题解决
using Microsoft.Extensions.PlatformAbstractions;
原因是发布时,没有正确选择dnx runtime版本。
解决办法:1.确认项目所用.NET Core版本
2.在cmd下输入 dnvm list ,确认装有该版本运行时
3.在解决方案Solution Items下的global.json里面添加
"sdk": {
"version": "1.0.0-rc1-final" //运行时版本
}