简体   繁体   English

从事务范围调用WCF服务方法

[英]Calling WCF service method from transaction scope

I have code like: 我的代码如下:

    using (TransactionScope scope = TransactionScopeFactory.CreateTransactionScope())
      {

        *// some methodes calls for which scope is needed*
        ...
        ...
        *//than WCF method code for which I don't want transaction to be involved, but if it throws  an exception I don't wish scope to be completed*
        WcfServiceInstance.SomeMethod();
        scope.Complete();
      }

My question is, can I call the WCF service method inside of the Transaction scope without any problems ? 我的问题是,我可以在Transaction范围内调用WCF服务方法而没有任何问题吗? (I don't know how the service method is implemented) Also, I want to be shure that Transaction will not be involved in wcf service method calling. (我不知道服务方法是如何实现的)另外,我想要避免Transaction不会涉及wcf服务方法调用。

WCF service methods can be transactional or not, depending on how they are implemented. WCF服务方法可以是事务性的,也可以不是,具体取决于它们的实现方式。 If you want to be sure that your service call does not participate in the transaction, wrap the service call in a "suppressed" transaction scope. 如果要确保服务调用不参与事务,请将服务调用包装在“已禁止”的事务范围中。 This will suppress any ambient transaction. 这将抑制任何环境事务。

using( new TransactionScope(TransactionScopeOption.Suppress) 
{
    WcfServiceInstance.SomeMethod()
}

To propagate a transaction from your client application ot the service you need to explicity opt-in to transaction flows on the serer and client. 要从客户端应用程序传播事务,您需要明确选择serer和客户端上的事务流。 If your client is using a transaction aware binding (NetTcp, NetNamedPipe, WSHttp, WSDualHttp, & WSFederation) then you should see a boolean property TransactionFlow . 如果您的客户端使用事务感知绑定(NetTcp, NetNamedPipe, WSHttp, WSDualHttp, & WSFederation)那么您应该看到一个布尔属性TransactionFlow Setting this to false will prevent any transactions from flowing from your cient to the server. 将此设置为false将阻止任何事务从您的客户端流向服务器。

You get some additional control on the operation level with the TransactionFlow attribute, but this is a server side attribute, so if you don't have access to the service code this likely isn't an option. 您可以使用TransactionFlow属性对操作级别进行一些额外控制,但这是服务器端属性,因此如果您无权访问服务代码,则可能不是一个选项。

Please let me know if the TransactionFlow attribute doesn't solve your problem. 如果TransactionFlow属性无法解决您的问题,请告诉我。 Understand that setting this to false on the client will prevent any & all transactions from being passed from client to service for that particular endpoint binding. 了解在客户端上将此设置为false将阻止任何和所有事务从客户端传递到该特定端点绑定的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM