【问题标题】:Silverlight + Google Chrome + custom request headers => content type errorSilverlight + Google Chrome + 自定义请求标头 => 内容类型错误
【发布时间】:2014-01-29 19:53:37
【问题描述】:

我想为从我的 Silverlight 应用程序向 RIA DomainService 发出的每个请求添加自定义标头。

我通过在域客户端端点的行为集合中添加自定义行为来做到这一点。

然后我的行为添加了一个自定义 MessageInspector,它将我的自定义标头设置为请求。

这一切在 IE 中都可以正常工作,但在 Google Chrome 中,我收到一个异常消息:“...内容类型 text/plain; charset=x-user-defined of the response message does not match the content type of the绑定 (application/msbin1)..."。

有没有人成功地将自定义标头添加到 RIA 服务请求并使其在 Google Chrome 中运行?有人可以帮我解决这个问题吗?

这是我的自定义行为的代码:

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();
}

这是我的自定义消息检查器的代码:

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

最后,这是我为 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();

提前致谢!

【问题讨论】:

    标签: silverlight ria


    【解决方案1】:

    将 [Query(HasSideEffects=true)] 属性添加到您的 WCF RIA IQueryable 方法,并将 [Invoke(HasSideEffects=true)] 属性添加到您的 WCF RIA Invoke 方法,一切顺利。

    【讨论】:

    • 谢谢!!!它确实解决了这个问题。但问题是,将“HasSideEffects”影响为真时会发生什么变化?我知道它会对网络缓存产生影响,但还有什么其他的吗?
    • http 方法将是 POST。您可能需要考虑 GET 和 POST http 方法之间的区别。一是您没有将参数作为 URL 的一部分传递。
    • 谢谢,我会调查的。
    【解决方案2】:

    在扩展 RIA 上下文以包含多个自定义标头值后,我们遇到了同样的问题。我尝试将 HasSideEffects=true 解决方法应用于我们的域服务方法,但它没有解决问题。解决一个完全不相关的问题实际上为我解决了这个问题。

    我们的 Silverlight 应用程序基于一个相当旧的 Visual Studio 项目模板,因此 HTML DOCTYPE 设置为 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>
    

    我开始看到与 Visual Studio 的浏览器链接功能相关的 Javascript 错误。研究导致我看到另一个关于 Visual Studio 2013 不能很好地与过渡 DOCTYPE 配合使用的 SO 帖子。根据那篇文章,我切换到托管 ASPX 页面以使用 HTML5 DOCTYPE,并从页面上的 HTML 元素中删除了 XML 命名空间以修复浏览器链接问题。

    新的页面标记如下所示:

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

    在测试其他回归时,我注意到此更改还解决了 Chrome 中的内容类型错误。

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 2018-05-07
      • 2017-06-18
      • 1970-01-01
      • 2017-03-27
      相关资源
      最近更新 更多