【问题标题】:Handling Endpoint exceptions in WCF在 WCF 中处理端点异常
【发布时间】:2011-12-12 13:36:55
【问题描述】:

在我的应用程序 (Windows Phone) 中,我通过后面的代码动态生成与 WCF Web 服务的连接。 用户必须指定 Web 服务的 url,然后我创建我的 EndPointAddress 和 Binding,它运行良好。 但是如果用户输入了一个无效的 url,就会抛出异常:

"System.ServiceModel.EndpointNotFoundException -> 在 [服务地址] 处没有侦听可以接受消息的端点。这通常是由不正确的地址或 SOAP 操作引起的。有关更多详细信息,请参阅 InnerException(如果存在) 。”

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();
        }

【问题讨论】:

  • 您究竟想如何“处理”这个异常?另外,究竟在什么时候引发了异常?

标签: c# silverlight wcf exception endpoint


【解决方案1】:

问题解决了。 解决方案是:在创建客户端之前必须测试 uri。 为此,我创建了一个 WebRequest 并在 WebResponse 中捕获了异常:

public void ValidateUri(String uri)
    {
        if (!Uri.IsWellFormedUriString(uri, UriKind.RelativeOrAbsolute)) return;
        request = WebRequest.Create(uri);
        request.BeginGetResponse(new AsyncCallback(ValidateUriCallback), null);
    }

    private WebRequest request;
    private void ValidateUriCallback(IAsyncResult result)
    {
        try
        {
            WebResponse httpResponse = (WebResponse)request.EndGetResponse(result);

            // Create the client here
            ServiceClient client = new MyServiceClient(myBinding, myEndpointAddress);
        client.RegisterUserCompleted += new EventHandler<RegisterUserCompletedEventArgs>(client_RegisterUserCompleted);
        client.RegisterUserAsync();

        }
        catch (WebException ex)
        {
            var response = ex.Response as System.Net.HttpWebResponse;
    if (response!=null 
        && response.StatusCode == HttpStatusCode.NotFound)
    {
        Dispatcher.BeginInvoke(delegate()
                {
                    MessageBox.Show("The web service address is not valid", "Sorry", MessageBoxButton.OK);
                 });
    }
        }

【讨论】:

  • 几年后我在 Xamarin 应用程序上遇到了这个问题。我在 App.cs 上创建了 ValidateUri 作为一个函数,并将回调传递给它——这样我可以在我的应用程序的任何地方使用相同的函数并以不同的方式处理错误情况,具体取决于我需要的服务:public static void TestWebAccess(string uri, ref WebRequest request, Action&lt;IAsyncResult&gt; callBack)跨度>
【解决方案2】:

您需要将您的尝试捕获移动到 client_RegisterUserCompleted 方法并环绕 var x = e.Result;

编辑:因为我的第一种方法是错误的...... 首先,您应该首先检查您的 URL(代码中的 myEndpointAddress)。您可以在浏览器中浏览到它吗(您可以在该行设置断点,然后复制该值并将其粘贴到浏览器中)。

如果可行,或者当您浏览到它时显示错误,则将其添加到托管 WCF 服务的 Web 应用程序的 web.config 中

<system.diagnostics> <sources> <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true"> <listeners> <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData= "Trace.svclog" /> </listeners> </source> </sources> </system.diagnostics>

然后运行您的应用程序或浏览到它。它将在与您的 Web 应用程序相同的目录中创建一个 Trace.svclog 文件。打开该文件并查找以红色突出显示的条目。这将为您提供有关异常的更多信息。默认情况下,WCF 不会返回完整信息以减慢恶意用户的速度。

【讨论】:

  • 感谢您的建议,但我已经尝试过了。问题是该方法永远不会被调用(因为 web 服务地址无效),因此永远不会到达 RegisterUserCompleted。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多