【问题标题】:Combine WCF, NHibernate and Ninject结合 WCF、NHibernate 和 Ninject
【发布时间】:2012-06-08 13:15:21
【问题描述】:

我正在编写 WCF 服务(托管在 IIS 中)。我从 Nuget 下载了 Ninject(3.0.0.0,带有 WCF 扩展)和 NHibernate。 我已经在 MVC 上下文中一起使用了这些,但还没有在 WCF 中使用。 我希望每次调用该服务都有一个新会话。 但是我真的找不到关于如何完成它的好教程。

现在,我只是将它放在“NinjectWebCommon.cs”文件中(就像我在 MVC 项目中所做的那样)

private static void RegisterServices(IKernel kernel)
{
    var helper = new NHibernateHelper(WebConfigurationManager.ConnectionStrings["xx"].ConnectionString, Assembly.GetAssembly(typeof(Template)));
    kernel.Bind<ISessionFactory>().ToConstant(helper.SessionFactory).InSingletonScope();
    kernel.Bind<IDbSessionFactory>().To<DbSessionFactory>().InSingletonScope();
    kernel.Bind<IDbSession>().To<DbSession>().InRequestScope();

    //Repository
    //Bind the repository stuff here
}      

但这并没有像我预期的那样真正起作用。有人可以告诉我我在这里做错了什么吗?

编辑

更多细节。我可以看到,当我的服务启动时,它会启动一个 NHibernate 会话对象。但它实际上在处置上失败了。 当它试图关闭我的会话时,我得到一个 NullReferenceException。 我正在使用 Nuget 的所有最新版本。

【问题讨论】:

  • 没有人对这个话题有答案吗?还是一个很好的例子?由于我的应用程序的其余部分即将完成,因此变得非常紧迫。

标签: c# .net wcf nhibernate ninject


【解决方案1】:

您必须从NinjectWcfApplication 继承Global 应用程序类。

public class Global : NinjectWcfApplication
{
    #region Overrides of NinjectWcfApplication

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    protected override IKernel CreateKernel()
    {
        IKernel kernel = new StandardKernel( new ServiceModule() );
        return kernel;
    }

    #endregion
}

https://github.com/ninject/ninject.extensions.wcf/tree/Maintenance_2.2/src/Examples

【讨论】:

  • 感谢您的回复。但我正在使用更新版本的 WCF 扩展,所以这不再有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
  • 1970-01-01
  • 2012-02-21
相关资源
最近更新 更多