我有一个WCF服务,我在某个方法中调用另一个方法,但是使用Task.StartNew(.... 之后,WCF方法返回,客户端失去连接。
到目前为止一切都很好。
在另一个线程的此方法中,我使用ImageMagick.NET x64来调整jpg图像的大小。 使用该服务的最后一次,我得到一个带有以下StackTrace的ThreadAbortException:
Message: 'System.Threading.ThreadAbortException: Thread was being aborted.
at Magick.Image.write(Image* , Blob* )
at ImageMagick.MagickWriter.Write(Image* image, Blob* blob)
at ImageMagick.MagickWriter.Write(Image* image, Stream stream)
at My.Libs.MyService.Sync.Actions.ResizeAction.ExecuteAction(Stream sourceStream)
另一个人也出现了:
Message: 'System.Threading.ThreadAbortException: Thread was being aborted.
at Magick.Image.resize(Image* , Geometry* )
at ImageMagick.MagickImage.Resize(MagickGeometry geometry)
at My.Libs.MyService.Sync.Actions.ResizeAction.ExecuteAction(Stream sourceStream)
所以有一次是Image.Resize-method,另一次是Image.Write-method。
但大多数时候,代码运行得很好。
我的问题: 很明显,这些特定的方法导致异常导致ThreadAbortException? 或者是否有可能来自IIS / WFF的expecion commes,它中止了Operation(例如App Pool Recycle,Extecution timeout超时等等? 我应该开始调查吗?
更新: 我没有等待的StartNew,所以调用将很快完成,只有任务正在运行(或应该),直到他完成哈希,代码:
// Start method
Task.Factory
.StartNew(() => this.Sync(0, syncTaskStatus))
.ContinueWith(task => blSyncTaskInstance.SetFinished(syncTaskId));
// End method
现在......
Task.Factory
.StartNew(() => this.Sync(0, syncTaskStatus), TaskCreationOptions.LongRunning)
.ContinueWith(task => blSyncTaskInstance.SetFinished(syncTaskId));
更新二 - 错误发生的代码段和服务内容
public Stream ExecuteAction(Stream sourceStream)
{
var memoryStream = new MemoryStream();
try
{
using (MagickImage image = new MagickImage(sourceStream))
{
int targetWidth = this._width;
int targetHeight = this._height;
string geometryString;
if (targetWidth == 0)
{
geometryString = "x" + targetHeight.ToString();
}
else if (targetHeight == 0)
{
geometryString = targetWidth.ToString();
}
else
{
throw new Exception(
string.Format(
"Weder die Höhen oder, noch die Breitenangabe ist 0, was bei ResizeAction der Fall sein muss! Width: '{0}', Height: '{1}'",
targetWidth, targetHeight));
}
var resizeGeometry = new MagickGeometry(geometryString);
image.Resize(resizeGeometry);
// Auf Wunsch progressiv rendern.
if (this._renderProgressive)
{
image.Interlace = Interlace.Line;
}
// Alle Bildprofile und Kommentare löschen.
image.Strip();
// Behebt falsche DPI Angabe mit Division durch Null in Crystal Reports.
image.Density = new MagickGeometry(300, 300);
image.Write(memoryStream);
}
}
catch (Exception ex)
{
sourceStream.Dispose();
LogService.Logger.Log(string.Format("ResizeAction failed, message: {0}", ex.Message), LogLevel.Error, ex);
throw;
}
return memoryStream;
}
这是代码,其中调用ExecuteAction:
private Stream GetStreamChainResult(Stream sourceStream, List<IAction> preCopyActions)
{
Stream workingStream = sourceStream;
foreach (var action in preCopyActions)
{
workingStream = action.ExecuteAction(workingStream);
workingStream.Position = 0;
}
return workingStream;
}
最诚挚的问候。