IDispatchMessageInspector: Improving BeforeSendReply Functionality


In my WCF project, I need to use custom headers in the answers, so I implemented IDispatchMessageInspector. Honestly, everything works very well, but I'm worried about one small thing.
The fact is that both the BeforeSendReply and AfterReceiveRequest methods work even when I simply open my .svc as a page or load the service into the WCF test client.
So the first question is : Is this behavior normal? Is there a way to handle this declaratively (maybe some kind of web.config trick)?
I am currently using the following code:

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

So now I handle all calls that are not service calls using this:

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.MetadataOnHelpPageMessage - , svc . = html- svc. reply.Version.Envelope EnvelopeVersion.None.
2) . , MEX . , MEX, .svc/mex, System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage reply.Version.Envelope EnvelopeVersion.Soap12.
, MEX, wsdl. XMLSchemaMessage.
3) -. . System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage reply.Version.Envelope, EnvelopeVersion.Soap11.

basicHttpBinding, SOAP - 1.1. , SOAP- . 1.1, , - :

        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);          
        }
    }
+5
1

, , IDispatchMessageInspector , HTTP- (WSDL), . , , .

, , , ? , , HTML-, , MessageVersion == MessageVersion.None. , , , .

+2

All Articles