Stop execution on wcf request

I am trying to respond to a WCF request in the AfterReceiveRequest

message inspector method (implements IDispatchMessageInspector

)

  XmlReader xmlReader = XmlReader.Create(
    new StringReader(string.Format(@"<{0}Response xmlns='{1}' />",methodName,namespace)));

  Message replyMsg = Message.CreateMessage(request.Version, request.Headers.Action, xmlReader);
  OperationContext.Current.RequestContext.Reply(replyMsg);

      

Client

gets a response immediately this way.

The rest of the challenge is to break the execution on the server and finish without crashing.

request.Close () aborts execution, but if you look at the message in "BeforeSendReply" it throws an error.

well its not suitable for us and looking for a comfortable finish. do you care to do it?

0


a source to share


2 answers


This is likely expected behavior. I don't think you can get around this.



Using the message inspector for anything other than observing or adapting before passing a message to a method is probably a bad idea. It seems that what you are trying to do is more appropriate for the implementation of your operation. You should see the message inspector as an opportunity to see the message or customize it before passing it to the dispatcher.

0


a source


I know this is an old question, but I had the same problem and ended up finding a solution, which I will now share with you.

What you actually want to do is prevent the service operation from starting based on some condition you find in the method AfterReceiveRequest

. To do this you need to add IOperationInvoker

which works alongside your postIDispatchMessageInspector



class CustomMessageInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        if (SuppressRequest(request, channel, instanceContext))
        {
            request.Properties.Add("SuppressMe", true);
        }
    }

    // Other code omitted
}

class CustomInvoker : IOperationInvoker
{
    public object Invoke(object instance, object[] inputs, out object[] outputs)
    {
        // If the message inspector added the "SuppressMe" property, then don't
        // invoke anything and create an empty output.
        if (OperationContext.Current.IncomingMessageProperties.ContainsKey("SuppressMe"))
        {
            bool suppress  = (bool)OperationContext.Current.IncomingMessageProperties["SuppressMe"];
            if (suppress)
            {
                outputs = null;
                return null;
            }
        }

        // Otherwise, go ahead and invoke the operation
        return invoker.Invoke(instance, inputs, out outputs);
    }
}

      

You can then connect this message inspector and this invoker to your activity using normal methods (via an attribute or endpoint behavior specified in the config file).

-1


a source







All Articles