google-code-export / unhaddins

Automatically exported from code.google.com/p/unhaddins
2 stars 1 forks source link

WCF and distributed transaction on MSMQ #25

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hello,

I found a problem with implementation NhSessionPerCallContextBehavior with WCF. 
Our WCF Service is working with MSMQ endpoint this endpoint is transactional. 
What mean message from it should be peek with transaction and when processing 
fails it goes back. For this situation it's important to add OperationBehavior 
like this [OperationBehavior(TransactionAutoComplete = true, 
TransactionScopeRequired = true)] on implementation of contract method. This 
mean that TransactionScope will be created. So when BeforeInvoke is called in 
class NhSessionPerCallContextBehavior transaction is opened. Then when 
AfterInvoke is called it is still opened and session should not be closed. 
Session if I am right (I've tested it and it works like I supposed) it's closed 
automatically by nhibernate implementation 
(AdoNetWithDistributedTransactionFactory). When this AfterInvoke is closing 
session after that we get exception from 
AdoNetWithDistributedTransactionFactory that says that session is closed  line 
looks like this                         using (var tx = new TransactionScope(AmbientTransation)) 
the problem is with this ambient transaction. We changed Your implementation of 
NhSessionPerCallContextBehavior and added one trick in line 49 if 
(!session.IsOpen || Transaction.Current != null). This solved our problems. If 
You have idea that this will have other consequences from do it like this 
please write. I hope this solution will be helpful.

Original issue reported on code.google.com by JS.Futur...@gmail.com on 17 Jun 2011 at 9:29

Attachments:

GoogleCodeExporter commented 9 years ago
The matter is that the behavior for NH should be executed "inside" the behavior 
for ambient transaction.
The BeforeInvoke of the ambient-transaction have to be executed before 
NhSessionPerCallContextBehavior.BeforeInvoke and the AfterInvoke of the 
ambient-transaction have to be executed after 
NhSessionPerCallContextBehavior.AfterInvoke.
Everything should work as expected, if you do it this way, without touch 
existing code.
Please try it and please let me know the result.

Original comment by fabiomaulo on 17 Jun 2011 at 12:00

GoogleCodeExporter commented 9 years ago
Proper file attached.

Original comment by JS.Futur...@gmail.com on 17 Jun 2011 at 12:00

Attachments:

GoogleCodeExporter commented 9 years ago
By deafult it is done like this. Because attribute of service operation first 
is opening TransactionScope and then calls to BeforeInvoke. This without 
changes from second file does not work. With my changes it forks properly. You 
can write simple test with TransactionScope and Your code and You will see this 
is not working like You expect. If You still do not belive me post comment and 
I will find some time to write test for it.

Original comment by JS.Futur...@gmail.com on 20 Jun 2011 at 2:09