Search code examples
silverlightria

Silverlight + Google Chrome + custom request headers => content type error


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!


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.