Finding a Client Channel
Where can I get the IContextChannel that OperationContextScope requires?
OperationContextScope allows you to create a temporary scope in which context for a service operation can build up before and after the operation is actually called. The constructor for OperationContextScope takes an instance of IContextChannel, which is a type that you've probably never seen before. Why are you expected to have this unknown type? It's because you have instances of it floating around all the time even though there's no particular hint of this.
There are different ways to get an IContextChannel depending on whether you're using a proxy generated by svcutil or a proxy generated at runtime. In either case, pretend that I've got service contract called IService with a single method called Foo.
When I generate a service client using svcutil, the client object has a member called InnerChannel that works as an IContextChannel.
ServiceClient client = new ServiceClient(binding, new EndpointAddress(address));
using (new OperationContextScope(client.InnerChannel))
{
WebOperationContext.Current.OutgoingRequest.Headers["X"] = "from compiled proxy";
client.Foo();
}
client.Close();
Otherwise, if I'm using a ChannelFactory to create the proxy at runtime, the channel that I get back happens to be an IContextChannel as well.
ChannelFactory<IService> factory = new ChannelFactory<IService>(binding);
factory.Open();
IService proxy = factory.CreateChannel(new EndpointAddress(address));
using (new OperationContextScope((IContextChannel)proxy))
{
WebOperationContext.Current.OutgoingRequest.Headers["X"] = "from runtime proxy";
proxy.Foo();
}
factory.Close();
Next time: Standards Guide
Comments
- Anonymous
July 18, 2008
Some tips for building support for versioning into the naming of data contracts. First, the primary route