简体   繁体   中英

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 ? (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.

WCF service methods can be transactional or not, depending on how they are implemented. 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. If your client is using a transaction aware binding (NetTcp, NetNamedPipe, WSHttp, WSDualHttp, & WSFederation) then you should see a boolean property TransactionFlow . Setting this to false will prevent any transactions from flowing from your cient to the server.

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.

Please let me know if the TransactionFlow attribute doesn't solve your problem. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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