Pregunta

In my Managed Application, I currently have my WCF services running as:

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

Whats the catch ? SomeService defined with:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single

This is not good anymore, I need to make it InstanceContextMode.PerCall.

When trying to .Open() If 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 the solution to my problem ? How do I pass values to the constructor on my wcf service?

My Main concern:
I run different types of services inside this managed application, It seems that this solution is good only if i run one type of service.

¿Fue útil?

Solución

When more than one service instance will be needed (PerCall or PerSession) then passing a single service instance into the ServiceHost isn’t enough... which is the exception.

Controlling instance creation is managed by the IInstanceProvider.

Is this the solution to my problem ? How do I pass values to the constructor on my wcf service?

This only answers half your question. You are using Unity. The management of creating the container needs to be part of the implementation. The most common solution is to use Unity.WCF which is also available as a NuGet package.

Note that Unity.WCF doesn’t support object lifetimes based WCF OperationContexts. There are multiple (more complicated) implementations like this that do.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top