Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WCF中处理端点异常_C#_Silverlight_Wcf_Exception_Endpoint - Fatal编程技术网

C# 在WCF中处理端点异常

C# 在WCF中处理端点异常,c#,silverlight,wcf,exception,endpoint,C#,Silverlight,Wcf,Exception,Endpoint,在我的应用程序(Windows Phone)中,我通过代码隐藏动态生成到WCF web服务的连接。 用户必须指定Web服务的url,然后我创建我的EndPointAddress和绑定,它工作得很好。 但如果用户输入了无效的url,则会引发异常: System.ServiceModel.EndpointNotFoundException->在[service address of the service]没有侦听可以接受消息的终结点。这通常是由于地址或SOAP操作不正确造成的。有关详细信息,请参阅

在我的应用程序(Windows Phone)中,我通过代码隐藏动态生成到WCF web服务的连接。 用户必须指定Web服务的url,然后我创建我的EndPointAddress和绑定,它工作得很好。 但如果用户输入了无效的url,则会引发异常:

System.ServiceModel.EndpointNotFoundException->在[service address of the service]没有侦听可以接受消息的终结点。这通常是由于地址或SOAP操作不正确造成的。有关详细信息,请参阅InnerException(如果存在)

innerexception非常经典:“远程服务器返回了一个错误:NotFoundatInnerException。”

问题是:我无法处理此异常。我尝试了很多在这里找到的东西,但都没有成功

有什么解决办法吗

我唯一能找到的就是在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();
        }
MyServiceClient client=newmyserviceclient(myBinding,myEndpointAddress);
尝试
{
client.RegisterUserCompleted+=新事件处理程序(client\u RegisterUserCompleted);
client.RegisterUserAsync();
}
捕获(TimeoutException例外)
{
MessageBox.Show(例如Message);
client.Abort();
}
捕获(FaultException-ex)
{
MessageBox.Show(例如Message);
client.Abort();
}
捕获(通信例外)
{
MessageBox.Show(例如Message);
client.Abort();
}
抓住
{
MessageBox.Show(“错误”);
client.Abort();
}
最后
{
client.CloseAsync();
}
您需要将try-catch移动到client\u 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不会返回完整信息以减慢恶意用户的速度。

问题已解决。 解决方案是:在创建客户端之前必须测试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);
                 });
    }
        }
public void ValidateUri(字符串uri)
{
if(!Uri.IsWellFormedUriString(Uri,UriKind.RelativeOrAbsolute))返回;
request=WebRequest.Create(uri);
BeginGetResponse(新的AsyncCallback(ValidateUriCallback),null);
}
私人网络请求;
私有void ValidateUriCallback(IAsyncResult结果)
{
尝试
{
WebResponse httpResponse=(WebResponse)request.EndGetResponse(result);
//在这里创建客户端
ServiceClient client=newmyserviceclient(myBinding,myEndpointAddress);
client.RegisterUserCompleted+=新事件处理程序(client\u RegisterUserCompleted);
client.RegisterUserAsync();
}
捕获(WebException ex)
{
var response=例如,响应为System.Net.HttpWebResponse;
if(响应!=null
&&response.StatusCode==HttpStatusCode.NotFound)
{
Dispatcher.BeginInvoke(委托()
{
MessageBox.Show(“web服务地址无效”,“对不起”,MessageBoxButton.OK);
});
}
}

谢谢您的建议,但我已经尝试过了。问题是该方法从未被调用(因为webservice地址无效),因此从未到达RegisterUserCompleted。您到底想如何“处理”这个异常?还有,到底是在什么时候出现的异常?几年后,我在一个Xamarin应用程序上遇到了这个问题。我在app.cs上创建了ValidateUri作为一个函数,并将回调传递给它-这样我就可以在我的应用程序中的任何地方使用相同的函数,并根据我需要的服务以不同的方式处理错误:
public static void TestWebAccess(字符串uri、ref WebRequest请求、操作回调)