Use ConfigureAwait(false)
inside your lambda:
public override async Task ProcessRequestAsync(HttpContext context)
{
var someLibrary = new SharedLibrary();
someLibrary.PostProcess = async (x,y) => {
await ProcessXY(x,y).ConfigureAwait(false);
}
await someLibrary.ProcessAsync();
// write out some content here
}
public class SharedLibrary {
public Func<int,int,Task> PostProcess { get; set; }
public void Process() {
// do some processing
if (PostProcess != null) {
// call PostProcess synchronously
PostProcess(1,2).Wait();
}
// do some final processing
}
}
This way you should avoid deadlocking on the UI thread.
Edit
As mentioned on the comments, once ConfigureAwait(false)
is used inside ASP.NET, The HttpContext is no longer avaliable inside the continuation
(code after await
)
I would suggest you look into the Microsoft.Bcl.Async
library deployed for .NET 4 via Nuget, so you could use async/await
properly without initializing Threads for no good reason: