c# - WCF 和 FluentNHibernate

标签 c# wcf fluent-nhibernate

我在这个问题上碰壁了。我有一个带有相应 WCF Web 服务的 WCF 库。沙盒 Web 应用程序尝试从该服务中触发一个方法。

然而,该服务将记录发出的请求。这里的持久层是用 FluentNHibernate 构建的。 Web 应用程序中的 session 管理在过去非常简单(即 HttpModules),但在 WCF 中它对我来说有点棘手。

我按照 IglooCommons library 的注释和示例进行操作并将以下行添加到我的 global.asax(驻留在 Web 服务目录中)。

    protected void Application_Start(object sender, EventArgs e)
    {
        NHibernateFactory.Initialize(new SessionFactory().BuildSessionFactory());
    }

BuildSessionFactory 返回以下内容:

public class SessionFactory
{
    public SessionFactory() { }

    public ISessionFactory BuildSessionFactory()
    {
        return Fluently.Configure()
            .Database(MsSqlConfiguration
                        .MsSql2005
                        .ConnectionString(x =>
                            x.FromConnectionStringWithKey("myConnection"))
                        .ShowSql()
                        .CurrentSessionContext<WebSessionContext>())
            .Mappings(m =>
                m.FluentMappings
                    .AddFromAssemblyOf<OneClass>()
                    .AddFromAssemblyOf<AnotherClass>()
                    .Conventions.Add(
                        PrimaryKey.Name.Is(x => x.EntityType.Name + "ID"),
                        ForeignKey.EndsWith("ID"),
                        Table.Is(x => Inflector.Net.Inflector.Pluralize(x.EntityType.Name))))
            .BuildSessionFactory();
    }
}

服务页面 (*.svc) 加载了令人舒缓的蓝色“使用此 wsdl 生成类”页面,没有任何问题。

当引用它的网络应用程序试图调用服务方法时,

NHibernateContext.Current().Session

无法找到任何实时 session 。单步执行应用程序后,global.asax 中的应用程序启动方法被触发,但似乎在任何需要它的操作之前就快要死了(这是一个猜测)。

我意识到 IglooCommons 可能有点特殊,因为 NHibernateContext 是专有库,但是,具有持久层的 WCF 并不少见。非常感谢任何关于我遗漏的想法或其他方向的想法。

补充说明:

对于使用此服务的 Web 应用程序,我创建了一个基于 wsdl 的“interop”库,使用 svcutil 自动生成。 Web 应用程序和 Web 服务之间没有 Web 引用,只有使用自动生成类的 Web 应用程序。

最佳答案

您是否为您的 WCF 服务启用了 ASP.NET 服务托管环境?这需要您:

  1. 申请AspNetCompatibilityRequirementsAttribute通过 AspNetCompatibilityRequirementsMode 为您服务需要的。
  2. 像这样在 WCF web.config 中配置托管环境:

    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
        <!-- rest of config here -->
    </system.serviceModel>
    

More information about WCF Service hosting with ASP.NET is available here in the MSDN documentation .

关于c# - WCF 和 FluentNHibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1664174/

相关文章:

fluent-nhibernate - 我可以制定包括父键名称的Fluent NHibernate外键约定吗?

c# - 如何在此 asp.NET web api 方法中检索 Ajax header ?

c# - 监视多个可变数量的进程的启动和退出

c# - 如何配置WCF向客户端推送实时数据?

sql-server - 流畅的 nhibernate SQL Server 2008 R2 Express 非常长的字符串保存问题

c# - NHibernate手动设置id自动递增

c# - 在ASP.NET MVC中发生NullReferenceException异常时在浏览器中创建错误消息

c# - AutoFixture AutoMoq 不为某些属性创建模拟

c# - 如何使用WCF的MessageContract返回值?

c# - C# 中的 LAN 进程间通信?