ICallContextInitializer with a generic contract
I must begin with the fact that I am totally in love with the extensible model of WCF.
I played around a bit with the 'ICallContextInitializer' interface - very cool.
It allows you to do whatever you desire before and after invocations of service operations.
It would be a good practice to set operation's thread specific settings and releasing it afterwards. (such as CultureInfo and so on)
Unfortunately, I found a scenario where using it would blow everything up.
The ICallContextInitializer won't function If I implement and host a generic contract, such as:
[ServiceContract]
public interface IRequestReplyService
{
[OperationContract(Action = "*", ReplyAction = "*")]
Message ProcessMessage(Message message);
}
If I use an endpoint behavior to add my ICallContextInitializer (as most examples illustrate)
public
void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
foreach (DispatchOperation operation in endpointDispatcher.DispatchRuntime.Operations)
{
operation.CallContextInitializers.Add(new ContextInitializer());
}
}
This code will not work as expected, it never enters inside the enumeration.
In this case, when using a generic contract, there are no dispatch operations defined.
I tried to tweak it, I implemented an operation behavior and placed it on the actual 'ProcessMessage' operation.
In this case, it did add the context initializer, looks alright? well, no.
When the client called the service the whole thing just blew up, no good.
I suspect ICallContextInitializer is unusable when working with generic contracts - shame, no doubt.
For the time being, I applied the implementation as a message inspector which works just fine though the natural place for my code would actually be in an 'ICallContextInitializer'.