gpt4 book ai didi

c# - 结合 WCF、NHibernate 和 Ninject

转载 作者:太空宇宙 更新时间:2023-11-03 14:00:02 25 4
gpt4 key购买 nike

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

现在,我只是把它放在“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 session 对象。但它实际上在处置上失败了。当它试图关闭我的 session 时,我得到一个 NullReferenceException。我使用的是 Nuget 的所有最新版本。

最佳答案

您必须从 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

关于c# - 结合 WCF、NHibernate 和 Ninject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10949596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com