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 :
What is the way to handle these exceptions?
source to share
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.
source to share