async-await

Losing HttpContext with async await in ASP.NET Identity GetRolesAsync

三世轮回 提交于 2019-12-24 17:53:06
问题 This is more of an async/await question than ASP.NET Identity. I am using Asp.Net Identity, and have a custom UserStore, with a customized GetRolesAsync method. The UserManager is called from a WebApi controller. public class MyWebApiController { private MyUserManager manager = new MyUserManager(new MyUserStore()); [HttpGet] public async Task<bool> MyWebApiMethod(int x) { IList<string> roles = await manager.GetRolesAsync(x); return true; } } public class MyUserManager : UserManager<MyUser,

HttpContext.Current null in async after await calls

蹲街弑〆低调 提交于 2019-12-24 17:09:02
问题 HttpContext.Current null in async after await calls. Here is my code: if (!string.IsNullOrEmpty(securityGroupName)) { // To remove the domain name from the security group name. string securityGroupDisplayName = securityGroupName.Split('\\')[1]; string serviceSecurityGroupId = await this.graphApiClient.GetGroupIdAsync(securityGroupDisplayName).ConfigureAwait(false); if (!string.IsNullOrEmpty(serviceSecurityGroupId)) { Task securityGroupRoleAddTask = this.CheckMembershipAndAddRole

WriteAsync with timeout

亡梦爱人 提交于 2019-12-24 16:37:31
问题 I try to code a simple async write with timeout as below and expect the function to throw a TaskCanceledException given a very large buffer and small waitTime. However, this does not happen. WriteAsync will block for many seconds until the write completes. What am I missing? public async void WriteWithTimeout(Stream os, byte[] buf, int waitMs) { CancellationTokenSource tokenSource = new CancellationTokenSource(waitMs); // cancel after waitMs milliseconds. await os.WriteAsync(buf, 0, buf

SynchronizationLockException on Monitor.Exit when using await

爷,独闯天下 提交于 2019-12-24 16:35:10
问题 I am creating a piece of code that gets a webpage from a legacy system we have. In order to avoid excessive querying, I am caching the obtained URL. I am using Monitor.Enter , Monitor.Exit and double checking to avoid that request is issued twice, but when releasing the lock with Monitor.Exit , I am getting this exception: System.Threading.SynchronizationLockException was caught HResult=-2146233064 Message=Object synchronization method was called from an unsynchronized block of code. Source

array returns length of 0 although it has values

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 15:21:43
问题 so i am trying to push items to an array and it appears to be return a length of 0 although there are items in the array. let calendarDates = [] async function getDates() { const response = await fetch('/calendars/fetch_dates') let res = await response.json() res.forEach(el => { calendarDates.push(el) }) } getDates() createCalendar(date, side) . . . function createCalendar(date, side) { console.log('createCalendar', calendarDates, "is array?", Array.isArray(calendarDates), 'length',

How are asynchronous I/O methods processed

烂漫一生 提交于 2019-12-24 14:48:57
问题 After reading alot about async-await, I can only find the benefits of using it in GUI thread (WPF/WinForms). In what scenarios does it reduce the creation of threads in WCF services? Does a programmer must use async-await on every method in the service by choosing to implement async-await in web service? Making some non-async-await methods in a service full of async-await reduse the efficiency of my service? How? Last question - some say that using 'await Task.Run(()=>...)' is not a "real

Error: The operation was canceled

吃可爱长大的小学妹 提交于 2019-12-24 14:03:59
问题 I'm using this code snippet to do an async query with a cancellation token: var _client = new HttpClient( /* some setthngs */ ); _client.GetAsync(someUrl, cancellationToken).ContinueWith(gettingTask => { cancellationToken.ThrowIfCancellationRequested(); SomeStuffToDO(); }, TaskScheduler.FromCurrentSynchronizationContext()); }, TaskScheduler.FromCurrentSynchronizationContext()); But, when operation get cancelled, cancellationToken.ThrowIfCancellationRequested(); throws an exception. I know

Imitate MessageBox - waiting for user's choice

情到浓时终转凉″ 提交于 2019-12-24 13:37:22
问题 I'm working mostly with Windows Phone and I'm trying to create something similar to MessageBox - small window that apperas and waits for user's choice (the Thread that invoked window waits). I found three ways how I can achive this goal: FIRST - TaskCompletionSource In this case my Task looks like this: TaskCompletionSource<bool> taskComplete = new TaskCompletionSource<bool>(); private async Task myTask1() { window.Show(); // Show window await taskComplete.Task; //some job run after User's

EntityFramework (6) and async ( waitingForActivation)?

[亡魂溺海] 提交于 2019-12-24 13:26:23
问题 I've downloaded EF6 ( in order to use async ) So I wrote this simple method : public async Task<List<int>> MyasyncMethod() { var locations = await MyDumpEntities.AgeGroups.Select(f=>f.endYear).ToListAsync(); return locations; } ...Later... DumpEntities1 MyDumpEntities = new DumpEntities1(); var data = MyDumpEntities.AgeGroups.ToListAsync(); MyasyncMethod().ContinueWith(s => { Response.Write("f"); }); MyDumpEntities.Dispose(); But I don't see anything on the screen and when I inspect data I

MongoDB endless Find ToListAsync

梦想与她 提交于 2019-12-24 13:13:42
问题 I'm attempting to retrieve data from a MongoDB collection, however something strange is happening. If I show a MessageBox the data fetch works, if I don't it doesn't. static class MongoDBController { static MongoClient client = new MongoClient("mongodb://localhost"); public static async Task<List<Course>> GetCourses(string dbName = "school") { // Get our course collection var db = client.GetDatabase(dbName); var collection = db.GetCollection<Course>("courses"); // Create an empty filter var