I have an MVC3 and a WCF REST service that are both hosted in Azure and are using SQL Azure with Entity Framework 4.1 as the ORM.

I want to introduce a sharding scheme to increase the scalability of this setup.

As Azure is a stateless hosting environment, in my system every single request is populated with a user id that I can use to generate my hash to create a connection string that points to the desired database (sharding by user).

Currently I am using Ninject with the WCF and MVC extensions to inject the EF DbContext into my repositories. The context lifecycle is in the request scope (My rest service uses aspnetcompatibility).

My question is: How can I intercept this injection and populate the DbContext with the correct connection string? Using a custom ServiceHostFactory in WCF for example?

I have had a look at the Ninject.Interception extension but I could not figure out how to set it up to intercept the HTTP request prior to activating my services and controllers.

This would allow me to use sharding without refactoring my existing code.

有帮助吗?

解决方案

I turns out I was massively overthinking the problem.

Bind<MyEntities>().ToMethod(ctx => GetShardedContext());

Where GetShardedContext() creates the instance with the appropriate constructor arguments from the HttpContext.Current or WebOperationContext.Current.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top