Dejo
Dejo

Reputation: 2148

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.

Upvotes: 2

Views: 3094

Answers (2)

Chris
Chris

Reputation: 2895

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.

Upvotes: 4

jlew
jlew

Reputation: 10601

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()
}

Upvotes: 8

Related Questions