I use Uncapsulator for testing private members.
in one of my use cases I saw this error:
Unable to cast object of type 'LINQPad.Uncapsulator.Uncapsulator' to type 'System.Runtime.CompilerServices.INotifyCompletion'
run this code in Linqpad to see the error:
await new RssService().Uncapsulate().IngestSubEpisodes(new List { new Episode { Id = "1" } });
public class RssService
{
private static readonly SemaphoreLocker _locker = new SemaphoreLocker();
private readonly DatabaseSimulator _context = new DatabaseSimulator();
protected async Task IngestSubEpisodes(List<Episode> episodes)
{
var options = new ParallelOptions { MaxDegreeOfParallelism = 1 };
await Parallel.ForEachAsync(episodes, options, async (episode, token) =>
{
var subEpisodes = await _locker.LockAsync(async () =>
{
return await _context.SubEpisodes;
});
});
}
}
public class Episode { public string Id; }
public class SubEpisode { public string Id; public string EpisodeId; }
public class SemaphoreLocker
{
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
public async Task<T> LockAsync<T>(Func<Task<T>> worker)
{
await _semaphore.WaitAsync();
try
{
return await worker();
}
finally
{
_semaphore.Release();
}
}
}
public class DatabaseSimulator
{
public Task<List> SubEpisodes
{
get
{
return Task.FromResult(new List { new SubEpisode { Id = "1" } });
}
}
}
I use Uncapsulator for testing private members.
in one of my use cases I saw this error:
Unable to cast object of type 'LINQPad.Uncapsulator.Uncapsulator' to type 'System.Runtime.CompilerServices.INotifyCompletion'
run this code in Linqpad to see the error:
await new RssService().Uncapsulate().IngestSubEpisodes(new List { new Episode { Id = "1" } });
public class RssService
{
private static readonly SemaphoreLocker _locker = new SemaphoreLocker();
private readonly DatabaseSimulator _context = new DatabaseSimulator();
}
public class Episode { public string Id; }
public class SubEpisode { public string Id; public string EpisodeId; }
public class SemaphoreLocker
{
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
}
public class DatabaseSimulator
{
public Task<List> SubEpisodes
{
get
{
return Task.FromResult(new List { new SubEpisode { Id = "1" } });
}
}
}