sharp architecture contrib transaction attribute in windows service

335 views Asked by At

For some reason this:

[Transaction]
public void DoSomething()
{
    ...
}

does not work I still have to explicitly use the transaction like this:

public void DoSomething()
{
    using (var tx = NHibernateSession.Current.BeginTransaction())
    {
        ....
        tx.Commit();
    }
}

Any ideas why?

I am using something like this to bootstrap stuff:

_container = new WindsorContainer();
ComponentRegistrar.AddComponentsTo(_container);

...

ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(_container));

ComponentRegistrar.AddComponentsTo(_container, typeof(NHibernateTransactionManager));

NHibernateSession.Init(new ThreadSessionStorage(),
        new[] { "Bla.Domain.dll" },
        new AutoPersistenceModelGenerator().Generate(),
        "NHibernate.config");
2

There are 2 answers

3
Doan Van Tuan On

Normally, this kind of problem is caused by the failure of invoking the dynamic proxy that provides the transaction management service. Two of the most common errors are:

  • The method cannot be proxied: most likely not implement any interface method, or the object was not proxied.
  • The method was called from the same class, which bypassed all proxies.

Edit:

I guess you use Castle Windsor as IoC container. The [Transaction] decoration requires the Automatic Transaction Management Facility in order to work. If you successfully configured the facility, i.e. you made [Transaction] work in one method, but not other, then the answer above applies. If all Transaction decoration failed to work, then you have to review the configuration of the facility first.

4
Seif Attar On

As Doan said the component that had the method is not proxied.

Since the method is not virtual, I am assuming that your class is implementing an interface. make sure that you have the dependency in the class calling DoSomething defined as the interface and not the implementing class.

if you debug the code, and check the run time type of the object, it should be a castle proxy

for more details check the trouble shooting section on Sharp Architecture contrib wiki https://github.com/sharparchitecture/Sharp-Architecture-Contrib/wiki/Troubleshooting