c# - IDispatchMessageInspector : Improving BeforeSendReply functionality

标签 c# .net wcf wcf-extensions


在我的 WCF 项目中,我需要在响应中使用自定义 header ,因此我实现了 IDispatchMessageInspector。老实说,一切都很好,但我对一件小事感到不安。
问题是 BeforeSendReply 和 AfterReceiveRequest 都会触发,即使我只是打开我的 .svc 作为页面,或者将服务加载到 WCF 测试客户端。
那么,第一个问题:这种行为是否正常?有没有一些方法可以声明性地处理这个问题(也许是一些 web.config 技巧)?
目前我使用下一个代码:

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        if (reply.Properties.Any(x => x.Key == "httpResponse"))
            return;

        MessageHeader header = MessageHeader.CreateHeader("Success", "NS", !reply.IsFault);
        reply.Headers.Add(header);          
    }

所以现在我通过使用它来处理所有不是服务调用的调用:

if (reply.Properties.Any(x => x.Key == "httpResponse"))
    return;

但我很确定还有其他更好的方法可以解决这个问题。 所以我的主要问题:请建议我一个更好的方法来处理所描述的情况。
提前致谢!

更新 1
我的 system.serviceModel 部分

<system.serviceModel>       
    <services>          
        <service behaviorConfiguration="someBehavior" name="serviceName">
            <endpoint address="" binding="basicHttpBinding" contract="my contract" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
    </services>     
    <behaviors>
        <serviceBehaviors>              
            <behavior name="someBehavior">
                <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>                  
                <serviceDebug includeExceptionDetailInFaults="false"/>
                <exceptionInspector/>                   
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <extensions>
        <behaviorExtensions>
            <add name="exceptionInspector" type="class which implements BehaviorExtensionElement" />
        </behaviorExtensions>
    </extensions>

</system.serviceModel>

更新 2(接受的解决方案)
我花了一些时间调查问题的根源,最后我找到了我可以接受的解决方案。
那么我发现了什么:
首先,Message 是一个抽象类。所以BeforeSendReply每次都会收到不同类型的具体消息。
最常用的是:
1) System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.MetadataOnHelpPageMes​​sage - 表示客户端打开 svc 作为页面。结果 = 众所周知的 html 格式页面,其中包含有关 svc 服务的常见信息。对于这种类型 reply.Version.EnvelopeEnvelopeVersion.None
2) 获取元数据请求。这是一个有点棘手的部分,它取决于我们是否使用 MEX。 因此,如果我们使用 MEX,则请求执行到 .svc/mex 端点,其消息类型将为 System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessagereply.Version.Envelope 相等到 EnvelopeVersion.Soap12
如果我们不使用 MEX,那么客户端会执行很少的请求来获取 wsdl 数据。消息类型将为 XMLSchemaMessage
3) 执行web方法请求。这仅对我的请求类型有用。它是 System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage 并且 reply.Version.Envelope 等于 EnvelopeVersion.Soap11

我使用的是 basicHttpBinding,所以 SOAP 版本是 1.1。所以我的最终代码应该只检查回复是否有 SOAP 信封并检查它的版本。如果 envelope 存在并且版本为 1.1,那么我们可以非常确定我们有 web 方法调用并且可以添加自定义 header :

        public void BeforeSendReply(ref Message reply, object correlationState)
    {   
        if(reply.Version.Envelope == EnvelopeVersion.Soap11)
        {               
            MessageHeader header = MessageHeader.CreateHeader("Success", "NS", !reply.IsFault);
            reply.Headers.Add(header);          
        }
    }

最佳答案

我似乎记得 IDispatchMessageInspector 将针对所有消息运行,包括在同一端点公开的对元数据 (WSDL) 的 HTTP 请求。你没有提到你是如何注册你的检查员的,所以这也可能是相关的。

除了检查之外,您是否尝试过检查邮件包含的内容?例如,包含服务 HTML 页面的消息可能具有 MessageVersion == MessageVersion.None。同样,与消息关联的操作也可能有用。

关于c# - IDispatchMessageInspector : Improving BeforeSendReply functionality,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4749670/

相关文章:

wcf - 与其他大容量程序解决方案相比,WCF 的性能如何?

c# - WCF Web API 和 ASP.NET Web API 有什么区别

C# 打印 TableEntity 属性,但忽略具有 [IgnoreProperty] 属性的属性

c# - IsAssignableFrom 和 GetInterface 之间有什么区别?

c# - Webjob V3 未启动 : Method not found: 'Microsoft. WindowsAzure.Storage.Blob.CloudBlobContainer

c# - .NET WinForms 中 UI 元素的授权

c# - netpeertcpbinding 是否支持请求/回复

c# - 如何将 C# 与 Postgres 连接起来?

c# - 如何检测 Console.In (stdin) 是否被重定向?

c# - 如何在 C# 摘要中包含 html 标记,以便将其作为文本处理(而不是解析为 XML)?