事务范围wcf并避免不必要的升级到分布式事务

时间:2011-08-24 14:09:18

标签: wcf transactions transactionscope mstdc

我在wcf应用程序中升级分布式事务时遇到问题。

我的代码看起来像这样:

LogRepository

    public void AddLog(ImportLogDbo log)
    {
        using (DbManager dbManager = new DbManager("controlServerConnectionString"))
        {
            dbManager.SetSpCommand("dbo.ImportLog_Create",
                                   dbManager.Parameter("@ImportFile", log.ImportFile),
                                   dbManager.Parameter("@ImportError", log.ImportError),
                                   dbManager.Parameter("@ImportHash", log.ImportHash),
                                   dbManager.Parameter("@ImportCompleted", log.ImportCompleted))
             .ExecuteNonQuery();
        }
    }

LogService

    public void AddImportLog(string ImportFile, string ImportHash)
    {
        ImportLogDbo importLogDbo = new ImportLogDbo
        {
            ImportCompleted = false,
            ImportFile = ImportFile,
            ImportHash = ImportHash                
        };


        if (_importLogRepository.GetByFileName(ImportFile) == null)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                _importLogRepository.AddLog(importLogDbo);
                scope.Complete();
            }
        }

    }

项目在.NET 4中,我使用的是Sql Server 2008R2

上面的代码调用我的wcf服务,配置如下

  <wsHttpBinding>
    <binding name="wsHttpBinding" transactionFlow="True" maxReceivedMessageSize="2147483647" closeTimeout="5:00:00" openTimeout="5:00:00" receiveTimeout="5:00:00" />          
  </wsHttpBinding>

<endpoint address="http://localhost:8080/AppServer/WsDocumentImportService" binding="wsHttpBinding" name="wsBinding" contract="ComDocs.AppServerServiceLibary.Abstract.IDocumentImportService"/>

我的问题在于交易范围,它总是按分配推广。

我做错了什么? (我在wcf服务编程方面有点新鲜)

由于

1 个答案:

答案 0 :(得分:0)

transactionFlow =“true”会将事务传播到服务边界之外。尝试将其设置为false。您需要一个更精细的事务范围,您可以使用该属性设置它。欲了解更多信息,请查看以下内容:

http://msdn.microsoft.com/en-us/magazine/cc163432.aspx