WCF和单元测试的不同SessionContexts

WCF和单元测试的不同SessionContexts

问题描述:

我在IIS上运行我的应用程序以测试我的服务是否按预期工作。另外,我运行了其他内部班级操作的单元测试。WCF和单元测试的不同SessionContexts

下面是我的会话出厂配置:

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”)

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

我找到了一种方法来选择正确的上下文。如果我运行我的单元测试,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();