0

我在 IIS 上运行我的应用程序以测试我的服务是否按预期工作。此外,我对其他内部类的操作进行单元测试。

以下是我的会话工厂配置:

Fluently.Configure()
                .Database(MySQLConfiguration.Standard
                              .ConnectionString(myconnectionString)
                              .ShowSql()
                )
                .CurrentSessionContext<WcfOperationSessionContext>()
                //.CurrentSessionContext("call")
                .Mappings(m =>
                          m.FluentMappings
                              .AddFromAssemblyOf<DtoDifficulty>())
                .BuildSessionFactory();

您可以注意到注释行,带有//.CurrentSessionContext("call")。当我在 IIS 上运行我的服务时,我必须使用它上面的行.CurrentSessionContext< WcfOperationSessionContext >(),当我运行单元测试时,.CurrentSessionContext("call")

有没有办法知道哪个案例正在运行并自动设置其中一个选项?

4

1 回答 1

0

我找到了一种选择正确上下文的方法。如果我运行单元测试,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();
于 2014-12-21T03:03:18.103 回答