简体   繁体   中英

Set KeepAlive in WCF Data Services

I am using the "abandonware" product from Microsoft called "WCF Data Services". I am hoping I can get some help here on it.

I am trying to setup my services to work in a load balancer (an F5). The problem I am having I also had with my normal WCF services. Basically, the F5 sees the connection as a 'persistent' connection.

To fix this with my WCF Services I could set the "KeepAliveEnabled" flag to false like this:

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;

As I said, this works great for my WCF Service. But I can't seem to find a way to set this with a WCF Data Services client. (The client does not have a "Endpoint" variable.)

Anyone have an idea on how to set KeepAlive to false for a Wcf Data Services client?

Update:

I tried this:

entities.SendingRequest2 += EntitiesOnSendingRequest2;

private static void EntitiesOnSendingRequest2(object sender, 
    SendingRequest2EventArgs sendingRequest2EventArgs)
{
    sendingRequest2EventArgs.RequestMessage.SetHeader("Keep-Alive", "false");
}

But it did not seem to help.

Update II:

I tried this in the "EntitiesOnSendingRequest2 as well:

sendingRequest2EventArgs.RequestMessage.SetHeader("Connection", "close");

But I got an error because the Connection header is restricted.

Figured it.

There is an event called "SendingRequest" that can be subscribed to. (It is deprecated, but that does not matter as Wcf Data Services is dead to Microsoft. It will never be removed due to Microsoft's short attention span these days.)

That event will allow you to get at the WebRequest, which can be cast to an HttpWebRequest.

Then it is simple as setting KeepAlive=false;

entities.SendingRequest += EntitiesOnSendingRequest;


private static void EntitiesOnSendingRequest(object sender, 
      SendingRequestEventArgs sendingRequestEventArgs)
{
    var webRequest = ((HttpWebRequest) sendingRequestEventArgs.Request);
    webRequest.KeepAlive = false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM