是什么污染了这个开关语句?



我有一个系统在做以下工作,

  • 将文档上载到 SharePoint
  • 事件接收器会将作业添加到数据库,在文档转换目录中为作业创建一个文件夹
  • 目录观察程序将触发文档转换窗口服务
  • Windows 服务将从数据库获取一批 10 个作业(使用主线程(
  • 启动时,Windows 服务根据处理器的内核创建 X 个线程(使用并行(
  • 然后为每个数据库作业创建超时的工作线程(这与并行线程不同(
  • 它继续...
  • 哦,在转换时...在工作线程中..我们正在调用ActiveDirectory,记录到DB(读,写(并将文档上传回SharePoint

我设法打破它...如果我上传受密码保护的文档...上传PowerPoint文档后不久,PowerPoint文档会抛出密码不正确的异常等。

但是,如果两个文档之间有间隙,即使是 60 秒,它也可以正常工作,这意味着 PowerPoint 文档确实会转换为 PDF。

以下是代码,但我不得不从中删除不必要的部分,

这是事情开始的主要类,

Parallel.For(0, noOfThreadsToRunOnPDFServer, new ParallelOptions { MaxDegreeOfParallelism = noOfThreadsToRunOnPDFServer },
i =>
{
this.docConvService.ProcessDocuments(i);
});

然后转换就在这里发生...

using System;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Threading;
namespace PDFService
{
public class AsposePDFConverter : IPDFConverter
{
private IDocConversionSettings settings;
private ExceptionDispatchInfo conversionException = null;
public enum SupportedExtensions
{
Doc,
Docx,
Xls,
Xlsx,
Pdf,
Pps,
Ppsx,
Ppt,
Pptx,
Txt,
Html,
Mhtml,
Xhtml,
Msg,
Eml,
Emlx,
One,
Vsd,
Vsdx,
Vss,
Vssx
}
public AsposePDFConverter(IDocConversionSettings settings)
{
this.settings = settings;
}
private void SyncThreadStartWithTimeout(ThreadStart threadStart, TimeSpan timeout)
{
Thread workerThread = new Thread(threadStart);
workerThread.Start();
bool finished = workerThread.Join(timeout);
if (!finished)
{
workerThread.Abort();
throw new ConversionTimeoutException("PDF Conversion exceeded timeout value");
}
}
public MemoryStream ConvertToPDF(string documentName, Stream docContent, double timeoutMS)
{
this.conversionException = null;
MemoryStream outStream = null;
MemoryStream inStream = new MemoryStream();
docContent.CopyTo(inStream);
inStream.Seek(0, SeekOrigin.Begin);
SupportedExtensions documentExtension;
string szExtension = Path.GetExtension(documentName).TrimStart('.');
if (Enum.TryParse(szExtension, true, out documentExtension))
{
switch (documentExtension)
{
case SupportedExtensions.Doc:
case SupportedExtensions.Docx:
case SupportedExtensions.Txt:
case SupportedExtensions.Html:
case SupportedExtensions.Mhtml:
case SupportedExtensions.Xhtml:
SyncThreadStartWithTimeout(
() => { outStream = ConvertWordsToPDF(inStream); },
TimeSpan.FromMilliseconds(timeoutMS));
break;
case SupportedExtensions.Pps:
case SupportedExtensions.Ppsx:
case SupportedExtensions.Ppt:
case SupportedExtensions.Pptx:
SyncThreadStartWithTimeout(
() => { outStream = ConvertSlidesToPDF(inStream); },
TimeSpan.FromMilliseconds(timeoutMS));
break;
}
// Conversion happens on sub-threads so they can time out, if they throw an exception, throw it from this thread
if (this.conversionException != null)
this.conversionException.Throw();
return outStream;
}
else
{
throw new FormatNotSupportedException("Document type is not supported");
}
}
private MemoryStream ConvertWordsToPDF(Stream docContent)
{
try
{
Aspose.Words.License lic = new Aspose.Words.License();
lic.SetLicense(this.settings.AsposeLicensePath);
Aspose.Words.Document doc = new Aspose.Words.Document(docContent);
MemoryStream stream = new MemoryStream();
doc.Save(stream, Aspose.Words.SaveFormat.Pdf);
return stream;
}
catch (Exception ex)
{
this.conversionException = ExceptionDispatchInfo.Capture(ex);
return null;
}
}
private MemoryStream ConvertSlidesToPDF(Stream docContent)
{
try
{ 
Aspose.Slides.License lic = new Aspose.Slides.License();
lic.SetLicense(this.settings.AsposeLicensePath);
using (Aspose.Slides.Presentation presentation = new Aspose.Slides.Presentation(docContent))
{
MemoryStream stream = new MemoryStream();
presentation.Save(stream, Aspose.Slides.Export.SaveFormat.Pdf);
return stream;
}
}
catch (Exception ex)
{
this.conversionException = ExceptionDispatchInfo.Capture(ex);
return null;
}
}
}
}

错误是,

文档 PDF 转换过程中出错。 详细信息是: PDFConversionID: 6061, 文档名称: powerpoint.ppsx, 网址: 已删除, 上传者: 已删除,转换持续时间:00:01:06.3072410

Aspose.Words.IncorrectPasswordException:文档密码为 不對。 在Aspose.Words.Document。(流,加载选项(
在Aspose.Words.Document。(流,加载选项(在 DocumentPDFConversionService.AsposePDFConverter.ConvertWordsToPDF(Stream 文档内容(在...

正如你所看到的,有一些非常可疑的事情正在发生

您在多个线程中使用 this.docConvService 的同一实例,因此您的 conversionException 属性可能是在处理其他文档时由受密码保护的文档写入的。您应该实例化AsposePDFConverter的新实例,或者更改返回异常的方式,例如在ConvertToPDF返回的结果对象中,其中包含MemoryStream和您的错误。

每个请求的单独实例:

Parallel.For(0, noOfThreadsToRunOnPDFServer, new ParallelOptions { MaxDegreeOfParallelism = noOfThreadsToRunOnPDFServer },
i =>
{
new AsposePdfConverter(settings).ProcessDocuments(i);
});

返回一个结果对象:

public ConversionResult ConvertToPDF(string documentName, Stream docContent, double timeoutMS)
{
/** Your code **/
return new ConversionResult() 
{
MemoryStream = memoryStream,
ConversionException = conversionException
};
}
class ConversionResult {
MemoryStream MemoryStream {get;set;}
ExceptionDispatchInfo ConversionException {get;set;}
}

最新更新