检测或避免客户端CommunicationException当服务器关闭保持活动http连接
问题描述:
我正在开发一个VS2010 Dotnet 4.0客户端到一个运行嵌入式Linux的设备(Pelco品牌的视频圆顶,因为它发生)的SOAP服务。我无法控制设备中的SOAP服务器。供应商提供了正确加载为服务引用的WSDL文件。检测或避免客户端CommunicationException当服务器关闭保持活动http连接
我正在使用命令行桌面客户端来解决这个问题(在将代码迁移到服务之前)。
简化了一下,在各种服务的SOAP调用采取的形式是这样的:
service.SetPosition(pan,tilt,zoom)
var pos = service.GetPosition();
等等。他们基本上工作和做预期的事情。
问题是这样的:曾经在一段时间,间歇性,带有图案我还没有(还)想通了,这些服务的一个随机调用将引发的CommunicationException其消息是
The underlying connection was closed: A connection that was
expected to be kept alive was closed by the server.
这里的我如何构造我的服务对象:
var binding = new System.ServiceModel.BasicHttpBinding();
uri = new Uri(/*the correct uri for the service*/);
address = new System.ServiceModel.EndpointAddress(u);
service = new PositioningControlPortTypeClient(binding, address);
在这种情况下,PositioningControlPortTypeClient
被通过WSDL定义,当我加载它作为一个服务引用。
有没有办法强制dotnet 4.0/wcf/soap使用HTTP/1.0?
有没有办法检测到我的服务客户端在抛出异常之前会抛出异常?
我应该只使用每个服务对象一次,然后处理它吗?
还有其他的智慧吗?
答
你可以尝试禁用保活在你的绑定:
var binding = new System.ServiceModel.BasicHttpBinding();
uri = new Uri(/*the correct uri for the service*/);
address = new System.ServiceModel.EndpointAddress(u);
CustomBinding cbinding = new CustomBinding(binding);
foreach (BindingElement be in cbinding.Elements)
{
if (be is HttpTransportBindingElement) ((HttpTransportBindingElement)be).KeepAliveEnabled = false;
}
service = new PositioningControlPortTypeClient(cbinding, address);
完美,谢谢。 –