0
votes

I'm using Ninject 3.01.1.10 with WCF extenstion and Web extention (all from nuget)

I am using NInject in a WCF Service (that has several client desktop apps) using Entity Framework and Repository pattern.

Since I am using EF, I wanted to use the InRequestScope so that each repository is created every time there is a service request (as the EF good practices dictate).

I installed everything through nuget and have NinjectWebCommon bootstrapper class registering the services in RegisterServices as the Ninject WCF sample shows. I also have the WCF Service in InstanceContextMode.PerCall as I read somewhere online.

My question is: does all the chain of injected objects needs to be in InRequestScope so the scope works?

My WCF service is being injected with a ServiceHandlerDispatcher in the constructor:

public VanillaService(IServiceHandlerDispatcher serviceHandlerDispatcher) { ... }

This ServiceHandlerDispatcher has a dictionary of objects ServiceHandler that, according to the client's handler request, call a Controller object. This controller object has methods that use the repositories.

public ServiceHandlerDispatcher(IMessageHandler[] messageHandlers) { ... }

public SaveSubmissionHandler(SubmissionController submissionController) { ... }

public SubmissionController(IRepository<Submission> submissionRepository) { ... }

So the chain is something like this: Service -> ServiceHandlerDispatcher -> ServiceHandler -> controller -> repository -> DB

For performance and memory purposes, I wanted the ServiceHandlerDispatcher and all ServiceHandler in singleton scope but the repository objects in InRequestScope (so that in each call the EF's DbContext is disposed), since I can have dozens of ServiceHandler objects.

Again, Service -> ServiceHandlerDispatcher (SingletonScope) -> ServiceHandler (SingletonScope) -> controller (none) -> repository (InRequestScope) -> DB

Is this possible or do I have to have the kernel creating each ServiceHandler in each call? If it's needed that every object in the chain needs to be InRequestScope, how to implement the ServiceHandler dictionary? With the Factory extension?

1

1 Answers

1
votes

So, this is not possible. If I have the first item in the chain with a greater scope than the others (in this case SingletonScope) the other items in the chain will have that scope too.