我想爲從我的Silverlight應用程序到RIA DomainService的每個請求添加自定義標頭。Silverlight + Google Chrome +自定義請求標頭=>內容類型錯誤
我這樣做是通過在域客戶端的端點的行爲集合中添加一個自定義行爲。
我的行爲然後添加一個自定義的MessageInspector,它將我的自定義標頭設置爲請求。
這一切工作正常在IE瀏覽器,但在谷歌瀏覽器,我收到一個異常說:「...內容類型text/plain; charset = x用戶定義的響應消息不匹配的內容類型綁定(應用程序/ msbin1)...「。
有沒有人成功地將自定義標題添加到RIA Services請求並使其在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();
在此先感謝!
謝謝!它確實解決了這個問題。但是,問題在影響「HasSideEffects」爲true時會發生什麼變化?我知道它會對網頁緩存產生影響,但還有其他什麼嗎? –
http方法將是POST。您可能想要考慮GET和POST http方法之間的差異。一個是你沒有傳遞你的參數作爲URL的一部分。 – marce
謝謝,我會研究一下。 –