【发布时间】:2016-02-16 14:11:55
【问题描述】:
我们正在尝试开发 WCF 服务来执行长时间运行的任务。如果任务已成功排队,实现长时间运行任务的方法会生成一个新任务并立即返回到客户端(在我们的例子中是一个 .aspx 页面)。该服务在自己的应用程序池上运行,没有回收和单个 InstanceContextMode。
WCF 服务
[OperationContract]
public bool testThreadAbortException()
{
Task.Factory.StartNew
(
() =>
{
try
{
//long operation
int i = 0;
while (i < 250)
{
int j = 0;
while (j < 2000000) j++;
i++;
}
ThreadState state = Thread.CurrentThread.ThreadState;
string dummy = "finished ";
}
catch(Exception exception)
{
ThreadState state = Thread.CurrentThread.ThreadState;
string msg = exception.Message;
Exception inner = exception.InnerException;
}
}
);
return true;
}
客户
protected void btnRun_Click(object sender, EventArgs e)
{
_default.IISHOST_ETLSchedulerServiceReference.ETLSchedulerServiceClient client = new _default.IISHOST_ETLSchedulerServiceReference.ETLSchedulerServiceClient();
bool ret = client.testThreadAbortException();
}
现在的问题是,在执行 testThreadAbortException 方法时,我捕获了线程被中止的异常(这总是在客户端退出事件处理程序方法后发生)。奇怪的是,这个异常只是第一次抛出(即如果我再次按下运行按钮,代码执行得很好)。我必须重新启动本地 IIS 才能再次复制错误。
- 有人知道为什么会这样吗??
- 除了切换到 Windows 服务之外,还有更好的方法来实现我要归档的内容吗??
标签: c# multithreading wcf service abort