2012-06-08 54 views
2

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

现在,我只是把这个在“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的最新版本。

+0

难道没有人对这个题目的答案吗?或者一个很好的例子?由于我的应用程序的其余部分几乎已经完成,因此它变得非常紧迫。 –

回答

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

+0

感谢您的回复。但是我正在使用更新版本的WCF扩展,所以这不再有效。 –