共用方式為


Parameter Filter

Download sample

This sample demonstrates how to validate the parameters passed to a method before it is invoked. The ParameterFilterAttribute and ParameterFilterBehavior are defined such that the filter can be applied to methods for which constraints can be imposed on the parameters passed to the method.

NoteNote:

The setup procedure and build instructions for this sample are located at the end of this topic.

The service implements a contract that defines a request-reply communication pattern. The contract is defined by the ICalculator interface, which exposes math operations Add, Subtract, Multiply, and Divide.

 // Define a service contract.
 [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
 public interface ICalculator
 {
    [OperationContract]
    double Add(double n1, double n2);
    [OperationContract]
    double Subtract(double n1, double n2);
    [OperationContract]
    double Multiply(double n1, double n2);
    [OperationContract]
    double Divide(double n1, double n2);
 }

The client makes synchronous requests to a math operation and the service replies with the result.

In the service, the ParameterFilterBehavior class implements the IParameterInspector interface. The BeforeCall method of the interface is implemented such that the parameters are validated before the method is called.

internal class ParameterFilterBehavior : IParameterInspector
{
    double minValue;
    double maxValue;
    public ParameterFilterBehavior(double minValue, double maxValue)
    {
        this.minValue = minValue;
        this.maxValue = maxValue;
    }
    public void AfterCall(string operationName, object[] outputs,  
                          object returnValue, object correlationState)
    {
    }
    public object BeforeCall(string operationName, object[] inputs)
    {
        // validate parameters before call
        foreach (object input in inputs)
        {
            if ((input != null) && (input.GetType() == typeof(double)))
            {
                if ((double)input < minValue || (double)input > maxValue)
                {
                    throw new FaultException("Parameter out of range: " + input.ToString());
                }
            }
        }
        return null;
    }
}

The ParameterFilter is applied on the Multiply method as shown in the following sample code:

[ParameterFilter(MaxValue = 10, MinValue = 1)]
public double Multiply(double n1, double n2)
{
    return n1 * n2;
}

When you run the sample, the operation requests and responses are displayed in the client console window. In this sample, two operations are submitted to the service. The first multiply operation has the input values between 1 and 10, so the operation succeeds. The second multiply operation has input values outside of that range, and so a fault is sent back to the client to indicate that a parameter was out of range.

Multiply(2,5.25) = 10.5
System.ServiceModel.FaultException: Parameter out of range: 81.25

Press <ENTER> to terminate client.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. To build the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

Footer image

Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.