c# - Stream.CopyToAsync never calls my WriteAynsc method -


i wrote custom stream class needs hit database read/write info. wanted use xxasync database methods, advised write both synchronous , asynchronous methods in stream since couldn't use connection.openasync() in synchronous read/write methods.

however, after implementing both versions, when use stream destination in call following in simple console application's void main() method:

task.run( async () => {     using ( var ctx = new jobcontext() )     {         // create assemblycache record , 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 call xxasync methods. trace synchronous versions. looking @ source stream base class, looks copytoasync should call writeasync method, i'm not sure i'm missing.

update

it turns out getbinarywritestream extension (intentionally) wraps gzipstream around custom stream support compression. gzipstream cause of not calling writeasync. i've asked new question specific here

you can use sqlclient's streaming support stream data client blov (ie varbinary(max)) column on server. use same code you'd use perform insert instead of using passing value sql command, pass stream or stream reader. example, instead of using

var sql="insert [mytable] (blobfield) values (@data)" using (sqlconnection conn = new sqlconnection(connectionstring))  using (sqlcommand cmd = new sqlcommand(sql, conn))  {     cmd.parameters.add("@data", sqldbtype.binary, mybuffer.length).value = mybuffer;     await conn.openasync();      await cmd.executenonqueryasync(); } 

you'd write

var sql="insert [mytable] (blobfield) values (@data)" using (sqlconnection conn = new sqlconnection(connectionstring))  using (sqlcommand cmd = new sqlcommand(sql, conn))  {     await conn.openasync();     using (filestream file = file.open("binarydata.bin", filemode.open))      {         cmd.parameters.add("@data", sqldbtype.binary, -1).value = file;         await cmd.executenonqueryasync();      } } 

if want send many files, can assign streams parameter in loop:

    var blobparam=cmd.parameters.add("@data", sqldbtype.binary, -1);     foreach(var somepath in alistofpaths)     {         using (filestream file = file.open(somepath, filemode.open))          {             blobparam.value = file;             await cmd.executenonqueryasync();          }      } 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -