I have a fully functional MVC4 application which utilizes WCF endpoints and Entity Framework 4. The problem now is that I need to make the system Multi-tenant with the following setup:-
- 1 MVC Application host
- 1 WCF Service Layer
- Separate Databases (1 database per tenant, all with the same schema)
I'm implementing a Generic Repository class in which I can use a property (connectionName) set on my Principal object which relates to the connection string name in the config. I can access this property using my UnityHelper class. This is done as follows:-
private Entities Context
{
get
{
string connectionString = _Config.GetString(Helpers.UnityHelper.ConnectionName);
var entityConn = new EntityConnection
{
ConnectionString = connectionString
};
_Context = new Entities(entityConn);
return _Context;
}
}
This appears to be working but falls over if I am logged in as two separate tenants at the same time (different Browsers) and refresh the same grid on both instances. I have updated my WCF service to have the following behaviors:-
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession)]
I found the above solution at the following website, which I thought would be exactly what I was looking for.
mode = Per Call and Concurrency = Single
However, it seems to return values into the grid for tenantA that I would expect to see in the grid for tenantB, and vice versa. This is not always the same result, sometimes it returns the same data set for both grids.
I have never implemented such a system so any assistance would be much appreciated at this time.