[英]Set KeepAlive in WCF Data Services
我正在使用微軟稱為“WCF數據服務”的“abandonware”產品。 我希望我能在這里得到一些幫助。
我正在嘗試設置我的服務以在負載均衡器(F5)中工作。 我遇到的問題我也使用了正常的WCF服務。 基本上,F5將連接視為“持久”連接。
要使用我的WCF服務修復此問題,我可以將“KeepAliveEnabled”標志設置為false,如下所示:
var endpointAddress = new EndpointAddress(new Uri("http://someAdrs/MyWcfService.svc"));
MyWcfClient client = new MyWcfClient (new BasicHttpBinding(), endpointAddress);
var customBinding = new CustomBinding(client.Endpoint.Binding);
var transportElement = customBinding.Elements.Find<HttpTransportBindingElement>();
transportElement.KeepAliveEnabled = false;
client.Endpoint.Binding = customBinding;
正如我所說,這對我的WCF服務很有用。 但我似乎無法找到一種方法來設置WCF數據服務客戶端。 (客戶端沒有“端點”變量。)
任何人都知道如何為Wcf數據服務客戶端設置KeepAlive為false?
更新:
我試過這個:
entities.SendingRequest2 += EntitiesOnSendingRequest2;
private static void EntitiesOnSendingRequest2(object sender,
SendingRequest2EventArgs sendingRequest2EventArgs)
{
sendingRequest2EventArgs.RequestMessage.SetHeader("Keep-Alive", "false");
}
但它似乎沒有幫助。
更新II:
我在“EntitiesOnSendingRequest2中嘗試了這個:
sendingRequest2EventArgs.RequestMessage.SetHeader("Connection", "close");
但我得到一個錯誤,因為Connection標頭是受限制的。
想通了。
有一個名為“SendingRequest”的事件可以訂閱。 (它已被棄用,但這並不重要,因為Wcf數據服務對微軟來說已經死了。由於微軟目前的短暫關注,它永遠不會被刪除。)
該事件將允許您訪問WebRequest,可以將其轉換為HttpWebRequest。
然后設置KeepAlive = false就很簡單了;
entities.SendingRequest += EntitiesOnSendingRequest;
private static void EntitiesOnSendingRequest(object sender,
SendingRequestEventArgs sendingRequestEventArgs)
{
var webRequest = ((HttpWebRequest) sendingRequestEventArgs.Request);
webRequest.KeepAlive = false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.