Azure mysql virtual machine stops working after a while - c#

I have a ASP.net Web API deployed on azure and I also have there a MySql database in separate virtual machine running on linux. The problem I have is that when I restart the database and redeploy the web API from visual studio the connection between web API and mySql is working fine but after like 30 minutes I get this error:
"Unable to connect to any of the specified MySQL hosts"
If I want to make it work again i have to restart the virtual machine with database and redeploy the web API from visual studio. I am using connection string like this to connect in my web config
<add name="DefaultConnection" connectionString="Server=publicIpAddress;Port=3306;Database=db_12345_db;Uid=user;Pwd=*********;" providerName="MySql.Data.MySqlClient" />
This connection was working on server we had before we switched to azure. I suspect I did not configure azure correctly.
Any idea how to fix this issue ? Thanks

Connections Dropped
When connecting to a Azure SQL Database, idle connections may be terminated by a network component (such as a firewall) after a period of inactivity. There are two types of idle connections, in this context:
Idle at the TCP layer, where connections can be dropped by any number
of network devices.
Idle by the SQL Azure Gateway, where TCP keepalive messsages might be occurring (making the connection not idle from a TCP perspective), but not had an active query in 30 minutes. In this scenario, the Gateway will determine that the TDS connection is idle at 30 minutes and terminate the connection.
https://github.com/sidorares/node-mysql2/issues/316
Hope this works for you.

Related

What can cause a SignalR connection to drop?

I have a C# Windows Service running in approximately 500 locations. When the services start they initiate a connection with a remote SignalR hub running on a VM in AWS. The connection/tunnel is then used for sending information from the server to each location throughout the day.
This has been running fine for the past 18 months, but for the past week the remote services have been losing connection and have to be restarted to reinitiate the connection with the server. I'm using SignalR 2.4.1 which doesn't have the auto reconnection feature of the newer versions.
I've noticed that it seems to be happening about every 18 hours. Nothing has changed on the clients or on the server that anyone is aware of.
I'm looking for ideas on what could be causing this. Any input would be appreciated.

Blazor Server on IIS10 - only 4 concurrent connections

One of my customers suddenly reported that a local website I've made in Server-side Blazor only can have 4 concurrent connections. When the 5th connection is established, one of the other 4 is getting disconnected.
I Have looked through my configuration and can't see any sign of this. Connections is not limited in IIS configuration.
OS: Windows Server 2016
I have tried to debug at my local copy of the virtual machine, but here everything is working as expected.
Anyone who got a clue what I can look for?

Slow Database Connection from Azure Web Application

I have developed a Web Application a standard web application to allow users to display and update a set of data from an SQL database.
The Web Application uses a AngularJS client side which interacts with the Web Server via MVC Web API calls to retrieve and update data on the database.
The Server side code is written in C# using .NET 4.5 and uses Entity Framework v6.0 to access the database.
The Web Application is hosted in an Azure Web App.
The Database is the Azure SQL Database.
The issue is that when the Application has not been used for about 10-15 minutes, then it is used again, the first data retrieval often takes over 10 seconds to return to the browser. After that the performance is fine until the next time the application is left unused.
I've put trace in the application and we see that the delay is when the connection opens. The actual query on the database runs sub-second.
I've noticed though that with different hosting configurations I get different results. In particular hosting in house and pointing to the Azure database does not encounter anywhere near the same delays.
I've changed one of the routines to use ADO.NET instead of Entity Framework and changed the trace to try to narrow it down further.
What I see is this:
ConnectionStringSettings ADOcnxstring = ConfigurationManager.ConnectionStrings["DevFEConnectAdo"];
DbConnection ADOconnection = new SqlConnection(ADOcnxstring.ConnectionString);
The delay is here (before the SQL has even been defined!
and then I build the command and do the DataReader etc:
DbCommand ADOcommand = ADOconnection.CreateCommand();
:
etc
So the delay is on opening the Connection to the database.
My connection string is standard:
<add name="DevFEConnectAdo" connectionString="data
source=feeunsqldevfeconnect.database.windows.net;initial
catalog=feeunsqldbdevfeconnect;persist security info=True;user id=???
#???;password=???;multipleactiveresultsets=True"></add>
15 minutes is too short for your app to be recycled (as suggested by CSharpRocks). I dont think its the issue here.
The delay is because a new Db connection is established upon first call after idle timeout. Typically if a connection is inactive for 4-10 minutes it will be closed. If a minimum pool size is specified, those connections will be kept alive even after idle timout expires.
Try using this connection string (adjust min pool size as per your needs)
<add name="DevFEConnectAdo" connectionString="data
source=feeunsqldevfeconnect.database.windows.net;initial
catalog=feeunsqldbdevfeconnect;persist security info=True;user id=???
#???;password=???;multipleactiveresultsets=True;Min Pool Size=3;Load Balance Timeout=180;"></add>
Further details
Why do we need to set Min pool size in ConnectionString
List of SQL Connection Properties - documentation
After some time, this eventually got resolved with some help from Microsoft Azure support.
The detail that I left out was that my Web App was actually pointing to 2 databases
- 1 the Application Azure SQL database, I was having the delay problem with
- A 'Data Warehouse' we had on an Azure Virtual Machine
Because of replication between inhouse database servers and the 'Data Warehouse' the Virtual Machine and Web App were all in a Azure Virtual Network.
The problem was there can be network problems if a Web App inside a Virtual network wants to talk to Azure SQL Databases (which cannot be within a Virtual Network).
My solution was to
configure an Endpoint on the Data Warehouse Virtual Server,
take the Web App out of the Virtual network and make it point to the Virtual Server by means of the Endpoint
At this point all the delays went away and I could take off the MinPool Size settings (and Timeout which I later discovered did nothing anyway).
Web apps are recycled after a few minutes of inactivity. Try enabling the Always On setting located in Settings/Application Settings in the portal to see if this helps with your issue.

my windows service starts before the sql server service

I have a windows service written in c#, which when running connects to a sql server database.
When the service first starts it connects to the database to obtain various information for the service to run.
The service is set to automatically start if the server it is installed on is restarted.
If the service starts before the sql server service has started. My service when started obviously has an initial execption error when trying to connect to the database.
To combat this issue, if the exception occurs I have added a short delay before try to connect to the database again, I continue this n amount of times until it connects
My question is, is there any alternative solutions to what I am doing already
thanks
Make your service dependent on SQL Server, done using ‘sc’.
Using a poll, as you're doing right now is fie, and you'll still need it if your service starts after SQL Server because you can never be sure SQL Server has fully started then.

Windows Azure Website connect external SQL server

I just upload my project to Windows Azure website free version.
My website need to connect to external SQL server on another web hosting.(not SQL server on windows azure).
When i develop on my PC. Everything works well (that mean my connection string is correct).
But when i upload to windows azure. It can't connect to that database.
And throw exception like this.
Access is denied
when i do the following this answer, error show like this
Connecting to remote SQL Server 2008 from Windows Azure
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
and both show stack trace like this
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
[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.
how can i solve this issue, please help.
You're uploading your application to a Windows Azure Website. Most probably port 1433 that connects to SQL Server is blocked by default on their firewall. Since you're using an azure website you will have no control over this firewall setting.
You can either:
Migrate your existing SQL Server database into SQL Azure and consume it from there
Upgrade your application to a Cloud Service where you can define the endpoints you need to connect to the external database (which I wouldn't recommend anyway because it would be very slow on deployment)
Find a way to publish your existing database as web services so you can consume them from your application, but you would probably have to rewrite your app's data access layer.
I would simply move everything to SQL Azure, it's pretty straightforward.
If you can add a website to the remote host where the database is hosted, can you create a service (e.g. WCF or Rest) on that same host that allows you to talk to the database? That way you can still host the database on the external host and communicate with it via the service from the Azure Website.
Looks like there's a new way to allow Azure webapps to connect to on premises SQL Servers using Hybrid Connections. Google leads me to believe that this feature became available sometime in 2016. It will require that you install additional software on the machine that hosts your SQL Server instance.
More information available here.

Categories