0

I run my application on IIS to test if my services are working as expected. Also, I run unit tests of my other internal classes' operations.

The following is my session factory configuration:

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

You can notice the commented line, with //.CurrentSessionContext("call"). When I run my service on IIS, I have to use the line above it .CurrentSessionContext< WcfOperationSessionContext >(), when I run unit tests, .CurrentSessionContext("call").

Is there a way to know which case is running and set one of those options automatically?

Th3B0Y
  • 884
  • 1
  • 12
  • 30

1 Answers1

0

I found out a way to choose the right context. HttpContext.Current returns null in case I run my unit tests. When I run my Services it returns the instance of an object.

Here is the code:

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();
Th3B0Y
  • 884
  • 1
  • 12
  • 30