【发布时间】:2012-05-22 23:00:14
【问题描述】:
我在生产环境中遇到了一个我没有在本地解决的问题。
我正在 TransactionScope 中运行一些 LINQ to SQL 代码,如下所示:
using (var scope = new TransactionScope())
{
uploadRepository.SubmitChanges();
result = SubmitFileResult.Succeed();
ScanForNewData(upload);
scope.Complete();
}
ScanForNewData() 调用 GetSubmittedData()。如果 GetSubmitted() 发生异常,我们使用 Nlog 将错误写入文件、数据库并发送电子邮件:
catch (Exception ex)
{
//MT - having to comment this out beause it is causing a problem with transactions on theproduction server
logger.ErrorException(String.Format("Error reading txt file {0} into correct format", upload.DocumentStore.FileName), ex);
return new UploadGetSubmittedDataResult { Exception = ex, Success = false, Message = String.Format("Error reading txt file {0} into correct format", upload.DocumentStore.FileName) };
}
然后在 ScanForNewData 中,我们调用 repository.SubmitChanges()。这会导致:
该操作对事务的状态无效。 System.Transactions.TransactionException TransactionException System.Transactions.TransactionException: 该操作对于事务的状态无效。
我想出的最佳想法是,在生产环境中,此代码在 Web 服务器上运行并调用单独的数据库服务器。 DataContext 和 Nlog 都具有相同的连接字符串配置和 Sql 用户,但可能是因为服务器是远程的(而我在本地使用集成安全性)发生了一些奇怪的事情。
知道在这种情况下交易会发生什么吗?
更新 - 我刚刚在本地使用 SQL 用户进行了尝试,它仍然可以正常工作。一定与制作设置有关...
另一个更新 - 我撒谎。在开发过程中,Nlog 数据库记录永远不会被写入,电子邮件已发送,并且不会发生 TransactionException。
【问题讨论】:
标签: c# .net transactionscope nlog