简体   繁体   English

在Asp.Net Core 2.X中部署后无法解析DbContext

[英]Could not resolve DbContext after deployment in Asp.Net Core 2.X

I have a webapi in asp.net core 2.1 and that is working properly if I press F5 from the Visual Studio, I can hit my controllers, but when I deploy the application to the IIS, it says that it can resolve the DbContext on the Configure method. 我在asp.net core 2.1中有一个webapi,如果我从Visual Studio中按F5键,则可以正常工作,可以打我的控制器,但是当我将该应用程序部署到IIS时,它说它可以解析DbContext。配置方法。

My Configure looks like this 我的Configure看起来像这样

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, IConfiguration configuration, DocumentDbContext documentContext)
        { …}

on my ConfigureServices I inject the dbcontext on this way 在我的ConfigureServices我以这种方式注入dbcontext

container.AddDbContext<DocumentDbContext>(options => options.UseSqlServer(connectionString), ServiceLifetime.Scope);

and as I said is working. 正如我所说的那样。 In order to deploy the application to an IIS I added the following information on the Program class 为了将应用程序部署到IIS,我在Program类上添加了以下信息

 public class Program
    {
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

            host.Run();
        }

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

The ApplicationPool is running under No Managed Code do I need to check something else? ApplicationPool是否在“ No Managed Code下运行,我需要检查其他内容吗?

Issue was identified to be the connection string was using integrated security which was not working when deployed to run under IIS as web app was running under application pool identity. 确定问题是连接字符串正在使用集成安全性,当Web应用程序在应用程序池标识下运行时,该集成字符串在部署为在IIS下运行时不起作用。 Since application pool identity does not have access to database, connection was failing. 由于应用程序池标识无权访问数据库,因此连接失败。

Suggested fix was to create a new sql server login, assign that user to application database, update connection string to use the userid and password instead of integrated security. 建议的解决方法是创建一个新的sql server登录名,将该用户分配给应用程序数据库,更新连接字符串以使用用户名和密码而不是集成安全性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM