我在这个问题上遇到了障碍。我有一个WCF库与相应的WCF Web服务。沙箱Web应用程序尝试从此服务触发一个方法。
但是,服务会在发出请求时对其进行记录。这里的持久层是使用FluentNHibernate构建的。 Web应用程序中的会话管理过去一直很简单(即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”页面,没有问题。
当引用它的Web应用程序尝试调用服务方法时,
NHibernateContext.Current().Session
无法找到任何实时会话。在单步执行应用程序之后,global.asax中的app start方法被触发,但在任何需要它的操作之前似乎正在死亡(这是猜测)。
我意识到IglooCommons可能有点具体,因为NHibernateContext是一个专有库,但是,带有持久层的WCF并不罕见。我非常感谢您对我错过的内容或其他方向的任何想法。
附加说明:
对于使用此服务的Web应用程序,我创建了一个基于wsdl的“interop”库,使用svcutil自动生成。 Web应用程序和Web服务之间没有Web引用,只有使用auto-gen类的Web应用程序。
答案 0 :(得分:1)
请参阅此文http://realfiction.net/?q=node/167
执行它所说的内容,您不需要启用aspnetcompatibility。
答案 1 :(得分:0)
您是否为WCF服务启用了ASP.NET服务托管环境?这需要你:
您在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
答案 2 :(得分:0)
这个决议特别是对IglooCommons lib的误解。示例中的“NHibernateContext”属性用于装饰界面,实际上它需要修饰实现。通过单独移动,Igloo站点上的样本能够正常运行。
作为旁注,这里提供的其他解决方案非常适合扩展我对WCF内部工作的简单知识及其与NHibernate的交互。
答案 3 :(得分:0)
您的WCF服务svc.cs文件应如下所示:
[NHibernateContext(Rollback.Automatically)]
public class YourWcfService : IYourWcfService