0

I am using .NET Core MVC API (.NET Core 6.0), actually just C# in general.

I need to deploy a .NET API on a linux server. However, it seems to that I can't connect to the SQL Server that is running on the same server.

This is the error from .NET

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1] An unhandled exception has occurred while executing the request. System.Exception: Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000001, 11): Resource temporarily unavailable at System.Net.Dns.GetHostEntryOrAddressesCore(String hostName, Boolean justAddresses, AddressFamily addressFamily, ValueStopwatch stopwatch)

This is the error I get from curl

{"error":true,"message":"Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started."}

Keep in mind that both the API and the database are on the same server. I think the problem comes from .NET. I am able to connect to the database on the server using the API locally.

This is the connection string in my appsettings.json

"ConnectionStrings": {
    "ConnStr": "Data Source=<PUBLICIP>;Initial Catalog=<DBNAME>;User Id=<USER>;Password=<PASS>;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",=
    "lockerConStr": ""
  }

Any help would be greatly appreciated!

Things I have tried:

  • Changed Data Source to (local) - (doesn't work)
  • Added Encrypt= false;Trusted_connection=True; - (doesn't work)
  • Connect to the SQL Server from local version of the .NET API (successful)
  • Connect to the SQL Server using a python script running from the same server (successful)
  • Specified Driver in the connection string (doesn't work)

The python script (not sure if this would help)


import pyodbc

conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
                      'Server=<PUBLICIP>;'
                      'Database=<DBNAME>;'
                      'UID=<USER>;'
                      'PWD=<PWD>;'
                      'Trusted_Connection=no;')

cursor = conn.cursor()
cursor.execute('SELECT * FROM users')

4
  • Cannot connect to SQL Server Browser? It seems that you're not showing us all of the connection/connectionString details. It's trying to connect to an SQL Server by instance name but there's no SQL Browser service on Linux/Docker-based SQL Servers to provide the necessary SQL Server Resolution Protocol support via udp/1434. In other words, if you're trying to connect to ServerName\InstanceName then lose the \InstanceName part. Commented Nov 30, 2022 at 7:00
  • Hi, thanks for the comment. Could you please elaborate on Linux SQL Servers having no SQL Browser service? I am unsure if accessing the server through the public IP is considered connecting to the \InstanceName. Do I need to set up an instance name for the SQL Server ? Currently the connection string is "Data Source=xx.xxx.xx.xxx;..." Commented Dec 6, 2022 at 2:58
  • What more would you like to know about SQL Server Browser service and the [MC-SQLR]: SQL Server Resolution Protocol that it implements? Commented Dec 6, 2022 at 3:13
  • If it's a Linux server and SQL Server is deployed on the exact same machine (i.e.: physically, not through Docker containers) you could try changing your connection string to use Data Source=tcp:127.0.0.1,1433; as per the SqlConnection.ConnectionString Property documentation. Commented Dec 6, 2022 at 3:15

1 Answer 1

0

I deleted the whole project on the server and cloned the git back.

After carefully reading the error, I noticed that it's running using my local connection string (not sure why, I changed the connection string on the server). I also rebuilt the solution and republished it a few times. Not sure what changed.

But I finally got it to work!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.