async-await

Reusable test code that waits for IO

旧城冷巷雨未停 提交于 2019-12-23 16:34:11
问题 I'm experimenting with using async/await on WCF exposed methods/services. Everything works fine but I'd like to simulate the service method actually waiting for IO so that the service call will be registered with an IO completion port, and the thread put back into the thread pool. To clarify, I'm just experimenting to confirm usage of IO completion ports and to get a better understanding of the mechanics of what's actually going on. So e.g. my test service currently looks like this:

How to display images from Picture Directory ?

删除回忆录丶 提交于 2019-12-23 16:34:10
问题 I want to display pictures in the Pictures library. I get pictures and bind data. StorageFolder picturesFolder = KnownFolders.PicturesLibrary; IReadOnlyList<StorageFile> myPictures = await picturesFolder.GetFilesAsync(); var mydata = from file in myPictures select new { Subtitle = "subtitle", Title = "title", Image = this.getImage(file.Path) }; this.DefaultViewModel["Items"] = mydata; This is getImage() for set BitmapImage. private async Task<BitmapImage> getImage(string finename) {

Simplify async-await with array iteration in Typescript

拜拜、爱过 提交于 2019-12-23 16:04:41
问题 Is there a simpler way to express this syntax in Typescript, without Promise.all and Array.prototype.map ? const items = [...]; await Promise.all(items.map(async item => { doSomething(item); const result = await doSomethingAsync(item); doSomethingMore(result, item); }); 回答1: ES5 array methods don't fully support async and generator functions, that's one of the reasons why for and other loop statements should be preferred to forEach in ES6. In this case map is partially misused, because it

async WCF method WebOperationContext is null after await

笑着哭i 提交于 2019-12-23 15:14:21
问题 In the following example, the method is exposed as a WCF service operation and the service is hosted in IIS. On entry into the function the WebOperationContext.Current is set as expected. After the await has finished waiting, however, the WebOperationContext.Current is set to null. public async Task TestAsync() { //WebOperationContext.Current is set Task t = new Task(()=>Thread.Sleep(10000)); t.Start(); await t; //WebOperationContext.Current is null } This would seem to be a shortcoming, so I

async and await while adding elements to List<T>

ぃ、小莉子 提交于 2019-12-23 13:11:47
问题 I wrote method, which adds elements to the List from many sources. See below: public static async Task<List<SearchingItem>> GetItemsToSelect() { List<SearchingItem> searchingItems = new List<SearchingItem>(); foreach (Place place in await GetPlaces()) { searchingItems.Add(new SearchingItem() { IdFromRealModel=place.Id, NameToDisplay=place.FullName, ExtraInformation=place.Name, TypeOfSearchingItem=TypeOfSearchingItem.PLACE }); } foreach (Group group in await GetGroups()) { searchingItems.Add

Am I doing something wrong combining dotMemory, xUnit and async

两盒软妹~` 提交于 2019-12-23 12:33:03
问题 I have a unit test where I try to verify that I have disposed of a document that was once attached to the main user interface. The unit test has to be async in that everything needs to be run under an STA thread and I have to await the user interface being created. I have a helper that dispatches actions onto an STA thread. I create the memory object in the main body of the test and then pass it to the async methods as below. See the lines of code commented with ### to see the actual problem

How does c# handle async void

邮差的信 提交于 2019-12-23 12:06:28
问题 I generally program webservers and at first I thought that there must be continuous chain of methods that return task, so stuff up the stack may ask database if it is done. Recently I saw wpf code, that does something like that: public async void Execute(object parameter) { await ExecuteAsync(parameter); } Called in event handler. UI seems to be responsive, so I guess it does work. How does it work? How does this translate to aspnet? 回答1: I explain how async void methods work - and why they

Reentrancy in async/await?

a 夏天 提交于 2019-12-23 10:52:12
问题 I have a button which has an async handler which calls awaits on an async method. Here's how it looks like: private async void Button1_OnClick(object sender, RoutedEventArgs e) { await IpChangedReactor.UpdateIps(); } Here's how IpChangedReactor.UpdateIps() looks: public async Task UpdateIps() { await UpdateCurrentIp(); await UpdateUserIps(); } It's async all the way down. Now I have a DispatcherTimer which repeatedly calls await IpChangedReactor.UpdateIps in its tick event. Let's say I

Polly framework CircuitBreakerAsync does not retry if exception occur

空扰寡人 提交于 2019-12-23 10:48:25
问题 I am using Polly framework for transient fault handling. For synchronous operations Polly circuit breaker policy works fine but when I created its async version it does not retries the execution. Kindly suggest : Asynchronous method : private async static Task HelloWorld() { if (DateTime.Now < programStartTime.AddSeconds(10)) { Console.WriteLine("Task Failed."); throw new TimeoutException(); } await Task.Delay(TimeSpan.FromSeconds(1)); Console.WriteLine("Task Completed."); } Polly circuit

HttpContext.Current is null after await completed in .NET 4.5

旧街凉风 提交于 2019-12-23 10:28:42
问题 I have the following simple WCF service defined in .NET 4.5 web app: [ServiceContract] public interface IService1 { [OperationContract] [WebGet(UriTemplate = "json/DoWork/", ResponseFormat = WebMessageFormat.Json)] Task<string> DoWork(); } public class Service1 : IService1 { public async Task<string> DoWork() { Debug.WriteLine(HttpContext.Current != null); var s = await new HttpClient().GetStringAsync("http://www.google.com"); Debug.WriteLine(HttpContext.Current != null); return s; } } And