System.Data.SqlClient.SqlException (0x80131904) - A Network relate...(Only on localhost)

0

I'm developing an application in ASP.NET Core 2.1, with Windows authentication for the first time.

I've been working on my dev machine with a connection to a remote server hosting the SQL Server database for this application.

Connection string is set in the appsettings.json file as follows:

"DefaultConnection": "Server=V-APPSRV01;Database=devHub;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=true"

I've had no issues working on the application, with connections, CRUD, or otherwise, during development.

When I deploy the release version to the server hosting both the IIS server and the SQL server (same as used during Development), I get this error:

System.Data.SqlClient.SqlException (0x80131904): 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (53): The network path was not found

Also, when connecting to the SQL Server (with SSMS), I'm able to connect from any machine in the network as well as locally.

The website runs, but when I perform the EnsureCreated(), it throws that error, and of course anywhere I try to CRUD data it does the same thing.

The server is a Windows Server 2018 R2 SP1, with IIS 7.5 and SQL Server 2012 (v11.0.2218.0).

Local dev machine is a Windows 10, running IIS Express.

I've followed the Microsoft docs to setup authentication for the app.

As well as this article on configuring the connection strings.

In addition, I read and implemented the recommendations on this article.

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

        using (var scope = host.Services.CreateScope())
        {
            var services = scope.ServiceProvider;

            try
            {
                var context = services.GetRequiredService<PEXContext>();
                context.Database.EnsureCreated();
            }
            catch(Exception ex)
            {
                var logger = services.GetRequiredService<ILogger<Program>>();
                logger.LogError(ex, "An Error occurred creating the DB.");
            }
        }

        host.Run();
        // CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>();
}

The issue seems to be connecting locally to the database.

Remotely works as intended:

  • it connects to the database using Windows authentication
  • it (re)creates the database when it's empty
  • and performs CRUD on the table created from its controller

If I wasn't using the same database on both machines, and if the problem was reversed - where it worked locally and not remotely - I would say it was the connection string, or the firewall rules, but no.

c#
asp.net-core
sql-server-2012
iis-7.5
asp.net-core-2.1
asked on Stack Overflow Apr 3, 2019 by Trihugger • edited Apr 3, 2019 by marc_s

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0