MissingTransactionException when running NHibernate with Castle.Facilities.AutoTx
I'm trying to configure Castle Windsor with NHibernate and Castle.Facilities.AutoTx, but I keep getting problems with the PerTransactionLifeStyleOption
. If I do
var sessionFactory = Container.Resolve<Func<ISession>>();
var session = sessionFactory();
I get the following exception (on the second line above):
Castle.Facilities.AutoTx.MissingTransactionException: No transaction in context when trying to instantiate model 'NHibernate.ISession' for resolve type 'NHibernate.ISession'. If you have verified that your call stack contains a method with the [Transaction] attribute, then also make sure that you have registered the AutoTx Facility. at Castle.Facilities.AutoTx.Lifestyles.PerTransactionLifestyleManagerBase.Resolve(CreationContext context) in d:\Builds\Castle.Transactions-beta\src\Castle.Facilities.AutoTx\Lifestyles\PerTransactionLifestyleManagerBase.cs:line 153 at Castle.Facilities.AutoTx.Lifestyles.WrapperResolveLifestyleManager`1.Resolve(CreationContext context) in d:\Builds\Castle.Transactions-beta\src\Castle.Facilities.AutoTx\Lifestyles\WrapperResolveLifestyleManager.cs:line 143 at Castle.MicroKernel.Handlers.De开发者_Go百科faultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired) at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context, Boolean instanceRequired) at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context) at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments) at Castle.MicroKernel.DefaultKernel.Resolve(Type service, IDictionary arguments) at Castle.Facilities.TypedFactory.TypedFactoryComponent.Resolve(IKernel kernel) at Castle.Facilities.TypedFactory.Resolve.Invoke(IInvocation invocation) at Castle.Facilities.TypedFactory.TypedFactoryInterceptor.Intercept(IInvocation invocation) at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.Func`1Proxy.Invoke() at IntegrationTest.NCVIB.WindsorIoC.LocalDbTest.get_Reader() in D:\Projects\NCVIB-GIT\NCVIB\src\IntegrationTest.NCVIB\WindsorIoC\LocalDbTest.cs:line 22 at IntegrationTest.NCVIB.InspectionObjectMapTests.ReadWrite() in D:\Projects\NCVIB-GIT\NCVIB\src\IntegrationTest.NCVIB\InspectionObjectMapTests.cs:line 34
Here's a compilation of the setup calls I'm making (They're actually spread out across several different installers):
container.AddFacility<AutoTxFacility>();
container.Register(Component.For<INHibernateInstaller>().Instance(new FluentNHibernateInstaller));
container.AddFacility<NHibernateFacility>(fac => fac.Option = DefaultSessionLifeStyleOption.SessionPerTransaction);
The FluentNHibernateInstaller
is shown here:
public class FluentNHibernateInstaller : INHibernateInstaller
{
public FluentConfiguration BuildFluent()
{
return Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2005
.DefaultSchema("dbo")
.ConnectionString(b => b.Is(ConnectionString ?? ConnectionStringChooser.GetConnectionString())))
.Cache(c => c.UseQueryCache().ProviderClass<SysCacheProvider>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>().Conventions.AddFromAssemblyOf<EnumConvention>())
.ExposeConfiguration(
c => c.SetProperty(Environment.SqlExceptionConverter, typeof (MsSqlExceptionConverter).AssemblyQualifiedName))
.ExposeConfiguration(c => c.SetProperty(Environment.ShowSql, "true"));
}
public string ConnectionString { get; set; }
public void Registered(ISessionFactory factory)
{
}
public bool IsDefault
{
get { return true; }
}
public string SessionFactoryKey
{
get { return "default.sf"; }
}
public Maybe<IInterceptor> Interceptor
{
get { return Maybe.None<IInterceptor>(); }
}
}
It turns out that that the AutoTxFacility MUST be added to the container before any any component that has a [Transaction] attribute.
I was adding the facility in one of a few IWindsorInstaller classes in which the order of installation was "random" causing the facility to be installed AFTER some of my components with transactional methods:
var container = new WindsorContainer();
container.Install(FromAssembly.This);
But now I am adding the facility BEFORE installing from my installer classes:
var container = new WindsorContainer();
container.AddFacility<AutoTxFacility>();
container.Install(FromAssembly.This());
Here is what the usage would be like:
public class MyClass
{
private readonly Func<ISession> _sessionFactory;
public MyClass(Func<ISession> sessionFactory)
{
_sessionFactory = sessionFactory;
}
[Transaction]
public virtual void UseTheSessionForSomething()
{
// The transaction (and session) will be initialized
// because of the
// [Transaction] attribute
// AND this method is virtual
// AND this instance was resolved from the container
MethodWithinTransactionScope();
}
public void MethodWithinTransactionScope()
{
// Method just needs to be invoked in the scope of a transaction
var session = _sessionFactory();
session.Get<Entity>(1);
}
}
public void Test()
{
var myInstance = container.Resolve<MyClass>();
myInstanace.UseTheSessionForSomething();
}
Is the exception says; the answer is to add a transaction around the method with the line of code: var session = sessionFactory();
If you are getting the exception and HAVE added [Transaction] on that method, then you are not resolving the service LocalDbTest properly.
Have a look at my quick-start for a quick-start: https://github.com/haf/Castle.Facilities.NHibernate/wiki/NHibernate-Facility---Quick-Start
I'm reading your code now ;), the reason that it was missing was because of this line:
https://github.com/haf/Castle.Transactions/blob/master/src/Castle.Facilities.AutoTx/AutoTxFacility.cs#L86
It's a known 'TODO' that you have to register your facility before your components, and I think this is stated in the quickstart as well.
Cheers
精彩评论