简体   繁体   中英

Handling Endpoint exceptions in WCF

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

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 :

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

You need move your try catches to the client_RegisterUserCompleted method and wrap around the var 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

<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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM