[英]dotnet quit unexpectedly - after creating new project in ASP.NET Core
I'm getting this warning while creating new MVC project using ASP.NET Core (both 3.1 version and 5.0 version):我在使用 ASP.NET Core(3.1 版和 5.0 版)创建新的 MVC 项目时收到此警告:
do.net quit unexpectedly
do.net 意外退出
Because the warning description does show me where the problem comes from, so I don't know where to start.因为警告描述确实告诉我问题出在哪里,所以我不知道从哪里开始。
This is my Startup.cs
file:这是我的
Startup.cs
文件:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
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, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
Am I missing something?我错过了什么吗?
There are multiple cases to get this warning.有多种情况会收到此警告。 Some problem can be fixed inside the method
ConfigureServices
in the file Startup.cs
.可以在文件
Startup.cs
的ConfigureServices
方法中修复一些问题。
If you are using Visual Studio, you can open Application Output
tab (View -> Other Windows -> Application Output) .如果您使用的是 Visual Studio,则可以打开
Application Output
选项卡(View -> Other Windows -> Application Output) 。
If you've got some message, such as:如果您收到一些消息,例如:
Unhandled exception.
未处理的异常。 System.InvalidOperationException: Unable to find the required services.
System.InvalidOperationException:无法找到所需的服务。 Please add all the required services by calling 'IServiceCollection.AddAuthorization' inside the call to 'ConfigureServices(...)' in the application startup code.
请通过在应用程序启动代码中对“ConfigureServices(...)”的调用中调用“IServiceCollection.AddAuthorization”来添加所有必需的服务。
Fixed: Calling services.AddAuthorization();
修复:调用
services.AddAuthorization();
method inside the ConfigureServices
method. ConfigureServices
方法中的方法。
Note: It's different from services.AddAuthentication();
注意:它不同于
services.AddAuthentication();
(this service can be used when you want to enable some services like: Login with Facebook/Google/Twitter...) . (当您想启用某些服务时可以使用此服务,例如:使用 Facebook/Google/Twitter 登录...) 。
Unhandled exception.
未处理的异常。 System.InvalidOperationException: Unable to find the required services.
System.InvalidOperationException:无法找到所需的服务。 Please add all the required services by calling 'IServiceCollection.AddControllers' inside the call to 'ConfigureServices(...)' in the application startup code.
请通过在应用程序启动代码中对“ConfigureServices(...)”的调用中调用“IServiceCollection.AddControllers”来添加所有必需的服务。
Fixed: Calling services.AddControllersWithViews();
修复:调用
services.AddControllersWithViews();
or services.AddControllers();
或
services.AddControllers();
service inside that method to solve the problem.服务里面的那个方法来解决问题。
Unhandled exception.
未处理的异常。 System.InvalidOperationException: Unable to find the required services.
System.InvalidOperationException:无法找到所需的服务。 Please add all the required services by calling 'IServiceCollection.AddRazorPages' inside the call to 'ConfigureServices(...)' in the application startup code.
请通过在应用程序启动代码中对“ConfigureServices(...)”的调用中调用“IServiceCollection.AddRazorPages”来添加所有必需的服务。
This error ocurrs when you enable to use Razor Pages .当您启用使用Razor 页面时会出现此错误。 To solve this problem, you need to add
services.AddRazorPages();
要解决这个问题,需要添加
services.AddRazorPages();
service.服务。
An unhandled exception has occurred while executing the request.
执行请求时发生未处理的异常。
System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.
System.InvalidOperationException:没有注册类型为“Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]”的服务。
You've got this error when missing to add identity service.缺少添加身份服务时出现此错误。 This can be done by one of these ways:
这可以通过以下方式之一完成:
services.AddDefaultIdentity<IdentityUser>(options =>
{
//options.Password.RequireDigit = false;
//options.Password.RequiredLength = 8;
//options.Password.RequireLowercase = false;
//options.Password.RequireNonAlphanumeric = false;
//options.Password.RequireUppercase = false;
}).AddEntityFrameworkStores<ApplicationDbContext>();
or要么
services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
//options.Password.RequireDigit = false;
//options.Password.RequiredLength = 8;
//options.Password.RequireLowercase = false;
//options.Password.RequireNonAlphanumeric = false;
//options.Password.RequireUppercase = false;
}).AddEntityFrameworkStores<ApplicationDbContext>();
If you want to extend IdentityUser
class to add more property like LastSignedIn
:如果您想扩展
IdentityUser
class 以添加更多属性,例如LastSignedIn
:
public class User : IdentityUser
{
public DateTimeOffset LastSignedIn { get; set; }
}
you can replace IdentityUser
with User
when adding identity service:添加身份服务时,您可以将
IdentityUser
替换为User
:
services.AddIdentity<User, IdentityRole>(options => {})
.AddEntityFrameworkStores<ApplicationDbContext>();
Unhandled exception.
未处理的异常。 System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory
1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory
1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'X.Data.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager
System.AggregateException:无法构建某些服务(验证服务描述符时出错'ServiceType:Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory
1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory
1[Microsoft.AspNetCore.Identity.IdentityUser]':在尝试激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String]、Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String ]]'。)(验证服务描述符时出错'ServiceType:Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager
1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager
1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'X.Data.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'X.Data.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore
6[Microsoft.AspNetCore.Identity.IdentityUse1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager
1 [Microsoft.AspNetCore.Identity.IdentityUser]':尝试激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String]、Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String]]'。)(验证服务描述符“ServiceType:Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime:Scoped ImplementationType:Microsoft.AspNetCore.Identity.SecurityStampValidator1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'X.Data.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore
r,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator
1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'X.Data.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String]]'.) (Error while validating the service descrr,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator
1 [Microsoft.AspNetCore.Identity.IdentityUser]':无法在尝试激活 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
] 时解析类型 'X.Data.ApplicationDbContext' 的服务6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String]]'。)(验证服务描述时出错iptor 'ServiceType: Microsoft.AspNetCore.Identity.SignInManager1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager
1[Microsoft.AspNetCore.Identity.IdentityUser]': Unable to resolve service for type 'X.Data.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore
6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbCoiptor 'ServiceType: Microsoft.AspNetCore.Identity.SignInManager
1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager
1[Microsoft.AspNetCore.Identity.IdentityUser]': 无法解析服务在尝试激活 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1 时输入'X.Data.ApplicationDbContext' [System.String]、Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String]]'。)(验证服务描述符 'ServiceType: Microsoft.AspNetCore 时出错.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore
6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbCo ntext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String]]': Unable to resolve service for type 'X.Data.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore
6[Microsoft.AspNetCore.Identity.IdentityUser,X.Data.ApplicationDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken`1[System.String]]'.)ntext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String]]': Unable to resolve service for type 'X.Data.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore
.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken`1[System.String]]'。)
This error ocurrs when you forget to enabling DbContext
service:当您忘记启用
DbContext
服务时会出现此错误:
// in this example, we use "Sqlite" to manage connection
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
If you want to use SQL Server instead of Sqlite, you can add Microsoft.EntityFrameworkCore.SqlServer NPM package:如果想使用SQL Server代替Sqlite,可以添加Microsoft.EntityFrameworkCore.SqlServer NPM package:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
If you define ApplicationDbContext
class in the class library project (Namespace: Your_main_project_namespace.Data) , when you refer it to the main project, you need to call MigrationsAssembly
method with the namspace name of the main project:如果在 class 库项目中定义了
ApplicationDbContext
class (Namespace: Your_main_project_namespace.Data) ,当你将其引用到主项目时,你需要使用主项目的命名空间名称调用MigrationsAssembly
方法:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
actions => actions.MigrationsAssembly("Your_main_project_namespace")));
Note: The current version (5.0.0) of this package is applied to net5.0 , so if you're using netcoreapp3.1 , you can use version 3.1.10 instead.注意:此 package 的当前版本 (5.0.0) 适用于net5.0 ,因此如果您使用的是netcoreapp3.1 ,则可以改用版本 3.1.10。 Don't try to upgrade to the lastest, it will crash your project.
不要尝试升级到最新版本,它会使您的项目崩溃。 Such as: removing/replacing some obsolete method/class/interface... (
IWebHostEnvironment
vs IHostingEnvironment
...) .如:删除/替换一些过时的方法/类/接口... (
IWebHostEnvironment
vs IHostingEnvironment
...) 。
Value cannot be null. (Parameter 'connectionString')
值不能是 null。(参数“connectionString”)
This error may come from your connection string name in the file appsettings.json
:此错误可能来自文件
appsettings.json
中的连接字符串名称:
{
"ConnectionStrings": {
"MyConnection": "DataSource=app.db;Cache=Shared"
}
}
In this example, we name the connection string with the name MyConnection
, so when we try to get:在此示例中,我们将连接字符串命名为
MyConnection
,因此当我们尝试获取时:
Configuration.GetConnectionString("DefaultConnection")
the exception will be thrown because there is no property name DefaultConnection
.将抛出异常,因为没有属性名称
DefaultConnection
。
This problem may come from the way to append a large of data files to project via copy-paste method using Finder (on Mac) or Explorer (on Windows).这个问题可能来自 append 使用 Finder(在 Mac 上)或资源管理器(在 Windows 上)通过复制粘贴方法投影大量数据文件的方式。 The general case is: Coping to
wwwroot
folder:一般情况是:应对
wwwroot
文件夹:
When Visual Studio is opening and some files are copied to wwwroot
folder (or somewhere else in the project) successful, the project will be saved automatically.当 Visual Studio 打开并且某些文件成功复制到
wwwroot
文件夹(或项目中的其他位置)时,项目将自动保存。 There is no way to stop it.没有办法阻止它。
And the remaining time will be calculated based on:剩余时间将根据以下条件计算:
So, be careful if you want to copy more than 5000 images or videos to wwwroot
folder.因此,如果要将超过 5000 个图像或视频复制到
wwwroot
文件夹,请小心。 If something goes wrong (I don't even know where the error comes from) , you cannot run the project.如果出现问题(我什至不知道错误来自哪里) ,则无法运行该项目。 In this case, I suggest:
在这种情况下,我建议:
Edit Project File
to edit .csproj
file.Edit Project File
以编辑.csproj
文件。 In this file, if you catch some implementation like this:<ItemGroup>
<Content Include="wwwroot\">
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</Content>
</ItemGroup>
The tag <ItemGroup>
which contains <Content>
tag, and the <Content>
tag mentions about wwwroot
folder or some folder name that inherits from wwwroot
folder. <ItemGroup>
标签包含<Content>
标签, <Content>
标签提到wwwroot
文件夹或从wwwroot
文件夹继承的一些文件夹名称。 DELETE all of them (including the parent tag: <ItemGroup>
), save the file and wait until the file is saved/all packages are restored.删除所有这些(包括父标记:
<ItemGroup>
),保存文件并等待文件保存/所有包恢复。
Exclude From Project
.Exclude From Project
。 Then waiting until Visual Studio completes the action, and right click the folder to choose Include From Project
again.Include From Project
。 Waiting action is required (the time to exclude/include may the same).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.