How do I catch an exception from a remote WCF service?

After adding the link from the Visual Studio interface, I initialized the client:

private WcfRequestProcessorClient _client;

var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

string serviceFullUrl = "http://myserviceurl.com/svcapi";
var address = new EndpointAddress(serviceFullUrl);

_client = new WcfRequestProcessorClient(binding, address);
_client.ClientCredentials.UserName.UserName = "MyUserName";
_client.ClientCredentials.UserName.Password = "MyPassword";

      

And later I call the related method with the request as a parameter:

try
{
    var itemsReq = LoadItemsRequest();
    _client.ProcessRequestsAsync(itemsReq);
}
catch(Exception)
{
     // source code for show an error in a popup
}

      

Now that the URL, Username and Password are correct , everything works fine . But , if the url, username, or password are wrong , the app crashes and Visual Studio shows me this window when debugging the catch :

enter image description here

What is the way to handle these exceptions?

+3


source to share


2 answers


The service must be configured to propagate these exceptions.

It somehow asserts that an exception can be thrown even if you don't know the actual cause, as you stated. 'The exception that was thrown

The reason why no exception was caught is because the call is asynchronous.

i.e. the exception is thrown onto another stack and then the one you called it on.



if it returns an assignment you will need to wait.

if it's APM-style async, you would have to provide a callback and try to catch it there.

Alternatively, you can register with AppDomain.UnhandledException to catch this exception eventually.

0


source


catch(Exception e)
    {
     MessageBox.Show(e.Message.ToString());
    }

      



Hope this helps. For Advance metadata, the exception refers to a FaultException in WCF. It is specifically designed for service oriented architecture.

0


source







All Articles