Marconline
Marconline

Reputation: 1451

Windows service with WCF programmatically created: how to expose it with Per-Call InstanceContextMode?

my application is composed by a lot of different windows service. Each of them creates programmatically a WCF service. I tried to configure my service with a "per-call" instancecontextmode but it seems to process one request at time.

What should I do? The class implementing the service interface is decorated with this attribute:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

While the service is instantiated like this:

ServiceHost _host = new ServiceHost(typeof(IMultiMarketBatchNotification));
_host.AddServiceEndpoint(typeof(IMultiMarketBatchNotification), binding, myAddress);

where:

Isn't that enough?

Thanks, Marco

Upvotes: 0

Views: 1253

Answers (1)

marc_s
marc_s

Reputation: 755381

You need to add those lines of code after instating your ServiceHost, but before opening it:

// look for the "ServiceBehavior" 
ServiceBehaviorAttribute srvBehavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();

if (srvBehavior == null)
{
   // if we didn't find the service behavior - create one and add it to the list of behaviors
   srvBehavior = new ServiceBehaviorAttribute();
   srvBehavior.InstanceContextMode == InstanceContextMode.PerCall;
   host.Description.Behaviors.Add(srvBehavior);
}
else
{
   // if we found it - make sure the InstanceContextMode is set up properly
   srvBehavior.InstanceContextMode == InstanceContextMode.PerCall;
}

That should do it.

Upvotes: 4

Related Questions