Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity.wcf and InstanceContextMode.Single

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.

like image 593
mersadk Avatar asked Mar 16 '13 18:03

mersadk


1 Answers

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.

like image 79
vittore Avatar answered Nov 15 '22 08:11

vittore