我遇到了 WCF 的一个非常奇怪的问题。如果接收到无法访问的端点 IP 地址或服务无法绑定,我需要为 WCF 服务实施一些恢复行为。如果应用程序在创建服务时出现异常失败,则流程很简单,它会终止它并向用户请求另一个 IP 地址并再次尝试创建服务。(下面的代码片段)。如果地址无效,我会收到“在侦听 IP Endpoint=.121.10.11.11 时发生 TCP 错误(10049:请求的地址在其上下文中无效)”异常,但是如果我尝试第二次尝试有效地址 我也有同样的异常,上次尝试的 IP 地址错误。这是一个代码:
ServiceHost service = null;
try
{
Uri[] uris = { new Uri(Constants.PROTOCOL + "://" + address + ":" + port) };
service = new ServiceHost(typeof(IRemoteService), uris);
NetTcpBinding tcpBinding =
WcfTcpRemoteServicesManager.LessLimitedNewNetTcpBinding(int.MaxValue,
int.MaxValue, int.MaxValue);
ServiceEndpoint ep = service.AddServiceEndpoint(implementedContract.FullName,
tcpBinding, serviceName);
var throttle =
service.Description.Behaviors.Find<ServiceThrottlingBehavior>();
if (throttle == null)
{
throttle = new ServiceThrottlingBehavior
{
MaxConcurrentCalls = Constants.MAX_CONCURRENT_CALLS,
MaxConcurrentSessions = Constants.MAX_CONCURRENT_SESSIONS,
MaxConcurrentInstances = Constants.MAX_CONCURRENT_INSTANCES
};
service.Description.Behaviors.Add(throttle);
}
service.Open();
}
catch (Exception e)
{
_debugLog.WriteLineMessage(
"Failed to open or create service exception. Exception message:" +
e.Message);
if (service!=null)
{
try
{
service.Close();
}
catch (Exception)
{
service.Abort();
service.Close();
throw e;
}
}
}
谢谢