0

I am trying to create client proxies using the excellent wcf facility in Castle Windsor. However, I need to access the OperationContextScope when utilizing the facility to add a custom property. My approach fails at runtime with the following error: Invalid IContextChannel passed to OperationContext. Must be either a server dispatching channel or a client proxy channel. This happens when the code enters the using block as shown below. Any advice on how to make this work is greatly appreciated.

Container setup:

public class WcfClientInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<WcfFacility>();
        container.Register(Component.For<IMyInterface>().AsWcfClient(new DefaultClientModel
        {
            Endpoint = WcfEndpoint.FromConfiguration("MyEndpoint"),
        }).LifestyleTransient());
    }
}

Invoking the proxy:

[Test]
public void Test()
{
    var container = new WindsorContainer();
    container.Install(new WcfClientInstaller());
    var proxy = container.Resolve<IMyInterface>();
    // Crashes here
    using (new OperationContextScope((IContextChannel)proxy))
    {
        var bmp = new BrokeredMessageProperty { CorrelationId = "someNonStaticId" };
        OperationContext.Current.OutgoingMessageProperties.Add(BrokeredMessageProperty.Name, bmp);                
    }
}
andersr
  • 98
  • 6
  • Try doing this in an IWcfPolicy implementation as suggested here: http://stackoverflow.com/a/16436951/246811 – Phil Degenhardt Mar 31 '15 at 20:50
  • Hi Phil. Thanks for your feedback! In the example you provided they are adding a static value using a wcfpolicy. However, I only know this value when I receive a message. Is it possible to pass this value to the policy somehow? – andersr Apr 01 '15 at 09:01
  • "receive a message"? So is this some kind of router? – Phil Degenhardt Apr 01 '15 at 09:18
  • Not really, it is a WCF service which receives a message from azure service bus, does some work with the data and then publishes the result to a topic using WCF. – andersr Apr 01 '15 at 15:42
  • I'm not familiar with service bus and whether there is some ambient context from which you can extract "someNonStaticId" (like OperationContext for WCF). However, it would be relatively straight forward to make your own thread based context in which to store a value that your IWcfPolicy could then extract. – Phil Degenhardt Apr 01 '15 at 21:52
  • Can you try to replace using block like using (new OperationContextScope(WcfContextChannel.For(proxy))) – Risky Pathak Nov 19 '17 at 10:09

0 Answers0