c#-5.0

Using ASP.NET Web API, my ExecutionContext isn't flowing in async actions

纵然是瞬间 提交于 2019-11-28 06:24:43
I'm having difficulty understanding the mechanics behind ExecutionContext. From what I've read online, context-sensitive items such as security (Thread Principal), culture, etc, should flow across asynchronous threads within the bounds of an execution unit of work. I'm encountering very confusing and potentially dangerous bugs though. I'm noticing my thread's CurrentPrincipal is getting lost across async execution. Here is an example ASP.NET Web API scenario: First, let's setup a simple Web API configuration with two delegating handlers for testing purposes. All they do is write out debug

Why InvalidCastException when awaiting Task-returning method?

左心房为你撑大大i 提交于 2019-11-28 05:40:03
问题 (The real title of the question should be "Why do I get a 'Unable to cast object of type 'System.Runtime.CompilerServices.TaskAwaiter`1[System.Runtime.CompilerServices.VoidTaskResult]' to type 'System.Runtime.CompilerServices.INotifyCompletion'", but unfortunately this is too long for StackOverflow. :) Hi, I'm getting really peculiar problems when trying to await the execution of a method of mine. The calling code looks like this (excerpt): private async Task DownloadAddonFileAsync(dynamic

Where can I find the C# 5 language specification?

a 夏天 提交于 2019-11-28 05:16:00
C# 5.0 is out now since August 2012. Where can I find the specification? They've stopped doing ECMA specs, but how about MSDN? Excommunicated It was originally unavailable online but since June 2013 it is available for download from Microsoft . If you have Visual Studio 2012 installed, you will find specification somewhere there: c:\Program Files (x86)\Microsoft Visual Studio 11.0\VC#\Specifications\1033\CSharp Language Specification.docx similar with VS2013: c:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Specifications\1033\CSharp Language Specification.docx VS2015: c:\Program Files

Asynchronous Programming with Async and Await

蹲街弑〆低调 提交于 2019-11-28 03:28:14
问题 I'm walking through this tutorial on how to program asynchronously in c# and have come across an error I'm not sure how to resolve. Here's the link: http://msdn.microsoft.com/en-us/library/hh191443.aspx and the error is: Cannot find all types required by the 'async' modifier. Are you targeting the wrong framework version, or missing a reference to an assembly? I am targeting the .NET 4.0 framework and am unsure as to any additional assemblies required. Here is the code: public async Task

How does C# 5.0's async-await feature differ from the TPL?

廉价感情. 提交于 2019-11-28 03:01:41
I don't see the different between C#'s (and VB's) new async features, and .NET 4.0's Task Parallel Library . Take, for example, Eric Lippert's code from here : async void ArchiveDocuments(List<Url> urls) { Task archive = null; for(int i = 0; i < urls.Count; ++i) { var document = await FetchAsync(urls[i]); if (archive != null) await archive; archive = ArchiveAsync(document); } } It seems that the await keyword is serving two different purposes. The first occurrence ( FetchAsync ) seems to mean, "If this value is used later in the method and its task isn't finished, wait until it completes

Can't await async extension method

时光总嘲笑我的痴心妄想 提交于 2019-11-28 00:19:29
Situation is pretty simple - I wrote an extension method and made it async with return type Task<T> . But when I try to call it using await, compiler throws an error which suggests that the extension method wasn't recognized as async at all. Here's my code: public static async Task<NodeReference<T>> CreateWithLabel<T>(this GraphClient client, T source, String label) where T: class { var node = client.Create(source); var url = string.Format(ConfigurationManager.AppSettings[configKey] + "/node/{0}/labels", node.Id); var serializedLabel = string.Empty; using (var tempStream = new MemoryStream())

How to throttle the speed of an event without using Rx Framework

十年热恋 提交于 2019-11-27 21:01:38
问题 I want to throttle the speed of an event, How I can achieve this without using Microsoft Rx framework. I had done this with the help of Rx. But what I am trying is, I need to throttle Map's View changed event based on a time slot. Is it possible to implement the same without using Rx. I am not allowed to use Rx and I have to keep the binary size as small as possible. 回答1: This works, if your event is of type EventHandler<EventArgs> for example. It creates a wrapper for your event handler that

An entry point cannot be marked with the 'async' modifier

橙三吉。 提交于 2019-11-27 21:00:06
I copied below code from this link.But when I am compiling this code I am getting an entry point cannot be marked with the 'async' modifier . How can I make this code compilable? class Program { static async void Main(string[] args) { Task<string> getWebPageTask = GetWebPageAsync("http://msdn.microsoft.com"); Debug.WriteLine("In startButton_Click before await"); string webText = await getWebPageTask; Debug.WriteLine("Characters received: " + webText.Length.ToString()); } private static async Task<string> GetWebPageAsync(string url) { // Start an async task. Task<string> getStringTask = (new

What actually happens when using async/await inside a LINQ statement?

空扰寡人 提交于 2019-11-27 20:30:25
The following snippet compiles, but I'd expect it to await the task result instead of giving me a List<Task<T>> . var foo = bars.Select(async bar => await Baz(bar)).ToList() As pointed out here , you need to use Task.WhenAll : var tasks = foos.Select(async foo => await DoSomethingAsync(foo)).ToList(); await Task.WhenAll(tasks); But a comment points out that the async and await inside the Select() is not needed: var tasks = foos.Select(foo => DoSomethingAsync(foo)).ToList(); A similar question here where someone tries to use an async method inside a Where() . So async and await inside a LINQ

Using async without await in C#?

两盒软妹~` 提交于 2019-11-27 19:42:49
Consider Using async without await . think that maybe you misunderstand what async does. The warning is exactly right: if you mark your method async but don't use await anywhere, then your method won't be asynchronous. If you call it, all the code inside the method will execute synchronously. I want write a method that should run async but don't need use await.for example when use a thread public async Task PushCallAsync(CallNotificationInfo callNotificationInfo) { Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId, } I want call PushCallAsync and run async