即使服务端点地址有效,WCF 也会发送相同的异常
我在使用 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;
}
}
}
谢谢
I'm running into a really strange problem with WCF. I need to implement some recovery behavior for WCF service if not reachable endpoint IP address received or service can not bind.
The flow is simple if the application fail on exception on service creation it terminate it and request from user another IP address and perform another attempt to create the service. (The code snippet below). If the address is not valid I get "A TCP error (10049: The requested address is not valid in its context) occurred while listening on IP Endpoint=.121.10.11.11" exception, but for any reason if I try the second attempt with valid address I've got the same exception with wrong IP address from previous attempt. Here is a code:
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;
}
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您捕获异常 e,然后捕获另一个异常,但抛出原始异常 e。
不确定这是否是您的问题,但这可能会导致您收到令人困惑的异常。
编辑
现在我已经更仔细地阅读了它。
问题是上下文和地址之间不匹配。我看到代码使用了netTcpBinding:
You catch exception e, then later you catch a different exception, but you throw the original exception e.
Not sure if this is your problem, but it could result in you getting a confusing exception back.
EDIT
Now I have read it a bit more carefully.
The problem is a mismatch between the context and the address. I see that the code uses netTcpBinding: