I was trying to get instance of ISession from IHttpHandler but can't figure out how it can be done. I reviewed Cuyahoga IHttpHandlers and noticed the following line:
(CoreRepository)HttpContext.Current.Items["CoreRepository"];
At the same time CoreRepository is obsolete:
CoreRepository is replaced by several decoupled services.
Please help - what is the best (correct) way to retrieve ISession from IhttpHandler.
The recommended way is to leverage the Castle NHibernate SessionManager.
For example if you want to use it in a module, just add the ISessionManager via the constructor. It will be resolved automatically by the container:
public class MyModule
{
private ISessionManager _sessionManager;
public MyModule(ISessionManager sessionManager)
{
this._sessionManager = sessionManager;
}
public void DoSomethingWithSession()
{
ISession session = this._sessionManager.OpenSession();
// Do stuff
}
}
Thanks. I noticed it with standard modules. But I'm trying to create ASP IHttpModule where I need to use SessionManager:
namespace My.Modules.PhotoUpload
{
public class PhotoGenerator : IHttpHandler
{
// not sure that it will work here :(
public PhotoGenerator (ISessionManager sessionManager)
{
this._sessionManager = sessionManager;
}
#region IHttpHandler Members
public bool IsReusable
{
get { return false; }
}
...
}
}
martijnbThe recommended way is to leverage the Castle NHibernate SessionManager.
For example if you want to use it in a module, just add the ISessionManager via the constructor. It will be resolved automatically by the container:
Allright, you can get services via the container directly:
IWindsorContainer container = Cuyahoga.Web.Util.ContainerAccessorUtil.GetContainer();
ISessionManager sessionManager = container.Resolve(typeof(ISessionManager));
Thanks. I was trying this way too. The odd thing is that it always write:
"Unknown entity class: My.Modules.PhotoUpload.Domain.Photo"
But at the same time from PhotoUploadModule it works correctly. I think that I need somehow attach assembly to the session as it was done when loading PhotoUploadModule, am I right?
class PhotoUploadModule : ModuleBase, INHibernateModule
{
...
}
martijnbAllright, you can get services via the container directly:
IWindsorContainer container = Cuyahoga.Web.Util.ContainerAccessorUtil.GetContainer();
ISessionManager sessionManager = container.Resolve(typeof(ISessionManager));
desunitBut at the same time from PhotoUploadModule it works correctly. I think that I need somehow attach assembly to the session as it was done when loading PhotoUploadModule, am I right?
The moduleloader automagically registers module assemblies in the NHibernate configuration. When you need to do it manually, you can use Cuyahoga.Core.Service.SessionFactoryHelper:
SessionFactoryHelper sessionFactoryHelper = container.Resolve(typeof(SessionFactoryHelper);
sessionFactoryHelper.AddAssembly(myAssembly);
It adds the assembly to the NH configuration, and builds a new SessionFactory that is registered in the container afterwards. I don't know if AddAssembly() checks if the assembly was already added. Maybe you have to add some extra code for that.
Also, after rebuilding the sessionfactory, you need to redirect to the original page again because the Session of the original request doesn't know of the newly added class(ses) yet.
Since my handler will generate a picture I can't do redirects. Are any ways to statically add this assembly?
martijnbdesunitBut at the same time from PhotoUploadModule it works correctly. I think that I need somehow attach assembly to the session as it was done when loading PhotoUploadModule, am I right?
The moduleloader automagically registers module assemblies in the NHibernate configuration. When you need to do it manually, you can use Cuyahoga.Core.Service.SessionFactoryHelper:
SessionFactoryHelper sessionFactoryHelper = container.Resolve(typeof(SessionFactoryHelper);
sessionFactoryHelper.AddAssembly(myAssembly);
It adds the assembly to the NH configuration, and builds a new SessionFactory that is registered in the container afterwards. I don't know if AddAssembly() checks if the assembly was already added. Maybe you have to add some extra code for that.
Also, after rebuilding the sessionfactory, you need to redirect to the original page again because the Session of the original request doesn't know of the newly added class(ses) yet.
Anyone? Still stucked here - I can't get ISession with loaded Domains from my assembly. :(
Hi desunit,
since HttpHandlers are only classes as well, you should be able to register it as a component in the container.
I don't know if HttpHandlers need to have an empty constructor but you might still be able to use setter injection to get your ISessionManager.
What layers/services does your module provide?
What exactly is the job of the handler? Does it really need direct access to dao objects?
Is your code available somewhere?
Regards,
Max
Thanks for your response, mgaerber! I have registered my component in service.cfg:
<component
id="PhotoUploadModule"
type="My.PhotoUpload.PhotoUploadModule, MyPhotoUpload" />
IWindsorContainer container = Cuyahoga.Web.Util.ContainerAccessorUtil.GetContainer();
PhotoUploadModule t = container.Resolve<PhotoUploadModule>();
t.GetPhotoByID(4);
And got the exception:
Unknown entity class: My.PhotoUpload.Domain.Photo.
It seems that Nhibernate facility does not know anything about my assembly at this step. As martin said I need to add assembly and redirect a request. Maybe I have a solution without redirect? Statically set the assembly?
Thanks again.
mgaerberHi desunit,
since HttpHandlers are only classes as well, you should be able to register it as a component in the container.
I don't know if HttpHandlers need to have an empty constructor but you might still be able to use setter injection to get your ISessionManager.
What layers/services does your module provide?
What exactly is the job of the handler? Does it really need direct access to dao objects?
Is your code available somewhere?
Regards,
Max
I found it - I needed to add my assembly into assemblies tag in NHibernate facility.