Click here to Skip to main content
15,886,063 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello,

I sometimes get an exception from Connecting to a webservice. I could catch the exception like this:
catch (System.ServiceModel.Security.MessageSecurityException ex)
if (ex.ToString().ToLower().Contains("security timestamp"))

but this is not working on foreign language versions of windows I guess.

So I need to catch the exception using the type I guess? I am having problems finding the matching exception type.

Something like this I guess:
if (ex is ExpiredSecurityTokenException)

Could someone please give me a hint what is the matching Exception type?

Thanks a lot
Eric
Posted
Updated 21-Jun-11 0:34am
v4
Comments
Slacker007 21-Jun-11 6:35am    
Edited: added code blocks and cleaned up a little.

I guess you want to use exception's message because there are numerous possible exceptions of exactly the same class but with other messages, and you want to set specific procession for only this one, thrown by particular method from the inner depths of .NET assemblies? Well, seems like a tough task. I can only think of ugly non-reliable solutions like:
1. Use exception properties, such as StackTrace and TargetSite, to find out the source of error and compare it to the one you expect.
2. Do compare exception's message string to the one existing in current culture - it may be like that

Type pType = typeof(System.ServiceModel.MessageSecurityOverHttp);
Assembly pAssembly = pType.Assembly; // one, and not the best, of numerous ways to get "System.ServiceModel" assembly 
System.Resources.ResourceManager pManager = new System.Resources.ResourceManager("System.ServiceModel", pAssembly); // getting resource manager for "System.ServiceModel" assembly
string sMessage = pManager.GetString("ExpiredTokenInChannelParameters",  CultureInfo.CurrentUICulture); // getting message text for current UI culture; you can now compare obtained string to your exception's message


Instead of "ExpiredTokenInChannelParameters" you should provide the name of the specific resource - ie your exception's message string. This can be obtained by IL disassemblers - like dotPeek, for example.

As I mentioned before, this approach, though possibly working, is definitely not recommended - it is an ugly hack I would only use in case of absolute necessity! :)
 
Share this answer
 
Log the exception when it occurs. The standard ToString of an exception includes the exception type.
 
Share this answer
 
Comments
StM0n 21-Jun-11 9:17am    
That's what I also got in my mind... seems like the expection is not available on the client...
What are you trying to accomplish with if (ex.ToString().ToLower().Contains("security timestamp"))?
 
Share this answer
 
Thanks a lot for your suggestions. Problem solved. :-)
 
Share this answer
 
Like in the above post, catching ExpiredSecurityTokenException and TimeoutException.

Also added a System.ServiceModel.EndpointNotFoundException for another case.

And trying to log the exception if it occurs again to see if there are other types also.

Thanks for the suggestions and ideas :-)
 
Share this answer
 
Comments
Timberbird 21-Jun-11 10:21am    
Not sure why you accepted my answer... looks like eventually your problem is not as I described :). As far as I understood you just need to catch exceptions of specific classes, which is common practice and doesn't require any hacks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900