我应该如何验证传递给我的 WCF 服务的参数?

问题描述:

通常,您使用 if-then-throw 模式或代码协定检查公共方法中的传入参数.
我的问题是,我应该如何验证在我的 WCF 服务中传递的参数?例如,我有以下合同:

Normally you check incoming parameters in your public methods using if-then-throw pattern or Code Contracts.
My question is, how should I validate parameters passed in my WCF service? For example, I've got the following contract:

[OperationContract]
Stock GetStock(string symbol);

我想确保 symbol 参数不是 null 或空字符串.我应该在服务端使用相同的 if-then-throw 模式还是代码契约前提条件?我是否应该向 GetStock 方法添加一个 FaultContract 属性并将错误返回给客户端?WCF 服务的最佳参数验证技术是什么?

I want to ensure that symbolparameter is not null or empty string. Should I use the same if-then-throw pattern or Code Contracts precondition on the service-side? Should I add a FaultContract attribute to the GetStock method and return a fault to the client? What's the best parameters validation technique for WCF service?

Enterprise Library 验证应用程序块有一个用于与 WCF 的集成正是为此而设计的.

Enterprise Library Validation Application Block has an adaptor for integration with WCF designed for exactly this.

这个CodeProject 介绍有点旧但提供了比 MSDN 链接更多的背景.

This CodeProject Introduction is a little old but gives a bit more background than the MSDN links.