C#:捕获特殊的异常“安全时间戳"; [英] C#: Catching a special Exception "security timestamp"

查看:132
本文介绍了C#:捕获特殊的异常“安全时间戳";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

有时,从连接到Web服务会出现异常.我可以这样捕获异常:

catch (System.ServiceModel.Security.MessageSecurityException ex)
if (ex.ToString().ToLower().Contains("security timestamp"))


但这不适用于Windows的外语版本.

所以我需要使用我猜的类型来捕获异常?我在查找匹配的异常类型时遇到问题.

我猜是这样的:

if (ex is ExpiredSecurityTokenException)


有人可以给我一个提示是什么匹配的异常类型吗?

非常感谢
我猜想要使用异常消息,因为存在许多可能完全相同的类,但还有其他消息的异常,并且您想设置从.NET程序集的内部深度通过特定方法抛出的仅此过程的特定过程?好吧,这似乎是一项艰巨的任务.我只能想到像这样的丑陋,不可靠的解决方案:
1.使用异常属性(例如StackTrace和TargetSite)找出错误源并将其与您期望的错误源进行比较.
2.将异常的消息字符串与当前区域性中存在的消息字符串进行比较-可能就像这样

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



而不是"ExpiredTokenInChannelParameters",您应该提供特定资源的名称-即您的异常的消息字符串.这可以通过IL反汇编程序获得,例如dotPeek.

正如我之前提到的,虽然不建议使用这种方法,但绝对不建议使用-这是一个丑陋的技巧,只有在绝对必要的情况下,我才会使用它! :)


记录该异常发生的时间.异常的标准ToString包含异常类型.


您要使用if (ex.ToString().ToLower().Contains("security timestamp"))完成什么?


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

解决方案

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! :)


Log the exception when it occurs. The standard ToString of an exception includes the exception type.


What are you trying to accomplish with if (ex.ToString().ToLower().Contains("security timestamp"))?


这篇关于C#:捕获特殊的异常“安全时间戳";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆