Question

I want to add custom headers to each requests made from my Silverlight application to a RIA DomainService.

I'm doing it by adding a custom behavior in the behaviors collection of the domain client's endpoint.

My behavior then add a custom MessageInspector which set my custom headers to the request.

This all works fine in IE but in Google Chrome, I get an exception saying: "...The content type text/plain; charset=x-user-defined of the response message does not match the content type of the binding (application/msbin1)...".

Has anyone ever succeeded to add custom headers to a RIA Services request and make it work in Google Chrome? Can someone help me out with this one?

Here's the code of my custom behavior:

public class AppendExtraHeadersHttpBehavior : WebHttpBehavior
{
  public AppendExtraHeadersHttpBehavior()
  {
  }

  public override void ApplyClientBehavior( ServiceEndpoint endpoint, ClientRuntime clientRuntime )
  {
    clientRuntime.MessageInspectors.Add( m_inspector );
  }

  private readonly AppendExtraHeadersMessageInspector m_inspector = new AppendExtraHeadersMessageInspector();
}

Here's the code of my custom message inspector:

public class AppendExtraHeadersMessageInspector : IClientMessageInspector
{
  public AppendExtraHeadersMessageInspector()
  {
  }

  public void AfterReceiveReply( ref Message reply, object correlationState )
  {
    // Nothing to do here.
  }

  public object BeforeSendRequest( ref Message request, IClientChannel channel )
  {
    var property = request.Properties[ HttpRequestMessageProperty.Name ] as HttpRequestMessageProperty;
    if( property != null )
    {
      property.Headers[ "CultureName" ] = Thread.CurrentThread.CurrentCulture.Name;
    }

    return null;
  }
}

And finally, here's the code I added in a partial for my DomainContext.

partial void OnCreated()
{
  var domainClient = this.DomainClient as WebDomainClient<IMyServiceContract>;
  if( domainClient != null )
  {
    domainClient.ChannelFactory.Endpoint.Behaviors.Add( AppendExtraHeadersHttpBehavior );
  }
}

private static readonly AppendExtraHeadersHttpBehavior AppendExtraHeadersHttpBehavior = new AppendExtraHeadersHttpBehavior();

Thanks in advance!

Was it helpful?

Solution

Add [Query(HasSideEffects=true)] attribute to your WCF RIA IQueryable methods, and [Invoke(HasSideEffects=true)] to you WCF RIA Invoke methods and your good to go.

OTHER TIPS

We were experiencing the same problem after extending our RIA context to include several custom header values. I tried apply the HasSideEffects=true workaround to our domain service methods, but it did not resolve the problem. Addressing a completely unrelated problem actually solved this issue for me.

Our Silverlight application is based on a rather old Visual Studio project template, so the HTML DOCTYPE was set to XHTML 1.1:

<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml2-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
...
</html>

I started seeing Javascript errors related to Visual Studio's Browser Link feature. Researching that led me to another SO post about Visual Studio 2013 not playing nicely with the transitional DOCTYPE. Per that post, I switched to hosting ASPX page to use the HTML5 DOCTYPE and removed the XML namespace from the HTML element on the page to fix the Browser Link issue.

The new page markup looks like this:

<!DOCTYPE html>
<html>
...
</html>

While testing for other regressions, I noticed that this change also resolved the content type errors in Chrome.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top