Nhibernate WCF和单元测试的不同SessionContexts
我在IIS上运行应用程序以测试我的服务是否按预期工作。此外,我还对其他内部类的操作运行单元测试 以下是我的会话工厂配置:Nhibernate WCF和单元测试的不同SessionContexts,nhibernate,fluent-nhibernate,sessioncontext,Nhibernate,Fluent Nhibernate,Sessioncontext,我在IIS上运行应用程序以测试我的服务是否按预期工作。此外,我还对其他内部类的操作运行单元测试 以下是我的会话工厂配置: Fluently.Configure() .Database(MySQLConfiguration.Standard .ConnectionString(myconnectionString) .ShowSql()
Fluently.Configure()
.Database(MySQLConfiguration.Standard
.ConnectionString(myconnectionString)
.ShowSql()
)
.CurrentSessionContext<WcfOperationSessionContext>()
//.CurrentSessionContext("call")
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<DtoDifficulty>())
.BuildSessionFactory();
流利。配置()
.Database(MySQLConfiguration.Standard
.ConnectionString(myconnectionString)
.ShowSql()
)
.CurrentSessionContext()
//.CurrentSessionContext(“调用”)
.Mappings(m=>
m、 FluentMappings
.AddFromAssemblyOf())
.BuildSessionFactory();
您可以注意到注释行,带有/.CurrentSessionContext(“call”)。当我在IIS上运行服务时,我必须使用其上方的行.CurrentSessionContext有没有办法知道哪个案例正在运行并自动设置其中一个选项 我找到了选择正确上下文的方法。如果我运行单元测试,则HttpContext.Current返回null。当我运行服务时,它返回一个对象的实例 代码如下:
var fluentConfiguration = Fluently.Configure().Database(MySQLConfiguration.Standard
.ConnectionString(myConnectionString)
.ShowSql()
);
var hasHttpContext = HttpContext.Current != null;
if (hasHttpContext)
fluentConfiguration.CurrentSessionContext<WcfOperationSessionContext>();
else
fluentConfiguration.CurrentSessionContext("call");
_sessionFactory = fluentConfiguration
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<DtoDifficulty>())
.BuildSessionFactory();
var fluentConfiguration=Fluently.Configure().Database(MySQLConfiguration.Standard
.ConnectionString(myConnectionString)
.ShowSql()
);
var hasHttpContext=HttpContext.Current!=无效的
if(hasHttpContext)
fluentConfiguration.CurrentSessionContext();
其他的
CurrentSessionContext(“调用”);
_sessionFactory=fluentConfiguration
.Mappings(m=>
m、 FluentMappings
.AddFromAssemblyOf())
.BuildSessionFactory();