WCF 4.0 Rest Service returns content type text / html

I have a WCF 4.0 REST service. If I include automaticFormatSelectionEnabled in the web.config , then the service will correctly choose between serialization as XML or JSON based on the HTTP Accept header.

However, when I issue the GET command in the browser, the response body is returned in XML format, but the HTTP content header is "text / html". This causes the browser to not understand the XML response and try to display it as html (which, of course, does not work well). This makes it difficult to test GET methods in the browser.

If I turn off automaticFormatSelectionEnabled then everything will work as expected (the response body contains XML, and the HTTP content type is "application / xml"), however I would like to be able to automatically switch to JSON if necessary.

Is there a way to return the type of content when requested through a browser?

+4
source share
1 answer

I ran into the same problem, here is the job. Essentially, you need to create a behavior that changes the type of content when sending a response.

Your web.config file should say 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 finally, a class that implements IDispatchMessageInspector and IServiceBehavior, which does the hard work of changing the type of content:

 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 } 

Now work as expected!

+3
source

Source: https://habr.com/ru/post/1381434/


All Articles