6

I am using Unity.WCF to inject dependencies for WCF service. Problem occurs when I set my service to InstanceContextMode.Single.

I found on Google that when InstanceContextMode is set to Single, InstanceProvider is not called. There is also a workaround for this but I was wondering if there is some built-in support for this in Unity.WCF because apparently this is a well known problem.

I found the information here: Enabling InstanceProvider for singleton services.

1
  • Can you share you current code that is not working? It will enable community to faster help you. Commented Mar 16, 2013 at 18:56

1 Answer 1

7

I will cite Paul Hiles comment on the same question you asked:

Using InstanceContextMode.Single makes your service scale very badly so is best avoided in most cases, particularly if it is just being used to allow AppFabric auto-start. You can safely remove the ServiceBehavior attribute and do it another way.

With Unity.WCF, you can add your initialisation code to the ConfigureContainer method of the WcfServiceFactory class that is created when you add the Unity.WCF NuGet package. This will only be executed once for the lifetime of the service.

BTW, you should not be passing the Unity container into your service. Add any components that your service uses into the constructor (e.g. repositories, helpers etc.) and also register then with Unity using the ConfigureContainer method. When your service is instantiated, the dependencies will be injected automatically.

You may also find article from this MSDN series useful.

Sign up to request clarification or add additional context in comments.

3 Comments

So it looks like I was able to remove InstanceContextMode.Single and replace it with ContainerControlledLifetimeManager in my WcfServiceFactory. Reason I need singleton is that I have a long running operation in separate thread and I need to check its status occasionally.
And why shouldn't I pass unity container to class if I need to resolve multiple object in that class (that also have dependencies)?
Because you will put two different responsibilities in one class, which is considered to be bad design. One class should do it's thing, and another class should configure it. That will enable you to easier test it in future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.