WCF,NHibernate和Ninject的每次请求会话实现 [英] session-per-request implementation for WCF, NHibernate, and Ninject

查看:80
本文介绍了WCF,NHibernate和Ninject的每次请求会话实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WCF应用程序中实现每个请求会话模型,并且我已经阅读了无数有关此主题的文档,但是似乎没有完整的演示.实际上,我遇到了一些非常有用的文章,例如:

I am trying to implement a session-per-request model in my WCF application, and I have read countless documents on this topic, but looks like there is not a complete demonstration of this. I actually came across some very useful articles such as this one:

NHibernate的ISession,其作用域为单个WCF调用

但是这些都是从过去NHibernate和Ninject没有WCF特定实现的时代开始的,因此它们通过实现其自定义服务提供程序来实现我所需要的功能.通过使用它们的模块来保持事物的一致性,但是我在这里结束了...

but these are all from the old days when NHibernate and Ninject did not have WCF specific implementations, therefore they achieved what I need by implementing their custom service providers, etc. Since both Ninject and NHibernate have WCF support now, I want to keep things consistent by using their modules, but I ended up here...

基本设置和流程应如下所示:

The basic setup and flow should be something like this:

  1. 在休眠配置中将CurrentSessionContext设置为WcfOperationSessionContext
  2. 在服务启动,开始请求或初始化时间附近的任何时候,打开会话并将其绑定到当前上下文
  3. 存储库使用SessionFactory.GetCurrentSession()方法获取当前会话实例
  4. 在生命周期结束时取消绑定并关闭会话

最初的问题是我无法访问wcf生命周期来处理绑定.深入研究ninject代码之后,我设法将我的方法挂接到ServiceHost的Open/Closes事件,而没有做太多更改,但是后来我无法访问OperationContext,因为它是线程静态的.

My initial problem was that I wasn't able to access to the wcf lifecycle to handle my bindings. After digging into the ninject code a bit, I managed to hook my methods to ServiceHost's Opening / Closing events without changing much, but then I wasn't able to access to the OperationContext since it is thread-static.

后来我尝试启用asp.net兼容性并使用Application_BeginRequest和Application_EndRequest,它看起来非常有前途,但是我认为这不是最好的解决方案,因为我应该将内容绑定到服务实例,而不是http请求.

Later I tried enabling asp.net compatibility and using Application_BeginRequest and Application_EndRequest, and it looked very promising, but I don't think that's the best solution since I should be binding stuff to the service instance, rather than the http request.

有人使用ninject的内置wcf扩展库实现了这一目标吗?或对我可能做错的事情有什么想法?

Has anyone ever achieved this using ninject's built-in wcf extension libraries? Or any ideas on what I might be doing wrong?

推荐答案

我已在

I have implemented per request session lifetime with the help of IDispatchMessageInspector. Probably you could implement custom lifetime manager for Ninject to achieve per web request.

这篇关于WCF,NHibernate和Ninject的每次请求会话实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆