简体   繁体   中英

Asp.Net Core application runs in IIS Express but not in IIS App Deployment

I have a problem which i can't solve and i need some help.

I am currently creating an ASP.Net Core App and i get data from database with Entity Framework. In my simple test app i just scaffolded Models from database and added the connection string in both appsettings and Startup.cs as Microsoft Documentation says. In Controller i am just taking records from table and display them in View. That is working fine when i run it in IIS Express but when I deply the Application in IIS and try to debug it the app stops when code trys to take data from table And displays this exception:

Microsoft.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SNI_PN11, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.

)'

Startup:

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

            services.AddDbContext<PushNotificationContextContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("PushNotificationContext")));
        }

Controller:

public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly PushNotificationContextContext _context;
        public HomeController(ILogger<HomeController> logger, PushNotificationContextContext context)
        {
            _logger = logger;
            _context = context;
        }

        public IActionResult Index()
        {
            HomeModel model = new HomeModel();
               model.AppUserList = _context.AppUsers.Take(10).ToList();
            return View(model);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }

The main problem is that a week ago i made a simple app that runs okay on IIS with the same connection string. I didnt change anything in SQL Managment Studio. The app wont play in other pc's as well. I know there are similar questions but none of them managed to solve my problem.

This error usually means that the client can't find the SQL Server instance. This normally happens when at least one of the following problems exists:

  • The name of the computer hosting the SQL Server
  • Instance doesn't resolve the correct IP
  • The TCP port number isn't specified correctly

About how to troubleshoot connecting to the SQL Server Database Engine you can refer to this link: Troubleshoot .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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