async-await

AsyncLocal Value updated to null on ThreadContextChanged

元气小坏坏 提交于 2020-01-13 07:53:07
问题 I'm trying to understand how AsyncLocal should work in .Net 4.6. I'm putting some data into AsyncLocal...but when the ThreadContext changes it is getting set to null. The whole reason I'm using AsyncLocal is to try to preserve/cache this value across threads as I await async operations. Any idea why this would be specifically called and set to a null as the context changes? Documentation on AsyncLocal is very sparse...perhaps I've got it all wrong. public class RequestContextProvider :

Is it possible to create a TransactionScope in a Custom WCF Service Behavior? (async, await, TransactionScopeAsyncFlowOption.Enabled)

北城以北 提交于 2020-01-12 07:25:48
问题 TL;DR ? Screencast explaining problem: https://youtu.be/B-Q3T5KpiYk Problem When flowing a transaction from a client to a service Transaction.Current becomes null after awaiting a service to service call. Unless of course you create a new TransactionScope in your service method as follows: [OperationBehavior(TransactionScopeRequired = true)] public async Task CallAsync() { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await _service.WriteAsync(); await

TaskCompletionSource throws “An attempt was made to transition a task to a final state when it had already completed”

醉酒当歌 提交于 2020-01-12 04:30:11
问题 I want to use TaskCompletionSource to wrap MyService which is a simple service: public static Task<string> ProcessAsync(MyService service, int parameter) { var tcs = new TaskCompletionSource<string>(); //Every time ProccessAsync is called this assigns to Completed! service.Completed += (sender, e)=>{ tcs.SetResult(e.Result); }; service.RunAsync(parameter); return tcs.Task; } This code is working well for the first time. But the second time I call ProcessAsync simply the event handler for the

How to support async methods in a TransactionScope with Microsoft.Bcl.Async in .NET 4.0?

允我心安 提交于 2020-01-12 04:12:32
问题 I have a method similar to: public async Task SaveItemsAsync(IEnumerable<MyItem> items) { using (var ts = new TransactionScope()) { foreach (var item in items) { await _repository.SaveItemAsync(item); } await _repository.DoSomethingElse(); ts.Complete(); } } This of course has issues because TransactionScope doesn't play nice with async/await. It fails with an InvalidOperationException with the message: "A TransactionScope must be disposed on the same thread that it was created." I read about

How to support async methods in a TransactionScope with Microsoft.Bcl.Async in .NET 4.0?

主宰稳场 提交于 2020-01-12 04:12:06
问题 I have a method similar to: public async Task SaveItemsAsync(IEnumerable<MyItem> items) { using (var ts = new TransactionScope()) { foreach (var item in items) { await _repository.SaveItemAsync(item); } await _repository.DoSomethingElse(); ts.Complete(); } } This of course has issues because TransactionScope doesn't play nice with async/await. It fails with an InvalidOperationException with the message: "A TransactionScope must be disposed on the same thread that it was created." I read about

IOCP threads - Clarification?

走远了吗. 提交于 2020-01-12 01:34:08
问题 After reading this article which states : After a device finishes its job , (IO operation)- it notifies the CPU via interrupt. ... ... ... However, that “completion” status only exists at the OS level; the process has its own memory space that must be notified ... ... ... Since the library/BCL is using the standard P/Invoke overlapped I/O system, it has already registered the handle with the I/O Completion Port (IOCP), which is part of the thread pool. ... ... ... So an I/O thread pool thread

IOCP threads - Clarification?

核能气质少年 提交于 2020-01-12 01:34:08
问题 After reading this article which states : After a device finishes its job , (IO operation)- it notifies the CPU via interrupt. ... ... ... However, that “completion” status only exists at the OS level; the process has its own memory space that must be notified ... ... ... Since the library/BCL is using the standard P/Invoke overlapped I/O system, it has already registered the handle with the I/O Completion Port (IOCP), which is part of the thread pool. ... ... ... So an I/O thread pool thread

ASP.NET C#5 Asynchronous Web Applications Using Async & Await

北慕城南 提交于 2020-01-11 17:15:30
问题 Having researched the concept of asynchronous web development, specifically from this source, I created a sample application to prove the concept. The solution is composed of 2 ASP.NET Web API applications. The first is a simulated slow endpoint; it waits for 1000 ms before returning a list a custom class called Student: public IEnumerable<Student> Get() { Thread.Sleep(1000); return new List<Student> { new Student { Name = @"Paul" }, new Student { Name = @"Steve" }, new Student { Name = @

How can I use async in an mvvmcross view model?

夙愿已清 提交于 2020-01-11 16:38:06
问题 I have a long running process in an mvvmcross viewmodel and wish to make it async (http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx). The async keyword is currently supported in the beta channel for Xamarin. Below is an example of how I'm currently implementing async. The IsBusy flag ccould be bound to a UI element and display a loading message. Is this the correct way? public class MyModel: MvxViewModel { private readonly IMyService _myService; private bool _isBusy; public bool

Default SynchronizationContext vs Default TaskScheduler

拈花ヽ惹草 提交于 2020-01-11 15:27:31
问题 This is going to be a bit long, so please bear with me. I was thinking that the behavior of the default task scheduler ( ThreadPoolTaskScheduler ) is very similar to that of the default " ThreadPool " SynchronizationContext (the latter can be referenced implicitly via await or explicitly via TaskScheduler.FromCurrentSynchronizationContext() ). They both schedule tasks to be executed on a random ThreadPool thread. In fact, SynchronizationContext.Post merely calls ThreadPool.QueueUserWorkItem .