Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3792228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:35:25+00:00 2026-05-19T12:35:25+00:00

In my WCF project I need to use custom headers in responses so I

  • 0

In my WCF project I need to use custom headers in responses so I implemented IDispatchMessageInspector. Honestly all works pretty fine, but I’m disturbing about one little thing.
The thing is that both BeforeSendReply and AfterReceiveRequest fires even when I just open my .svc as page, or load service into WCF Test Client.
So, the first question: Is that behavior normal? Are there some ways to handle that declaratively (maybe some web.config trick)?
Currently I use next 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 which aren’t service calls by using that:

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

But I’m pretty sure there some other and better way to handle that problem.
So my main question: please suggest me a better way to handle described situation.
Thanks in advance!

UPDATE 1
My system.serviceModel section

<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>

UPDATE 2 (ACCEPTED SOLUTION)
I spent some time investigation source of problem and finally I’ve found acceptable for me solution.
So what have I found:
First of all Message is a abstract class. So BeforeSendReply each time receive defferent types of concrete messages.
Most used are:
1) System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.MetadataOnHelpPageMessage – means that client opens svc as page. Result = well known html formatted page with common information about svc service. For this type reply.Version.Envelope is EnvelopeVersion.None.
2) Get metadata request. This is slightly tricky part and it depends if we use MEX or not.
So if we use MEX then request performs to .svc/mex endpoint and its Message type will be System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage with reply.Version.Envelope equal to EnvelopeVersion.Soap12.
In case if we don’t use MEX then client performs few requests for obtaining wsdl data. Message type will be XMLSchemaMessage.
3) Execute web method request. This only useful for me type of requests. It is System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage and has reply.Version.Envelope equal to EnvelopeVersion.Soap11.

I’m using basicHttpBinding so SOAP version is 1.1. So my final code should just check is reply has SOAP envelope and check version of it. In case if envelope exists and has version 1.1 then we may be pretty sure that we have web method call and custom header might be added:

        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);          
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:35:25+00:00Added an answer on May 19, 2026 at 12:35 pm

    I seem to remember that an IDispatchMessageInspector will run for all messages, including HTTP requests to metadata (WSDL), exposed at the same endpoint. You don’t mention how you’re registering your inspector, so that might also be related.

    Besides checking that, have you tried checking out what the message contains? For example, a message containing the service HTML page would likely have MessageVersion == MessageVersion.None. Likewise, the action associated with the message might also be useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working in a WCF project and we need to reuse some legacy components
I'm working on a project where I need the following. WCF service on the
I just started to use WCF Rest project template to design a REST service,
I try to consume WCF service in Silverlight project but in ass Reference dialog
I have a WCF project in Visual Studio that I need to deploy to
I just start a project where I need to have a WCF services that
I need to transfer large Excel files over a WCF service. Our project requires
I need to create a project for multiple web services using WCF in c#.
I am trying to write my first WCF project. I have created a project
VS.net creates a template when you create a WCF project. It adds a class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.