Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use OperationContextScope inside of a WCF service?

Tags:

c#

wcf

service

I'm currently working on a WCF service that reaches out to another service to submit information in a few of its operations. The proxy for the second service is generated through the strongly typed ProxyFactory<T> class. I haven't experienced any issues but have heard I should do something like the following when making the call:

using (new OperationContextScope((IContextChannel)_service))
     _service.Send(message);

So my question is: when is creating this new OperationContextScope appropriate, and why?

Thanks!

like image 295
Brandon Linton Avatar asked Mar 18 '10 14:03

Brandon Linton


1 Answers

If you are using callbacks or if you want to modify the message or headers then you need to use OperationContextScope. Your service might need to modify outgoing headers while calling that another service.

When you establish OperationContextScope then you can:

  1. Access and modify incoming and outgoing message headers and other properties.
  2. Access the runtime, including dispatchers, the host, channel, and extensions.
  3. Access other types of contexts, such as security, instance, and request contexts.
  4. Access the channel associated with the OperationContext object or (if the channel implements System.ServiceModel.Channels.ISession) the associated channel's session identifier.

The other service which you call, is it a session-based service? Probably you need to look at its sample client code or documentation if available.

like image 164
Ajay Kelkar Avatar answered Nov 12 '22 06:11

Ajay Kelkar