async-await

Amazon S3 Async Upload using Task library

别来无恙 提交于 2019-12-21 21:20:02
问题 I have a windows form that upload file to Amazon S3. I tried to implement built in async method but seems not working fine so I think the best way would be to implement System.Threading.Tasks. My actual code looks like this: public void UploadFileAsync(string bucketName, CloudDocument doc, bool publicRead) { config = new AmazonS3Config(); config.CommunicationProtocol = Protocol.HTTP; client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, config); // Load stream

How to get this recursive async/await issue right?

大城市里の小女人 提交于 2019-12-21 20:43:30
问题 I have a recursive method that visits every node in a tree hierarchy and triggers a callback for every node, like (code below is not tested and an example): void Visit(Node node, Func<Node, int> callback, CancellationToken ct) { if(ct.IsCancellationRequested) { return; } var processedNode = DoSomeProcessing(node); int result = callback(processedNode); // Do something important with the returned int. ... // Recursion. foreach(var childNode in node.Children) { Visit(childNode, callback); } }

Generic constraint based on non-implementation of interface

风流意气都作罢 提交于 2019-12-21 20:29:18
问题 I have an application with a factory service to allow construction of instances while resolving the necessary dependency injection. For instance, I use this to construct dialog view models. I have a service interface that looks like this: public interface IAsyncFactory { Task<T> Build<T>() where T: class, IAsyncInitialize; } Ideally, what I'd like to have is something like this (pseudo-syntax, as this isn't directly achievable) public interface IFactory { Task<T> Build<T>() where T: class,

.Net Core Queue Background Tasks

浪子不回头ぞ 提交于 2019-12-21 18:32:57
问题 Slender answered my original question about what happens to fire and forget, after the HTTP Response is sent, but Now I'm left with the question how to properly queue background tasks EDIT As we all know Async void is generally bad, except for in the case when it comes to event handlers, I would like to execute some background logic without have to have the client wait. My original Idea was to use Fire and Forget Say I have an event: public event EventHandler LongRunningTask; And then someone

Parallel scraping in .NET

安稳与你 提交于 2019-12-21 18:05:12
问题 The company I work for runs a few hundred very dynamic web sites. It has decided to build a search engine and I was tasked with writing the scraper. Some of the sites run on old hardware and are not able to take much punishment, while others can handle massive amount of simultaneous users. I need to be able to say use 5 parallel requests for site A, 2 for site B and 1 for site C. I know I can use threads, mutexes, semaphores, etc. to accomplish this, but it will be quite complicated. Are any

CallContext.LogicalGetData gets restored even where there is no asynchrony. Why?

北城以北 提交于 2019-12-21 17:55:24
问题 I noticed that CallContext.LogicalSetData/LogicalGetData don't work the way I expected them to do. A value set inside an async method gets restored even when there is no asynchrony or any kind of thread switching , whatsoever. Here is a simple example: using System; using System.Runtime.Remoting.Messaging; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication { class Program { static async Task<int> TestAsync() { CallContext.LogicalSetData("valueX", "dataX"); //

Making Ninject Interceptors work with async methods

佐手、 提交于 2019-12-21 17:09:50
问题 I am starting to work with ninject interceptors to wrap some of my async code with various behaviors and am having some trouble getting everything working. Here is an interceptor I am working with: public class MyInterceptor : IInterceptor { public async void Intercept(IInvocation invocation) { try { invocation.Proceed(); //check that method indeed returns Task await (Task) invocation.ReturnValue; RecordSuccess(); } catch (Exception) { RecordError(); invocation.ReturnValue = _defaultValue;

Running an async function before express.js start

十年热恋 提交于 2019-12-21 16:57:04
问题 I want to run an async operation (for example, wait for a URL call to complete) before I start my app. I don't know how to do that (since it's an upper-level application - no async/await here). www.js: var app = require('./app'); var http = require('http'); const port = '3000'; app.set('port', port); var server = http.createServer(app); server.listen(port); app.js: var express = require('express'); var app = express(); var Promise = require('bluebird'); # HERE IS WHERE I WANT TO "AWAIT" AN

Running an async function before express.js start

柔情痞子 提交于 2019-12-21 16:56:06
问题 I want to run an async operation (for example, wait for a URL call to complete) before I start my app. I don't know how to do that (since it's an upper-level application - no async/await here). www.js: var app = require('./app'); var http = require('http'); const port = '3000'; app.set('port', port); var server = http.createServer(app); server.listen(port); app.js: var express = require('express'); var app = express(); var Promise = require('bluebird'); # HERE IS WHERE I WANT TO "AWAIT" AN

Sharing scope across awaits

 ̄綄美尐妖づ 提交于 2019-12-21 14:08:12
问题 I have a UserScope class which functions similarly to TransactionScope , i.e., it stores the current state in a thread local. This of course doesn't work across calls to await , and neither did TransactionScope until TransactionScopeAsyncFlowOption was added in .NET 4.5.1. What alternative to thread local can I use so that UserScope can be used the same in single-threaded and multi-threaded scenarios? (If I had 4.5.1 installed I'd decompile to see how TransactionScope does it.) This is a