我在生产环境中遇到了一个问题,但在本地测试环境中没有出现过。
我正在使用如下代码在TransactionScope中运行一些LINQ to SQL代码:
using (var scope = new TransactionScope())
{
uploadRepository.SubmitChanges();
result = SubmitFileResult.Succeed();
ScanForNewData(upload);
scope.Complete();
}
ScanForNewData() 调用 GetSubmittedData()。如果在 GetSubmittedData() 中出现异常,我们使用 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没有发生。