I've got a strange behaviour. When I trying to execute SQL store procedure by EF Core 2.2. I get a timeout (30 seconds). But when I execute the same procedure within SQL Server Management Studio it runs successfully - it runs it about 1 or 2 sec.
UPDATE: This problem occured around 2 month ago. Before that the service was able to run this proc.
Here is a DbContext
that I use:
public class MyContext : DbContext
{
public MyContext(string connectionString)
: base(new DbContextOptionsBuilder().UseSqlServer(connectionString).Options)
{
}
// some DbSets omitted here ...
public async Task<string> ProcExecuteAsync(string login, string xml)
{
// get some data from db (omitted)
var xmlParameter = new SqlParameter("@XML", SqlDbType.Xml) { Value = xml };
var xmlOutputParameter = new SqlParameter("@XMLoutput", SqlDbType.Xml) { Direction = ParameterDirection.Output };
await Database.ExecuteSqlCommandAsync(
"MyProc_set @idUser=@idUser,@XML=@XML,@XMLoutput=@XMLoutput OUT",
new SqlParameter[] { userIdParameter, xmlParameter, xmlOutputParameter });
return (string)xmlOutputParameter.Value;
}
}
Then I have a handler (I use Mediator
library):
public class AddToClientCommandHandler : IRequestHandler<AddToClientCommand, AddToClientResponse>
{
private readonly IContextBuilder _contextBuilder;
public AddToClientCommandHandler(IContextBuilder contextBuilder)
{
_contextBuilder = contextBuilder;
}
public async Task<AddToClientResponse> Handle(AddToClientCommand command, CancellationToken cancellationToken)
{
using (var context = _contextBuilder.CreateMyContext())
{
// getting some data from db (omitted)
// generate xmlRequest (omitted)
var result = await context.ProcExecuteAsync(command.Login, xmlRequest);
return new AddToClientResponse(result);
}
}
}
Here is a ContextBuilder
:
public class ContextBuilder : IContextBuilder
{
private readonly IOptions<ConnectionStrings> _connectionStrings;
public ContextBuilder(IOptions<ConnectionStrings> connectionStrings)
{
_connectionStrings = connectionStrings;
}
public MyContext CreateMyContext()
{
var result = new MyContext(_connectionStrings.Value.Entities);
return result;
}
}
And I register it like this:
public static class ApplicationContextBuilderExtension
{
public static void AddApplicationContextBuilder(this IServiceCollection services)
{
services.AddSingleton<IContextBuilder, ContextBuilder>();
}
}
Is it possible that the problem comes from that I use AddSingleton
when I register ContextBuilder
?
Can someone point me up to how and where investigate the problem?