I have an ASP Core 3.1 web project where I want to add EntityFramework Core to.
I've created a db context, model class with database operations, and I've injected this into my main class (an Azure Bot).
However, when I try to insert a record into the database, it always fails with the error
System.Threading.Tasks.TaskCanceledException: 'A task was canceled.'
This is my startup.cs:
services.AddDbContext<IVRContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection"),
optionBuilder => optionBuilder.EnableRetryOnFailure()
)
);
services.AddTransient<IVRCallModel>();
This is the function in my IVRModel that I'm calling:
public async Task InsertCallAsync(IVRCall call)
{
try
{
await _ivrContext.Calls.AddAsync(call);
await _ivrContext.SaveChangesAsync();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
This is how I call it:
private async Task NotificationProcessor_OnNotificationReceivedAsync(NotificationEventArgs args)
{
this.GraphLogger.CorrelationId = args.ScenarioId;
if (args.ResourceData is Call call)
{
if (call.Direction != CallDirection.Outgoing && call.ToneInfo == null)
{
if (args.ChangeType == ChangeType.Created && call.State == CallState.Incoming)
{
await SaveCall(call.Id, call.CallChainId, "Incoming");
.... code removed
}
}
}
}
private async Task SaveCall(string callId, string callChainId, string callState, string redirectCallId = null)
{
IVRCall newCall = new IVRCall();
newCall.Id = callId;
newCall.CallChainId = callChainId;
newCall.TimeStamp = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
newCall.State = callState;
newCall.RedirectCallId = redirectCallId;
await _ivrCallModel.InsertCallAsync(newCall);
}
Edit: The 'original' NoticicationProcessor_OnNotificationReceived function, which calls the async method. (From a Microsoft sample project)
private void NotificationProcessor_OnNotificationReceived(NotificationEventArgs args)
{
_ = NotificationProcessor_OnNotificationReceivedAsync(args).ForgetAndLogExceptionAsync(this.GraphLogger, $"Error processing notification {args.Notification.ResourceUrl} with scenario {args.ScenarioId}");
}