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 7512053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:44:47+00:00 2026-05-29T23:44:47+00:00

I have a WCF 4.0 REST service. If I enable automaticFormatSelectionEnabled in the web.config

  • 0

I have a WCF 4.0 REST service. If I enable automaticFormatSelectionEnabled in the web.config file then the service will correctly choose between serializing as XML or JSON based on the HTTP “Accept” header.

However, when I issue the GET command with a browser, the response body comes back formatted as XML, but the HTTP content-type header is “text/html”. This causes the browser to not realize the response is XML and try to render it as html (which of course doesn’t work well). This makes testing my GET methods in a browser more difficult.

If I disable automaticFormatSelectionEnabled then everything works as expected (response body contains XML and the HTTP content-type is “application/xml”), however, I’d like to be able to automatically switch to JSON if requested.

Is there some way to make the content-type come back correctly when requesting through the browser?

  • 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-29T23:44:48+00:00Added an answer on May 29, 2026 at 11:44 pm

    I ran into the same problem, here is a work around. Essentially what you need to do is create a behaviourextension that will change the content type when sending the response.

    Your web.config would need to include something like:

    <behaviors>
      <serviceBehaviors>
        <behavior name="WebServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <MessageInspector/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add name="MessageInspector"
              type="Namespace.ServiceContentTypeBehaviorExtensionElement, assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>
    

    Then you will need to create a class that inherits from BehaviorExtensionElement

    public class ServiceContentTypeBehaviorExtensionElement : BehaviorExtensionElement
    {
        protected override object CreateBehavior()
        {
            return new ServiceContentTypeMessageInspector();
        }
    
        public override Type BehaviorType
        {
            get
            {
                return typeof(ServiceContentTypeMessageInspector);
            }
        }
    }
    

    And then finally a class that implements IDispatchMessageInspector and IServiceBehavior which does the hard work of changing the content type:

    public class ServiceContentTypeMessageInspector : IDispatchMessageInspector, IServiceBehavior
    {
    
        #region IDispatchMessageInspector
    
        public object AfterReceiveRequest(ref Message request, IClientChannel channel,
            InstanceContext instanceContext)
        {
            return null;
        }
    
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            // inspect and/or modify the reply
            if (WebOperationContext.Current.OutgoingResponse.Headers[HttpResponseHeader.ContentType] == "text/html; charset=utf-8" &&
                WebOperationContext.Current.OutgoingResponse.Format == WebMessageFormat.Xml)
            {
                WebOperationContext.Current.OutgoingResponse.Headers[HttpResponseHeader.ContentType] = "application/xml; charset=utf-8";
            }
        }
    
        #endregion
    
        #region IServiceBehavior
    
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
            ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
            {
                foreach (var endpoint in dispatcher.Endpoints)
                {
                    endpoint.DispatchRuntime.MessageInspectors.Add(new ServiceContentTypeMessageInspector());
                }
            }
        }
    
        public void AddBindingParameters(ServiceDescription serviceDescription,
            ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints,
            BindingParameterCollection bindingParameters)
        {
        }
    
        public void Validate(ServiceDescription serviceDescription,
            ServiceHostBase serviceHostBase)
        {
        }
    
        #endregion
    }
    

    Should work as expected now!

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

Sidebar

Related Questions

I have a WCF REST web service that is hosted via a service route
I have a WCF 4 REST service which does some processing and then returns
I have created a simple Northwind's Product REST Web Service in WCF at /Northwind/Product
I have a WCF REST Web Service which returns POCO entities generated by Entity
I have a WCF/REST web service that I'm considering using Linq to SQL to
I have developed an rest enable wcf service by using the following link http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/simplerestservice11172009221218PM/simplerestservice.aspx
I have a C# WCF REST Service which allows the addition of a bookmark,
I have a REST service consumed by a .Net WCF client. When an error
I have some REST web services implemented in WCF. I wish to make these
I have a WCF Web Service which is referenced from a class library. After

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.