【问题标题】:stop the execution in wcf request在 wcf 请求中停止执行
【发布时间】:2013-09-25 23:48:57
【问题描述】:

我正在尝试在消息检查器的 AfterReceiveRequest 方法中回复 WCF 请求(实现 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);

客户端通过这种方式立即得到响应。

剩下的任务是中断服务器上的执行并最终没有错误。

request.Close() 会中断执行,但如果您查看 'BeforeSendReply' 中的消息,则会产生错误。

它不适合我们,正在寻找一个方便的终止。 有什么办法吗??

【问题讨论】:

  • 您能否尝试更详细地解释您要完成的工作?为什么要停止执行?

标签: .net wcf


【解决方案1】:

这可能是预期的行为。我不认为你能解决这个问题。

在将消息传递给方法之前使用消息检查器进行除观察或调整之外的任何事情可能是个坏主意。看起来您正在尝试做的事情更适合您的操作实施。您应该看到消息检查器,以便在将消息传递给调度员之前查看消息或对其进行调整。

【讨论】:

  • 实际上是身份验证、授权和日志记录任务发生的地方,这些任务和实际操作执行之间的任何内容都应该放在检查器的“AfterRequestReceive”方法中。它的工作方式类似于单点输入实现。因此,如果您对所有这些任务应该在哪里完成有任何建议,我们将不胜感激。
【解决方案2】:

我知道这是一个老问题,但我遇到了同样的问题并最终找到了解决方案,我现在将与您分享。

您真正想要做的事情是根据您在AfterReceiveRequest 方法中发现的某些条件来阻止服务操作被调用。为此,您需要添加一个IOperationInvoker,它与您的消息IDispatchMessageInspector一起使用

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);
    }
}

然后,您可以使用常规方法(通过配置文件中指定的属性或端点行为)将该消息检查器和该调用程序与您的操作联系起来。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    相关资源
    最近更新 更多