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')
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 toServerName\InstanceNamethen lose the\InstanceNamepart.Data Source=tcp:127.0.0.1,1433;as per the SqlConnection.ConnectionString Property documentation.