c# - Merging XPS files leaves open file handles to document references -


i merging xps files via iis web application during merging process, leaves document reference file handles open. in windows wpf or console application, wouldn't worry handles released after application closed. recycling app pool close handles too. however, can't recycle app pool closing file handles due caching , performance.

is there way close these file handles after merge complete? i've tried few different merging approaches, following code has given me best results:

private documentpaginator createmergeddocument(ienumerable<string> xpsfiles) {      using ( memorystream mergedstream = new memorystream() ) {     using ( var pkg = package.open( mergedstream, filemode.create, fileaccess.readwrite ) ) {       var pack = "pack://merged.xps";       var uri = new uri( pack, urikind.absolute );       packagestore.addpackage( uri, pkg );       using ( xpsdocument mergeddocument =            new xpsdocument( pkg, compressionoption.maximum, pack ) ) {         fixeddocumentsequence seqnew = new fixeddocumentsequence();          foreach ( string sourcedocument in xpsfiles ) {           using ( xpsdocument xpsold =                new xpsdocument( sourcedocument, fileaccess.read ) ) {             fixeddocumentsequence seqold = xpsold.getfixeddocumentsequence();              foreach ( documentreference dr in seqold.references ) {               documentreference newdocumentreference = new documentreference();               newdocumentreference.source = dr.source;               ( newdocumentreference iuricontext ).baseuri =                ( dr iuricontext ).baseuri;               seqnew.references.add( newdocumentreference );             }           }         }          xpsdocumentwriter xpswriter = xpsdocument.createxpsdocumentwriter( mergeddocument );         xpswriter.write( seqnew );          packagestore.removepackage( uri );          return seqnew.documentpaginator;       }     }   } } 

i tracked down file handles opening xpswriter.write(seqnew) line of code. expected needs load files copy new fixeddocumentsequence. however, nice if xpsdocumentwriter disposable , cleaned unmanaged resources/file handles.

during research, have tried few other approaches. 1 approach i've tried writing each page visual serializerwritercollator. approach i've looked processing fixedpage contents , updating uris copied image , font streams , writing raw xaml xmlwriter. neither have worked well. page visuals , serializerwritercollator, getting incorrect page sizes on merged xps , cutting off content. raw xmlwriter approach, jumbling of images in merged xps , incorrectly displaying of page content.

after more research, found original code used running function in sta thread not releasing file handles , unmanaged resources. had custom threading class custom implementation of invoking action , running join on thread. thread not exiting correctly after action method completed. fix this, wrapped code in block similar this:

private static readonly taskscheduler _stascheduler = new stataskscheduler( 1 );  [route( "merge" )] [httpget] public async task<httpresponsemessage> mergexps() {   var paginator = await task<documentpaginator>.factory.startnew(     () =>       {         var xpsfiles = directory.getfiles( "c:\\xps", "*.xps" );         var documentpaginator = createmergeddocument( xpsfiles );          return documentpaginator;       },     cancellationtoken.none,     taskcreationoptions.none,     _stascheduler );    var response = new httpresponsemessage();   response.content = new stringcontent( $"merged document page count: {paginator.pagecount}" );   response.statuscode = httpstatuscode.ok;   return response; } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -