c#-5.0

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

南笙酒味 提交于 2019-11-27 19:10:00
问题 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

Why can't “async void” unit tests be recognized?

China☆狼群 提交于 2019-11-27 18:54:51
async void unit tests cannot be run within Visual Studio 2012: [TestClass] public class MyTestClass { [TestMethod] public async void InvisibleMyTestMethod() { await Task.Delay(1000); Assert.IsTrue(true); } } If I want to have an asynchronous unit test, the test method has to return a Task: [TestMethod] public async Task VisibleMyTestMethod() { await Task.Delay(1000); Assert.IsTrue(true); } Why is it so? Not that I absolutely need to have an async void test method, I am just curious. Visual Studio 2012 gives no warning nor error when you build an async void test method even though it won't be

How do you implement an async action delegate method?

馋奶兔 提交于 2019-11-27 17:45:56
A little background information. I am learning the Web API stack and I am trying to encapsulate all data in the form of a "Result" object with parameters such as Success and ErrorCodes. Different methods however, would produce different results and error codes but the result object would generally be instantiated the same way. To save some time and also to learn more about async/await capabilities in C#, I am trying to wrap all the method bodies of my web api actions in an asynchronous action delegate but got caught in a bit of a snag... Given the following classes: public class Result {

How do I disable C# 6 Support in Visual Studio 2015?

笑着哭i 提交于 2019-11-27 17:08:44
Background We have a project that we're developing in VS 2015 with C#6 enabled that occasionally needs to be opened by developers using VS 2013 without C#6. We have no intention to use C# 6 within this particular solution (as much as I'd like to). Problem Visual Studio and ReSharper suggest helpful C# 6 language constructs that render the solution inoperable in earlier versions of Visual Studio without C#6 support. I've disabled the ReSharper C#6 support but I can't seem to disable / limit C# features across the whole solution. Question How do I limit C# to C#5 capabilities within a solution

A good solution for await in try/catch/finally?

情到浓时终转凉″ 提交于 2019-11-27 17:00:56
I need to call an async method in a catch block before throwing again the exception (with its stack trace) like this : try { // Do something } catch { // <- Clean things here with async methods throw; } But unfortunately you can't use await in a catch or finally block. I learned it's because the compiler doesn't have any way to go back in a catch block to execute what is after your await instruction or something like that... I tried to use Task.Wait() to replace await and I got a deadlock. I searched on the Web how I could avoid this and found this site . Since I can't change the async methods

Why doesn't generic ICollection implement IReadOnlyCollection in .NET 4.5?

一曲冷凌霜 提交于 2019-11-27 13:00:17
In .NET 4.5 / C# 5, IReadOnlyCollection<T> is declared with a Count property: public interface IReadOnlyCollection<out T> : IEnumerable<T>, IEnumerable { int Count { get; } } I am wondering, wouldn't it have made sense for ICollection<T> to implement the IReadOnlyCollection<T> interface as well: public interface ICollection<T> : IEnumerable<T>, IEnumerable, *IReadOnlyCollection<T>* This would've meant that classes implementing ICollection<T> would've automatically implemented IReadOnlyCollection<T> . This sounds reasonable to me. The ICollection<T> abstraction can be viewed as an extension of

Explicitly use a Func<Task> for asynchronous lambda function when Action overload is available

ε祈祈猫儿з 提交于 2019-11-27 11:56:45
问题 Reading over this blog post on some of the gotchas of C#5's async/await. It mentions in Gotcha #4 something that is quite profound and that I hadn't thought of before. Briefly, it covers the scenario where you have a method that has two overloads, one that takes an Action and one that takes a Func<Task> (for example Task.Run ). This issue is rooted in the argument that async void methods should only be used for event handlers, with the post then going on to portray the following scenario -

call async method without await #2

假如想象 提交于 2019-11-27 11:54:10
I have an async method: public async Task<bool> ValidateRequestAsync(string userName, string password) { using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(url); string stringResponse = await response.Content.ReadAsStringAsync(); return bool.Parse(stringResponse); } } I call this method like this: bool isValid = await ValidateRequestAsync("user1", "pass1"); Can i call the same method from an synchronous method, without using await keyword? Ex: public bool ValidateRequest(string userName, string password) { return ValidateRequestAsync(userName,

Converting ordinary Http Post web request with Async and Await

醉酒当歌 提交于 2019-11-27 11:46:55
问题 How I can convert my traditional HttpWebRequest "POST" call with Async / Await pattern, Here with this I am attaching my current code, Any one please help me to convert this code using Async / Await pattern for windows phone 8. public void GetEnvironmentVariables(Action<Credentials> getResultCallback, Action<Exception> getErrorCallback) { CredentialsCallback = getResultCallback; ErrorCallback = getErrorCallback; var uri = new Uri(BaseUri); var request = (HttpWebRequest)WebRequest.Create(uri);

How to force async child overrides in C# 5.0

被刻印的时光 ゝ 提交于 2019-11-27 11:27:34
问题 I'm working on a system in which multiple client objects are expected to implement a particular function via an interface, and I want that function to run asynchronously with continuations (I'm expecting the implementations to be I/O-bound and want to ensure that all the client objects complete this function as soon as possible). I'm using the Visual Studio Async CTP Refresh for SP1, with C# "5.0". What is the recommended practice for enforcing asynchronous behavior in child objects of my