C# 是什么污染了这个开关语句?
我有一个系统在做以下工作C# 是什么污染了这个开关语句?,c#,multithreading,windows-services,task-parallel-library,aspose,C#,Multithreading,Windows Services,Task Parallel Library,Aspose,我有一个系统在做以下工作 将文档上载到SharePoint 事件接收器将把作业添加到数据库中,在文档转换目录中为作业创建一个文件夹 目录监视程序将触发文档转换windows服务 Windows服务将从DB获得一批10个作业(使用主线程) 启动时Windows服务根据处理器的内核创建X个线程(使用并行处理) 然后为每个db作业创建带超时的工作线程(这与线程的并行不同) 而且它还在继续 哦,转换时。。。在工作线程中。。我们正在调用ActiveDirectory,登录到DB(读、写)并将文档上载回S
- 将文档上载到SharePoint
- 事件接收器将把作业添加到数据库中,在文档转换目录中为作业创建一个文件夹
- 目录监视程序将触发文档转换windows服务
- Windows服务将从DB获得一批10个作业(使用主线程)
- 启动时Windows服务根据处理器的内核创建X个线程(使用并行处理)
- 然后为每个db作业创建带超时的工作线程(这与线程的并行不同)
- 而且它还在继续李>
- 哦,转换时。。。在工作线程中。。我们正在调用ActiveDirectory,登录到DB(读、写)并将文档上载回SharePoint
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(流 文档内容)在
正如您所看到的,您在多个线程中使用同一个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;}
}
如果将Parallel.For循环更改为标准For循环,问题是否仍然存在?这将帮助您诊断是否存在线程安全问题。谢谢,除此之外,我正在考虑将文档转换移动到完全不同的服务,我们的windows服务正在转换文档,读取和写入数据库和SharePoint,并调用active directory+登录到文件系统。当你看到它时,你会建议任何只用于文档转换的架构:)@Mathematics Aspose.Words API确实支持多线程,你唯一需要注意的是,一次只能有一个线程访问文档对象。例如,您可以编写一个方法,在该方法中初始化文档的新实例,并将输出保存在全局列表对象中(可在执行结束后访问),以获取所有输出流/文件。您可以按照上述指导原则,根据任务的要求实现多线程例程。我在Aspose担任支持开发人员。@AsadAli我可以根据给定的指南解决问题,但我们可以进行快速聊天吗?请我想讨论两件事,不要花费太多时间:)@Mathematics您可以随时提问,我相信您也在Aspose论坛上发布了类似的查询。虽然您可能会在这里询问,但在论坛上,我相信我们可以相应地回复您更多详细信息和有用信息。:)请加入这个房间-