why is my System.ServiceModel.OperationContext null from silverlight client and the SOAP Message Header - note to me
Monday, June 29th, 2009When you are starting out with Silverlight and WCF/SOAP to implement WS-Security using SOAP headers into request messages or for that matter if you are accessing SOAP services from silverlight, you may run into this silly issue that’s why I am making note of it here.
Silverlight documentation clearly says that
SOAP headers are supported via the OutgoingMessageHeaders and IncomingMessageHeaders properties inside an operation method call.
OutgoingMessageHeaders is the outgoing message headers for the active OperationContext.
Use the OperationContext from within a service operation to access the current operation execution environment.
The OperationContext has the following properties and methods.
- The Current property returns the OperationContext object that represents the current execution context.
Documentation is not very clear so this is when you may run into this issue because OperationContext is null if you directly access OperationContext.Current from silverlight client.
Let’s look at OperationContext .Current setter to find out who sets it, as shown below in reflector snapshot. Now you know OperationContextScope sets the Context so you have to first create OperationContextScope using your ContextChannel before accessing the current Operation Execution Environment.
using (OperationContextScope contextScope = new OperationContextScope(client.InnerChannel))
{
OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader
}