简体   繁体   English

设置DBContext时出现ASP.NET Core 2.2错误(在Kestrel中起作用,不在IIS Express中起作用)

[英]ASP.NET Core 2.2 error when setting up DBContext (works in Kestrel, not in IIS Express)

I have a .Net Core 2.2 web project where I set up the dbcontext connections in the Startup.cs file: 我有一个.Net Core 2.2 Web项目,其中在Startup.cs文件中设置了dbcontext连接:

services.AddDbContext<FirmDirectoryContext>(options =>  
              options.UseSqlServer(Configuration.GetConnectionString("DefaultDatabase")));

When I run this is Kestrel it works, but if I try to run it using IIS Express I get the following: 当我运行Kestrel时,它可以工作,但是如果尝试使用IIS Express运行它,则会得到以下信息:

{System.ArgumentNullException: Value cannot be null. {System.ArgumentNullException:值不能为null。 Parameter name: connectionString 参数名称:connectionString

The connection string is obviously set in the appsettings.json file, or Kestrel would fail as well? 连接字符串显然是在appsettings.json文件中设置的,否则Kestrel也将失败?

I'm not sure how your appsettings.json looks like, but you should have a section in your json file like this : 我不确定您的appsettings.json看起来如何,但是您应该在json文件中像这样设置一个部分:

"ConnectionStrings": {
    "DefaultDatabase": "<Your connection string>"
}

If your environment is set as development, dotnet gets connection string from appsettings.development.json. 如果您的环境设置为开发,则dotnet从appsettings.development.json获取连接字符串。

Make sure you have set connection string there as well. 确保在那里也设置了连接字符串。

Looks like the problem was in my Program.cs file. 看来问题出在我的Program.cs文件中。 I had copied some code I used from a .NET Core 2.1 project 我已经复制了.NET Core 2.1项目中使用的一些代码

WebHost.CreateDefaultBuilder(args)
     .UseContentRoot(Directory.GetCurrentDirectory())
     .UseWebRoot(Directory.GetCurrentDirectory() + "\\wwwroot") // THIS LINE FIXES STATIC FILES ON PROD
     .UseIISIntegration()
     .UseStartup<Startup>();

Once I edited it to remove the Directory.GetCurrentDirectory() code it works. 一旦对其进行编辑以删除Directory.GetCurrentDirectory()代码,它就会起作用。

WebHost.CreateDefaultBuilder(args)
     .UseIISIntegration()
     .UseStartup<Startup>();

Now I'll have to test on the server to see if it still works. 现在,我必须在服务器上进行测试以查看其是否仍然有效。 Thanks for the responses. 感谢您的答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net Core 2-Powershell远程处理仅在IIS Express中有效(Kestrel无法正常工作) - ASP.Net core 2 - powershell remoting works only in IIS Express (Kestrel not working) ASP.Net Core Web API 适用于 IIS Express,但在部署到 IIS 时不起作用 - ASP.Net Core Web API works on IIS Express, but not working when deployed to IIS 在 Windows 身份验证的 ASP.Net 5 中使用 IIS Express/Kestrel 时,User.IsInRole 始终为 false - User.IsInRole is always false when using IIS Express / Kestrel in ASP.Net 5 with Windows Authentication ASP.NET Core、Kestrel和IIS之间有什么关系? - Whats the relationship between ASP.NET Core, Kestrel and IIS? ASP.NET Core 2.2 集成测试 DbContext 服务未注册 - ASP.NET Core 2.2 Integration Testing DbContext Service not registered ASP.NET Core 2.2在DbContext中获取userId - ASP.NET Core 2.2 get userId in DbContext ASP.NET Core IIS Express AD服务权限错误 - ASP.NET Core IIS Express AD Services Permissions Error 在 ASP.NET Core MVC 中将其用于 ActionFilter 时出现 DbContext 错误 - DbContext error when using it for ActionFilter in ASP.NET Core MVC ASP.NET Core Routing适用于VS IIS Express,但不适用于IIS 10 - ASP.NET Core Routing works in VS IIS Express but not in IIS 10 Asp.Net Core 2.0-2.2 Kestrel 不提供静态内容 - Asp.Net Core 2.0-2.2 Kestrel not serving static content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM