【发布时间】:2017-07-24 09:56:20
【问题描述】:
我有一个系统正在执行以下操作,
- 将文档上传到 SharePoint
- 事件接收器将添加作业到数据库,在文档转换目录中为作业创建一个文件夹
- 目录观察器将触发 Document Conversion windows 服务
- Windows 服务将从 DB 中批量获取 10 个作业(使用主线程)
- 在启动时,Windows 服务会根据处理器的内核创建 X 个线程(使用 Parallel For)
- 然后为每个数据库作业创建具有超时的工作线程(这与并行线程不同)
- 它继续...
- 哦,在转换时……在工作线程中……我们正在调用 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,WebURL:已删除,上传者: 已移除,转换持续时间:00:01:06.3072410
Aspose.Words.IncorrectPasswordException: 文档密码是 不正确。在 Aspose.Words.Document。 (流,加载选项)
在 Aspose.Words.Document。 (流,加载选项)在 DocumentPDFConversionService.AsposePDFConverter.ConvertWordsToPDF(Stream docContent) 在...中
如您所见,发生了一些非常可疑的事情
【问题讨论】:
-
如果您将 Parallel.For 循环更改为标准 For 循环,问题是否仍然存在?这将帮助您诊断是否存在线程安全问题。
标签: c# multithreading windows-services task-parallel-library aspose