【问题标题】:Set KeepAlive in WCF Data Services在 WCF 数据服务中设置 KeepAlive
【发布时间】:2015-06-23 13:28:13
【问题描述】:

我正在使用 Microsoft 的“废弃软件”产品,称为“WCF 数据服务”。我希望我能在这里得到一些帮助。

我正在尝试将我的服务设置为在负载平衡器(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");
}

但这似乎没有帮助。

更新二:

我也在“EntitiesOnSendingRequest2”中尝试过:

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

但我收到了一个错误,因为 Connection 标头受到限制。

【问题讨论】:

    标签: c# .net wcf-data-services keep-alive


    【解决方案1】:

    想通了。

    有一个可以订阅的名为“SendingRequest”的事件。 (它已被弃用,但这并不重要,因为 Wcf 数据服务对微软来说已经死了。由于微软最近关注的时间很短,它永远不会被删除。)

    该事件将允许您访问 WebRequest,该 WebRequest 可以转换为 HttpWebRequest。

    那么就简单的设置KeepAlive=false;

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

    【讨论】:

      猜你喜欢
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 2011-04-22
      • 2014-12-11
      • 1970-01-01
      相关资源
      最近更新 更多