3

I want to add my own IOperationInvoker to a wcf client but can't get it to work. I have this

class ClientProgram
{
    static void Main()
    {
        CreateClient().SomeMethod();
    }

    private static MyServiceClient CreateClient()
    {
        var client = new MyServiceClient(new NetTcpBinding(), new EndpointAddress"net.tcp://localhost:12345/MyService"));
        // I guess this is where the magic should happen
        return client;
    }
}

public class MyOperationInvoker : IOperationInvoker
{
    private readonly IOperationInvoker _innerOperationInvoker;

    public MyOperationInvoker(IOperationInvoker innerOperationInvoker)
    {
        _innerOperationInvoker = innerOperationInvoker;
    }

    public object Invoke(object instance, object[] inputs, out object[] outputs)
    {
        Console.WriteLine("Intercepting...");
        return _innerOperationInvoker.Invoke(instance, inputs, out outputs);
    }

    // Other methods not important
}

3 Answers3

2

You must have something mixed up here.

The IOperationInvoker is a server-side only extension point. It allows you to inspect an incoming message and invoke a particular operation on the service based on that message (its content, headers - whatever).

On the client side, this doesn't make sense at all - there's no way a client can use a IOperationInvoker implementation.

If you want to know how to add an IOperationInvoker implementation on the server-side, check out this blog post for a complete run-down.

For an excellent general-purpose introduction to WCF extensibility, check out Aaron Skonnards MSDN article here.

Marc

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Ok, I'm trying to add exception handling to all methods in the service client, is there a way to do this? –  Aug 05 '09 at 11:41
  • 1
    I'm desperately trying to extend the generated `ClientBase` class so that it independently takes care off ALL its exceptions AND unexpected server-replies and routes them all up to a single place or event handler where it will be decided if they tunnel up or handled. See my [bounty-question](http://stackoverflow.com/questions/33734660/wcf-client-side-error-handling) for more. – Shimmy Weitzhandler Nov 21 '15 at 17:27
1

Sounds like you might be better served implementing an IClientMessageInspector extension instead.

tomasr
  • 13,683
  • 3
  • 38
  • 30
  • Note that `IClientMessageInspector.AfterReceiveReply()` doesn't get called when there isn't any message back from the server, in case of `EndpointNotFoundException` or `TimeoutException` for example. – vyrp Jun 30 '22 at 12:14
-3

You have missed IOperationBehavior

public class MyOperationBehavior : IOperationBehavior
{
    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        dispatchOperation.Invoker = new MyOperationInvoker();
    }
}
meetjaydeep
  • 1,752
  • 4
  • 25
  • 39