我在这个问题上碰壁了。我有一个带有相应 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 服务托管环境?这需要您:
- 申请AspNetCompatibilityRequirementsAttribute通过 AspNetCompatibilityRequirementsMode 为您服务需要的。
像这样在 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/