2
I have a Webapi where I need to search the amount of records of a given table in sqlexpress on the production server, local I have no problems at all.
I’m using ADO
pure and with ExecuteScalar
, without any ORM, I have received timeout errors, I managed to catch the Exception through logs.
I tried to increase Connection Timeout=120
, but I just put the parameter in my connectionstring, with any timeout value, I get the error below in the log I had written:
The 'Context' entry has already been added. (C: Inetpub Webapi apiv2 web.config line 73)
My connection string looks like this:
<add name="Context" connectionString="data source=CLIENTE-XXXXX\SQLEXPRESS2014; initial catalog=BaseInventario; user id=UsuarioXXX;Password=XXXXX; MultipleActiveResultSets=True; Connection Timeout=30" providerName="System.Data.SqlClient" />
From the message, it seems that I entered the connection string twice, but I did not do this, the error only happens when I put the timeout parameter, while removing the error stops happening.
Does anyone know the reason for this mistake?
Below is my method that is giving timeout
public int GetTotalPorInventarioData(int InventarioId, int idPatrimonio, DateTime DataAtualizacao)
{
int retorno = 0;
try
{
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Context"].ConnectionString))
{
String sql = @"SELECT COUNT(*) as total FROM Inventarios.Patrimonios WITH (NOLOCK) where InventarioId = " + InventarioId + " and (DataUltimaAtualizacao >= @DataConsultar or DataUltimaAtualizacao is null) and Id >= " + idPatrimonio;
retorno = conn.ExecuteScalar<int>(sql, new { DataConsultar = DataAtualizacao });
}
}
catch (Exception ex)
{
throw ex;
}
return retorno;
}
Place a <clear /> before in the top line of the string Connection and check if it solves your problem.
– George Wurthmann
post the code to help. so can’t help much
– wallafe sousa
Thanks wallafe, I added the code... it is very simple and direct "think", there are other returning list that also give timeout. Suspected of some configuration in Sqlserver, but found nothing that could cause it.
– Vagner C. S.
I put the tag <clear /> that George Wurthmann, the error "The input 'Context' has already been added..." stopped occurring, but the timeout error kept happening...
– Vagner C. S.
Dude, the message is clear... You are adding more than once the Context key to your configuration files or creating a new one in Runtime connection... Another point to note is why you are getting timeout in such simple queries. You need to review your bank structure, typing, indexes and etc
– Leandro Angelo