[英]WCF and FluentNHibernate
我已經碰到了這堵牆。 我有一個帶有相應WCF Web服務的WCF庫。 沙箱Web應用程序嘗試從此服務中激發一種方法。
但是,該服務將記錄請求。 這里的持久層是用FluentNHibernate構建的。 過去,Web應用程序中的會話管理非常簡單(即HttpModules),但是在WCF中,這對我來說有點棘手。
我在IglooCommons庫中遵循了注釋和示例,並將以下行添加到了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中的應用程序啟動方法,但是在執行任何需要它的操作之前,它似乎已經死了(這是猜測)。
我意識到IglooCommons可能有點特定,因為NHibernateContext是專有庫,但是具有持久層的WCF並不少見。 非常感謝您對我錯過的任何想法或其他方向的想法。
附加說明:
對於使用此服務的Web應用程序,我創建了一個基於wsdl的“互操作”庫,該庫使用svcutil自動生成。 Web應用程序和Web服務之間沒有Web引用,僅是使用自動生成類的Web應用程序。
您的WCF服務svc.cs文件應如下所示:
[NHibernateContext(Rollback.Automatically)]
public class YourWcfService : IYourWcfService
您是否為WCF服務啟用了ASP.NET服務托管環境? 這要求您:
您可以在WCF web.config中配置托管環境,如下所示:
<system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <!-- rest of config here --> </system.serviceModel>
該決議尤其是對IglooCommons lib的誤解。 該示例中的“ NHibernateContext”屬性用於裝飾接口,而實際上需要裝飾實現。 通過單獨移動它,Igloo站點上的樣本能夠正常運行。
附帶說明一下,此處提供的其他解決方案在擴展我對WCF的內部工作及其與NHibernate交互的知識方面非常出色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.