WCF 验证错误
我正在使用 验证应用程序块 - Enterprise Library 来验证发送的参数到我的 WCF 服务操作。 例如,某个操作要求参数为 1 或 6,如下所示:
[OperationContract(Name="GetEmployeesByRegion")]
[FaultContract(typeof(ValidationFault))]
List<Employees> GetEmployeesByRegion([DomainValidator(1,6)]int regionId);
这工作得很好,即发生验证错误,但是,当客户端调用该服务时,会抛出通用 System.ServiceModel.FaultException。 消息指示:“此故障的创建者没有指定原因。”
现在,我可以在服务校准之前自己检查参数并抛出自定义故障,但这似乎违背了目的使用验证应用程序块对参数进行基于属性的验证。 有没有办法自定义验证错误返回的错误? 也有可能我正在做一些完全错误的事情。 我只是想让调用者知道他/她应该在异常消息中传递 1 或 6。 这可能吗?
I'm using Validation Application Block - Enterprise Library to validate parameters sent to my WCF Service operations. For instance, a certain operation requires the parameter to either be a 1 or 6, like so:
[OperationContract(Name="GetEmployeesByRegion")]
[FaultContract(typeof(ValidationFault))]
List<Employees> GetEmployeesByRegion([DomainValidator(1,6)]int regionId);
This works just fine i.e the validation fault occurs however, when the service is invoked by the client, a generic System.ServiceModel.FaultException is thrown. An the message indicates: "The creator of this fault did not specify a reason."
Now, I could check the parameters myself before the service cal and throw a custom fault but that seems to defeat the purpose of attribute based validation of parameters using the Validation Application Block. Is there anyway to customize the error returned by the validation Fault? It is also possible I'm doing something completely wrong. I just want the caller to know that he/she should have passed in a 1 or 6 in the exception message. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,故障中存在一个集合,其中包含您正在查找的所有消息:
There is actually a collection that is present within the fault that has all the messages you are looking for:
所以这是古老的,但我遇到了同样的问题并且确实找到了答案。
对我来说问题是我添加了 [ValidationBehavior] 和 [FaultContract<; ValidationFault>)] 属性在我在客户端中创建了对服务的引用。 为了让它工作,我必须刷新参考。
其他一些可能需要检查的事情:
1) 这是 Enterprise Library 6 中修复的一个错误。我不确定这一点,但发现了一些针对类似问题对 EntLib 进行代码修复的提及。 确保您使用的是最新版本的 EntLib。
2) 您使用 ErrorMessage 参数代替 MessageTemplate 作为验证错误消息。
So this is ancient, but I was having the same problem and did find an answer.
The problem for me was that I added the [ValidationBehavior] and [FaultContract< ValidationFault>)] attributes after I created the reference to the service in my client. In order to get it working, i had to refresh the reference.
Some other possible things to check:
1) This was a bug that was fixed in Enterprise Library 6. I'm not sure about that one but have found a few mentions of code fixes to EntLib for similar problems. Make sure you're using the latest version of EntLib.
2) You are using the ErrorMessage parameter for your validation error message instead of MessageTemplate.