2

On my small project, I try to save some data or send notification to users.

Could I use await/async on my c# code and query run even after sending data to client ?

Here is the sample:

async string GetName(long userId)
{
string information="";  // loading data with entity

await Task.Run(() => UpdateActivity(userId));
await Task.Run(() => SendNotification(userId));


return information;
}

void UpdateActivity(long userId)
{
// loading data with entity
// updating activity
}

void SendNotification(long userId)
{
// loading data with entity
// Sending Notification
}

Here is one of my problem during loading data with entity

An exception of type 'System.Data.Entity.Core.EntityException' occurred in mscorlib.dll but was not handled in user code

Additional information: The underlying provider failed on Open.

Entity code, works fine when I haven't using await-async

Meysam
  • 87
  • 5
  • 5
    The code you've posted has nothing to do with the exception you're getting. An answer is already [here](https://stackoverflow.com/questions/18271301/entity-framework-the-underlying-provider-failed-on-open) and [here](https://stackoverflow.com/questions/2475008/mssql-error-the-underlying-provider-failed-on-open) on SO and [here](https://www.codeproject.com/Tips/126919/Solution-for-The-underlying-provider-failed-on-Ope) on codeproject. – mrogal.ski Sep 19 '17 at 09:50
  • 4
    @m.rogalski question is about way of using await-async during using entity , entity code works fine when it hasn't await-async – Meysam Sep 19 '17 at 09:59
  • What happens inside `UpdateActivity`, esp. which context instance is used there? – Gert Arnold Sep 19 '17 at 10:21

3 Answers3

2

Let's try this

async Task<string> GetName(long userId)
{
string information="";  // loading data with entity

await Task.Run(() => UpdateActivity(userId));
await Task.Run(() => SendNotification(userId));


return information;
}

Return Task, not string

Dan Nguyen
  • 1,308
  • 6
  • 17
0

Maybe try

await Task.Run(() => UpdateActivity(userId)).ConfigureAwait(false);
await Task.Run(() => SendNotification(userId)).ConfigureAwait(false);

This will make GetName function to stay in the same context.

Also, if create DbContext outside GetName function, it can be disposed before execution of UpdateActivity and SendNotification, since you cannot await GetName. For that you need to return Task, as Dan suggested

Krzysztof Skowronek
  • 2,796
  • 1
  • 13
  • 29
0

I think thats is because dbContext is not available in parallel task as you have passed only userId to parallel task which ultimately refer to dbcontext to perform dbOperation. You need to change architecture for this.

async Task<string> GetName(long userId)
{
string information="";  // loading data with entity
DbContext context = _context// Your dbContext
await Task.Run(() => UpdateActivity(userId, context));


return information;
}

void UpdateActivity(int userId, DbContext context)
{
   //perform dboperation with context
}
Bhuban Shrestha
  • 1,304
  • 11
  • 27