Hello, I am developing an application in which it connects to a remote database and I need to know if the server responds or not, I have researched on the internet and there is a property called "state"
PruebaEntities pruebaEntities = new PruebaEntities();
while (true)
{
//Siempre imprime Closed aunque si este conectado
Console.WriteLine(pruebaEntities.Database.Connection.State);
Thread.Sleep(10000);
}
The problem with that property is that it doesn't reflect changes in real time if the server is down or couldn't connect for whatever reason.
The only way Open prints for me is if I manually start the connection.
PruebaEntities pruebaEntities = new PruebaEntities();
//Inicio la conexion manualmente, aunque si no puede conectarse, manda una excepcion
pruebaEntities.Database.Connection.Open();
while (true)
{
//Ahora si imprime Open
Console.WriteLine(pruebaEntities.Database.Connection.State);
Thread.Sleep(10000);
}
But another problem with it is that if the server closes, this will still print Open
Does anyone know how I can get the status of the connection in real time?
At the moment of making the connection, we are already talking about real time. With the Entities Framework I don't know if it is possible to carry out these verifications, I have created my own Framework that generates all the code of the Business Layer and the Data Access Layer, plus all the entities of the tables and I only worry about the UI, be it web, desktop or services.
And in this way I control the access and availability of the database server. In each LAYER of software.
Then if there is an error in the connection from the business layer I catch it with the exception defined by BaseDatosException
So:
Example of a method that brings the data and also controls the connection with the data server, look at the try and catch:
And finally from the user interface I do the following to capture problems in the connection from the user interface (look at the cache):
With this I control the condition of the connection to the database server all the time, it works perfectly for me with web applications, desktop applications, windows services, and worker services.
The only important thing is that all this work is done by the application I built, I only care about the interface since it even generates the stored procedures.
It generates:
Entities Business Layer Code and Stored Procedures
SPs
The business layer classes
and the entities
Using your own Framework gives you more freedom to manipulate everything!!! Think about that and get inspired by your own way of working to create your own methodology, it is the easiest thing you will have total control.