1

Inside my Asp.net MVC 3 app, using Entity Framework 6.0, I am trying to update a database table record using an asynchronous method like:

public static async Task UpdateAssetLogAsync(string assetID)
{
    using (var context = new MyContext())
    {
        var log = await context.UploadedLogs.SingleAsync(e => e.AssetID == assetID);
        if (log != null)
        {
            log.LastUpdatedOn = DateTime.Now;
            await context.SaveChangesAsync();
        }
    }
}

Unfortunately code execution stops as soon as it comes to line number 5 i.e

var log = await context.UploadedLogs.SingleAsync(e => e.AssetID == assetID);

Visual Studio (2012) shows no error at all and program execution just breaks from here.

Please can anyone tell what is the issue?

[Edit] This is my connection string

<add name="MyContext" connectionString="Server=.\SQLExpress;Database=Test;Max Pool Size=80;Integrated Security=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>

[Edit] This is happening because of 'Integrated Security=true' within connection string. If I use sa and password options in connection string it works fine. In my web.config an impersonation context is already defined which works fine outside Task but inside Task it seems the impersonation context is not available, causing EF not to connect.

9
  • put a try/catch and check if there is an exception being thrown. Commented Feb 18, 2015 at 17:19
  • 3
    You're almost certainly synchronously blocking on an asynchronous task somewhere in the call stack. Commented Feb 18, 2015 at 17:19
  • @vtortola I have set try/catch block and it is saying "The underlying provider failed on open" Commented Feb 18, 2015 at 17:24
  • 2
    You should probably avoid using static and async together when a DbContext is concerned. Commented Feb 18, 2015 at 17:28
  • Also, try the same code without debugging. Async code breaks the debugger sometimes.. Commented Feb 18, 2015 at 17:35

1 Answer 1

1

Threads spawned with async methods usually return a Task or Task<TResult>. Unfortunately, the impersonation context of the caller is not passed along to this thread - by default. That may be why you can't connect to the DB - wrong credentials.

You can pass the impersonation context through aspnet.config file settings using the <alwaysFlowImpersonationPolicy> in IIS

OR

Within your code, use WindowsIdentity and WindowsImpersonationContextto allow Impersonation context to flow with the async thread.

Here are a few articles of interest:

http://blog.codeishard.net/2012/09/17/await-async-mvc-and-impersonation/

How do I set the user identity for tasks when calling Task.WaitAll()?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.