SaxonCS seems to lock output files
Added by Nick Thijssen over 2 years ago
I convert several files and this seems to work OK. However when we try to move those files from the output folder they seem to be locked. And these files remain locked until i kill my service and restart it. Am i doing something wrong?
Directory.CreateDirectory(job.InputPath);
Directory.CreateDirectory(job.InputPath + "\\backup\\");
Directory.CreateDirectory(job.OutputPath);
_validator.ValidateAndThrow(job);
foreach (var fileName in Directory.GetFiles(job.InputPath).Where(x => x.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase)))
{
try
{
var originalFileInfo = new FileInfo(fileName);
var backuppath = job.InputPath + "\\backup\\" + originalFileInfo.Name;
var originalText = File.ReadAllText(originalFileInfo.FullName);
File.WriteAllText(backuppath, originalText, new UTF8Encoding(false));
var fileInfo = new FileInfo(backuppath);
// Create a Processor instance.
Processor processor = new();
// Load the source document
XdmNode input = processor.NewDocumentBuilder().Build(new Uri(fileInfo.FullName));
// Create a transformer for the stylesheet
Xslt30Transformer transformer = processor.NewXsltCompiler().Compile(new Uri(job.ConversionFile)).Load30();
// Create a serializer
Serializer serializer = processor.NewSerializer();
string outfile = SetOutputFileName(job.OutputPath, fileInfo.Name, job.MaintainFileName, job.UseInFileOutputPath);
serializer.OutputFile = outfile;
serializer.SetOutputProperty(Serializer.INDENT, "yes");
transformer.ApplyTemplates(input, serializer);
originalFileInfo.Delete();
_logger.LogInformation("Output written to {outfile}", outfile);
output.Add(new JobResultDto
{
Success = true,
JobId = job.Id,
Message = $"Output written to {outfile}",
ErrorMessage = null,
Exception = null,
TimeStamp = DateTime.Now,
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
output.Add(new JobResultDto
{
Success = false,
JobId = job.Id,
Message = null,
ErrorMessage = "Unhandled Exception",
Exception = ex.ToString(),
TimeStamp = DateTime.Now
});
}
}
Replies (8)
Please register to reply
SaxonCS seems to lock output files - Added by Norm Tovey-Walsh over 2 years ago
Saxonica Developer Community notifications@plan.io writes:
I convert several files and this seems to work OK. However when we try
to move those files from the output folder they seem to be locked. And
these files remain locked until i kill my service and restart it. Am i
doing something wrong?
What platform are you running on? Windows is quite aggressive about file
locking, more so than some other platforms.
Be seeing you,
norm
--
Norman Tovey-Walsh ndw@nwalsh.com
https://nwalsh.com/
What is more wonderful than the delight which the mind feels when it
knows? This delight is not for anything beyond the knowing, but is in
the act of knowing. It is the satisfaction of a primary instinct.--Mark
Rutherford
RE: SaxonCS seems to lock output files - Added by Nick Thijssen over 2 years ago
It runs on a windows machine inside of a C# windows service
RE: SaxonCS seems to lock output files - Added by Martin Honnen over 2 years ago
It might be another variation of https://saxonica.plan.io/issues/5552, if you can try to make sure your code closes any output file explicitly.
RE: SaxonCS seems to lock output files - Added by Martin Honnen over 2 years ago
Perhaps with
// Create a serializer
Serializer serializer = processor.NewSerializer();
string outfile = SetOutputFileName(job.OutputPath, fileInfo.Name, job.MaintainFileName, job.UseInFileOutputPath);
var fs = File.Open(outfile, FileMode.Create, FileAccess.Write);
serializer.OutputStream = fs;
serializer.OnClose(() => { fs.Close(); });
serializer.SetOutputProperty(Serializer.INDENT, "yes");
transformer.ApplyTemplates(input, serializer);
RE: SaxonCS seems to lock output files - Added by Nick Thijssen over 2 years ago
Yes that works perfectly! thank you!
RE: SaxonCS seems to lock output files - Added by Nick Thijssen over 2 years ago
How would i inject a filestream if the xsl file contains a <xsl:result-document href="{$filename}">
RE: SaxonCS seems to lock output files - Added by Martin Honnen over 2 years ago
I used
transformer.ResultDocumentHandler = (href, baseUri) => {
var serializer = processor.NewSerializer();
var fs = File.Open(new Uri(baseUri, href).LocalPath, FileMode.Create, FileAccess.Write);
serializer.OutputStream = fs;
serializer.OnClose(() => { fs.Close(); });
return serializer;
};
in a test and someone else trying that approach in or related to the named bug entry also said that worked fine.
RE: SaxonCS seems to lock output files - Added by Nick Thijssen over 2 years ago
After some fiddling i also got that bit to work, thanks for the info!
Please register to reply