I wrote a custom Stream class that needs to hit a database to read/write info. I wanted to use the XxAsync database methods, so I was advised to write both the synchronous and asynchronous methods in my stream since I couldn't use connection.OpenAsync()
in my synchronous Read/Write methods.
However, after implementing both versions, when I then use this stream as the destination in a call like the following in a simple Console application's void Main() method:
Task.Run( async () =>
{
using ( var ctx = new JobContext() )
{
// create AssemblyCache record and get assemblyKey...
using ( var fs = new System.IO.FileStream( @"C:\BTR\Source\Assemblies\BTR.Rbl.Evolution.Documents.dll",
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.None,
8192,
true ) )
using ( var bs = ctx.GetBinaryWriteStream<AssemblyCache>( assemblyKey ) )
{
await fs.CopyToAsync( bs );
}
}
} ).Wait();
It never seems to call my XxAsync methods. I only get trace for the synchronous versions. Looking at the source for Stream base class, it looks like CopyToAsync should call my WriteAsync method, so I'm not sure what I'm missing.
It turns out my GetBinaryWriteStream
extension (intentionally) wraps a GZipStream
around my custom stream to support compression. The GZipStream
was the cause of not calling WriteAsync
. I've asked a new question specific to this here