Custom ServiceHost with DI and InstanceContextMode.Percall

In my managed application, my WCF services currently work as:

SomeService service = new SomeService(container) //IUnityContainer ServiceHost serviceHost = new ServiceHost(service, serviceAddress); 

What is the catch? SomeService is defined using

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single 

This is no longer good, I need to make it InstanceContextMode.PerCall .

When trying .Open () When changing the InstanceContextMode to "PerCall" - it will throw:

 System.InvalidOperationException: In order to use one of the ServiceHost constructors that takes a service instance, the InstanceContextMode of the service must be set to InstanceContextMode.Single. This can be configured via the ServiceBehaviorAttribute. Otherwise, please consider using the ServiceHost constructors that take a Type argument 

Is this a solution to my problem? How to pass values ​​to a constructor in my wcf service?

My main problem:
I am running different types of services inside this managed application. This solution seems to be good only if I run one type of service.

+4
source share
1 answer

If you need multiple instances of the service (PerCall or PerSession), then transferring one instance of the service to ServiceHost is not enough ... which is an exception.

Instance creation management is controlled by IInstanceProvider .

Is this a solution to my problem? How to pass values ​​to a constructor in my wcf service?

This will only half answer your question. You are using Unity. Managing container creation should be part of the implementation. The most common solution is to use Unity.WCF , which is also available as NuGet .

Please note that Unity.WCF does not support operations based on WCF objects OperationContexts. There are several (more complex) implementations such as this .

+3
source

Source: https://habr.com/ru/post/1484421/


All Articles