【发布时间】: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