Wcf operation invoker, log inputs parameter inside InvokeEnd

182 views Asked by At

I'm stuck with an issue with WCF project. I'm tring to extend wcf behavior implementing a custom operation invoker to log all the exceptions, saving the operation name and operation inputs. No problem with syncronous function, but with the asynchronous one I don't konow how to get the input parameters inside InvokeEnd, this is the code I've wrote:

public class OperationInvokerWithLogger : IOperationInvoker
{
    

    private IOperationInvoker _baseInvoker;
    private DispatchOperation _operation;    
    private OperationDescription _operationDescription;

    public OperationInvokerWithLogger(
        OperationDescription operationDescription,
        DispatchOperation operation
        )
    {
        _baseInvoker = operation.Invoker;
        _operationDescription = operationDescription;
        _operation = operation;
        
    }
    public object Invoke(object instance, object[] inputs, out object[] outputs)
    {
        try
        {
            return _baseInvoker.Invoke(instance, inputs, out outputs);
        }
        catch (Exception ex)
        {            
            LogOperationException(_operationDescription.Name, inputs, ex);
            throw;
        }
    }
    
    public bool IsSynchronous => _baseInvoker.IsSynchronous;
    public object[] AllocateInputs() => _baseInvoker.AllocateInputs();

    public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
    {
        try
        {
            var asyncResult = _baseInvoker.InvokeBegin(instance, inputs, callback, state);                                    
            return asyncResult;
        }
        catch (Exception ex)
        {
            LogOperationException(_operationDescription.Name, inputs, ex);
            throw;
        }
    }
    public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
    {        
        try
        {
            var taskResult = _baseInvoker.InvokeEnd(instance, out outputs, result);
            return taskResult;
        }
        catch (Exception ex)
        {   
            LogOperationException(_operationDescription.Name, inputs, ex);             
            throw;
        }
    }
}

If _baseInvoker.InvokeBegin throw exceptions, I need to log the "inputs" parameter of the InvokeBegin function. I suppose I should work with "state" parameter of the _baseInvoker.InvokeBegin, but i don't now how, if pass my custom parameter instead of "state", the function never ends. Inspecting the code with debugger, "state" parameter seems to be an System.ServiceModel.Dispatcher.MessageRpc.Wrapper object.

Any suggestions? Thanks

0

There are 0 answers