Unity.wcf and InstanceContextMode.Single

二次信任 提交于 2019-11-28 07:52:15

问题


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:


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.



来源:https://stackoverflow.com/questions/15453261/unity-wcf-and-instancecontextmode-single

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!