处理WCF中的端点异常
在我的应用程序 (Windows Phone) 中,我通过代码隐藏动态生成与 WCF Web 服务的连接。 用户必须指定 Web 服务的 url,然后我创建 EndPointAddress 和 Binding,效果很好。 但是,如果用户输入无效的 url,则会引发异常:
“System.ServiceModel.EndpointNotFoundException -> 没有端点正在侦听 [服务地址] 可以接受消息。这通常是由于地址不正确或有关更多详细信息,请参阅 InnerException(如果存在)。
内部异常非常经典:“远程服务器返回错误:NotFound at InnerException。”
问题:我无法处理这个异常。我尝试了这里找到的很多东西但没有成功。
有什么解决办法吗?
我唯一能找到的是Reference.cs中的一些方法,这绝对不是一个好主意......!
预先感谢您,并对我的英语不好表示歉意:)!
我的实际代码如下所示,但它没有捕获异常=>
MyServiceClient client = new MyServiceClient(myBinding, myEndpointAddress);
try
{
client.RegisterUserCompleted += new EventHandler<RegisterUserCompletedEventArgs>(client_RegisterUserCompleted);
client.RegisterUserAsync();
}
catch (TimeoutException ex)
{
MessageBox.Show(ex.Message);
client.Abort();
}
catch (FaultException ex)
{
MessageBox.Show(ex.Message);
client.Abort();
}
catch (CommunicationException ex)
{
MessageBox.Show(ex.Message);
client.Abort();
}
catch
{
MessageBox.Show("Error");
client.Abort();
}
finally
{
client.CloseAsync();
}
in my application (Windows Phone), I generate a connection to a WCF web service dynamically via code behind.
The user must specify the url of the Web Service then I create my EndPointAddress and Binding, and it works well.
But if the user enter a invalid url, an exception is thrown :
"System.ServiceModel.EndpointNotFoundException -> There Was no endpoint listening at [address of the service] That Could accept the message. This Is Often Caused By An incorrect address or SOAP action. See InnerException, if present, for more details."
The innerexception is quite classic : "The remote server Returned an error: NotFound at InnerException."
The problem: I can not handle this exception. I tried many things found here without success.
Is there any solution?
The only thing I could find is surronding some methods in the Reference.cs, which is definitly not a good idea ...!
Thank you in advance and sorry for my bad english :) !
My actual code looks like this, but it doesn't catch the exception =>
MyServiceClient client = new MyServiceClient(myBinding, myEndpointAddress);
try
{
client.RegisterUserCompleted += new EventHandler<RegisterUserCompletedEventArgs>(client_RegisterUserCompleted);
client.RegisterUserAsync();
}
catch (TimeoutException ex)
{
MessageBox.Show(ex.Message);
client.Abort();
}
catch (FaultException ex)
{
MessageBox.Show(ex.Message);
client.Abort();
}
catch (CommunicationException ex)
{
MessageBox.Show(ex.Message);
client.Abort();
}
catch
{
MessageBox.Show("Error");
client.Abort();
}
finally
{
client.CloseAsync();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题解决了。
解决方案是:在创建客户端之前必须测试 uri。
为此,我发出一个 WebRequest 并捕获 WebResponse 中的异常:
the problem is solved.
The solution is : the uri have to be test before creating the client.
To do it, I make a WebRequest and catch the exception in the WebResponse :
您需要将 try catch 移至 client_RegisterUserCompleted 方法并环绕var x = e.Result;
编辑:因为我的第一种方法是错误的......
首先,您应该首先检查您的 URL(代码中的 myEndpointAddress)。您可以在浏览器中浏览到它吗(您可以在该行上设置断点,然后复制该值并将其粘贴到浏览器中)。
如果该方法有效,或者浏览到该应用程序时显示错误,请将其添加到托管 WCF 服务的 Web 应用程序的 web.config 中,
然后运行您的应用程序或浏览到它。它将在与您的 Web 应用程序相同的目录中创建一个 Trace.svclog 文件。打开该文件并查找以红色突出显示的条目。这将为您提供有关异常的更多信息。默认情况下,WCF 不会返回完整信息,以减慢恶意用户的速度。
You need move your try catches to the client_RegisterUserCompleted method and wrap around thevar x = e.Result;
EDIT: Since my first approach was wrong...
First you should start by inspecting your URL (myEndpointAddress in your code). Can you browse to it in a browser (you can set a breakpoint on that line, then copy the value and paste it into your browser).
If that works or if there is an error shown when you browse to it, then add this to your web.config of the web app hosting the WCF service inside of the
Then run your application or browse to it. It will create a Trace.svclog file in the same directory as your web application. Open up that file and look for the highlighted in red entry. That will give you more information on the exception. WCF doesn't return the full information by default to slow down malicious users.