async-await

Task when all, connection is closing

空扰寡人 提交于 2019-12-22 05:11:20
问题 I'm trying to execute multiple SqlDataReaders using Task.WhenAll. But when the tasks are awaited I get "System.InvalidOperationException: Invalid operation. The connection is closed". Creation of tasks: List<Task<SqlDataReader>> _listTasksDataReader = new List<Task<SqlDataReader>>(); _listTasksDataReader.Add(GetSqlDataReader1(10)); _listTasksDataReader.Add(GetSqlDataReader2(10)); SqlDataReader[] _dataReaders = await Task.WhenAll(_listTasksDataReader); My "SqlDataReader" methods: public Task

Cancel task.delay without exception or use exception to control flow?

故事扮演 提交于 2019-12-22 04:49:28
问题 I'm unsure about two possibilities to react to an event in my code. Mostly I'm concerned about which one needs less resources. I have a method with an observer registered to an eventproducer . If the eventproducer returns something, the method exits and the caller of the method starts the method again (you can think of it as a kind of a long polling). The eventproducer sometimes fires lots of events per seconds and sometimes rests for minutes. The first approach was to await a delay of 500ms

Async/Await not working as expected with Promise.all and a .map function

烈酒焚心 提交于 2019-12-22 04:49:04
问题 I have a slew of async functions I'm using and I'm having a weird issue. My code, working, looks like: async mainAsyncFunc (metadata) { let files = metadata.map(data => this.anotherAsyncFunc(data.url)); return Promise.all(files); } anotherAsyncFunc function looks like: async anotherAsyncFunc (url) { return await axios({ url, }).then(res => res.data) .catch(err => { throw err; }); } My issue comes when I try to append more data to what the first function ( mainAsyncFunc ) returns. My thoughts

How to unit test Promise catch() method behavior with async/await in Jest?

南楼画角 提交于 2019-12-22 04:36:11
问题 Say I have this simple React component: class Greeting extends React.Component { constructor() { fetch("https://api.domain.com/getName") .then((response) => { return response.text(); }) .then((name) => { this.setState({ name: name }); }) .catch(() => { this.setState({ name: "<unknown>" }); }); } render() { return <h1>Hello, {this.state.name}</h1>; } } Given the answers below and bit more of research on the subject, I've come up with this final solution to test the resolve() case: test.only(

Async/Await with Entity Framework 6.1.1 and impersonation

ε祈祈猫儿з 提交于 2019-12-22 04:32:06
问题 I have a WCF service hosted in IIS that is retrieving data from multiple sources (all SQL Server). With each data source, I have to impersonate a different Active Directory user to connect to the database. I am using Entity Framework v6.1.1 for two of the data sources. Integrated Security is set to True in the connection strings, too. I use the example below to set the impersonated user, where the impersonated user is a System.Security.Principal.WindowsImpersonationContext that I set from

Async/Await with Entity Framework 6.1.1 and impersonation

不羁岁月 提交于 2019-12-22 04:32:01
问题 I have a WCF service hosted in IIS that is retrieving data from multiple sources (all SQL Server). With each data source, I have to impersonate a different Active Directory user to connect to the database. I am using Entity Framework v6.1.1 for two of the data sources. Integrated Security is set to True in the connection strings, too. I use the example below to set the impersonated user, where the impersonated user is a System.Security.Principal.WindowsImpersonationContext that I set from

Can't Inspect Variables When Debugging .NET Async/Await Functions

Deadly 提交于 2019-12-22 04:30:16
问题 I have a .NET 4.5 proj that is using async/await functionality. When i try to inspect/quickwatch a variable reference after an await statement, i get the following: The name 'id' does not exist in the current context Know how to fix this so i can debug it? Edit-- here's the code [Fact] public async Task works_as_expected() { var repo = Config.Ioc.GetInstance<IAsyncRepository<Customer>>(); var work = Config.Ioc.GetInstance<IUnitOfWork>(); Customer c= new Customer() { FirstName = "__Micah",

How do I force a Task to stop?

徘徊边缘 提交于 2019-12-22 04:29:04
问题 I am using a Task with a CancellationTokenSource provided, and within my task I always check if cancellation is requested and stop executing if requested - in the parts of the code that I control. The problem is that within this task, I use very long running methods from libraries that don't support cancelling and were compiled for .NET 3.5 (I'm using 4.5.1). Depending on the input, these methods can run for several minutes (sometimes 10+). I don't want to let so much processing go to waste

How to cancel a TaskCompletionSource using a timout

痞子三分冷 提交于 2019-12-22 04:12:12
问题 I have the function that I call asynchronously using the await key word: public Task<StatePropertyEx> RequestStateForEntity(EntityKey entity, string propName) { var tcs = new TaskCompletionSource<StateInfo>(); try { var propInstance = BuildCacheKey(entity, propName); StateCacheItem cacheItem; if (_stateCache.TryGetValue(propInstance, out cacheItem)) { tcs.SetResult( new StateInfo (cacheItem.State.Name, cacheItem.State.Value) ); return tcs.Task; } //state not found in local cache so save the

How to cancel a TaskCompletionSource using a timout

馋奶兔 提交于 2019-12-22 04:11:04
问题 I have the function that I call asynchronously using the await key word: public Task<StatePropertyEx> RequestStateForEntity(EntityKey entity, string propName) { var tcs = new TaskCompletionSource<StateInfo>(); try { var propInstance = BuildCacheKey(entity, propName); StateCacheItem cacheItem; if (_stateCache.TryGetValue(propInstance, out cacheItem)) { tcs.SetResult( new StateInfo (cacheItem.State.Name, cacheItem.State.Value) ); return tcs.Task; } //state not found in local cache so save the