Th3B0Y
Th3B0Y

Reputation: 994

Different SessionContexts for WCF and unit tests

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?

Upvotes: 0

Views: 267

Answers (1)

Th3B0Y
Th3B0Y

Reputation: 994

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();

Upvotes: 0

Related Questions