Implementation of verification parameters in WCF (1)

Source: Internet
Author: User

Write a service using WCFProgramSometimes some parameter verification is required. Of course, these parameter verification can be processed on the caller, but the service also needs to verify the parameters for the sake of insurance. If there are not many services, you can verify them directly in the service method, and then throw an exception or return an error result. However, this type of verification for a single method cannot be reused. We hope there is a method similar to model verification. You can add a tag or configure the attribute. After all, the verification is not the key content of WCF.CodeIt is not reasonable to put it into service implementation. WCF provides some extensions to verify whether the parameters are valid at run time. By defining some behavior and then adding it to the method to be verified, you can verify the method parameters.

The following describes the implementation methods for verifying input parameters in WCF.

Method 1:

Customize the behavior of the validation method, and then mark this behavior as an attribute on the method of the interface. The specific interfaces involved include iparameterinspector and ioperationbehavior.

Iparameterinspector

It is obviously used for parameter detection. The specific parameter detection is implemented in the beforecall method of this interface. The following defines an implementation of parameter length verification. When verification fails, an error message is transmitted by returning an exception. Parameterindex is the parameter index, and maxlength is only the maximum length of the parameter.

Lengthparameterinspector

1 Public Class Lengthparameterinspector: iparameterinspector
2 {
3 Public Int Parameterindex { Get ; Private Set ;}
4
5 Public Int Maxlength { Get ; Private Set ;}
6
7 Public Lengthparameterinspector ( Int Parameterindex, Int Maxlength)
8 {
9 This . Parameterindex = parameterindex;
10 This . Maxlength = maxlength;
11 }
12
13 Public Void Aftercall ( String Operationname, Object [] Outputs, Object Returnvalue, Object Correlationstate)
14 {
15 // Throw new notimplementedexception ();
16 }
17
18 Public Object Beforecall ( String Operationname, Object [] Inputs)
19 {
20 If (Inputs. Length <parameterindex)
21 {
22 Return Null ;
23 }
24
25 String Input = inputs [parameterindex] As String ;
26
27 If (Input! = Null )
28 {
29 If (Input. length> This . Maxlength)
30 {
31 Throw New Faultexception ( String . Format ( " The length of parameter {0} must less than {1} " , Parameterindex. tostring (), maxlength. tostring ()));
32 }
33 }
34
35 Return Null ;
36 }
37 }

Ioperationbehavior

It is the behavior interface used for the method. The iparameterinspector instance is added through the applydispatchbehavior method (partial red code ). It is actually an attribute, so it can be directly used in the service method.

Lengthvalidatebehavior

1 [Attributeusage (attributetargets. method, allowmultiple = False )]
2 Public Class Lengthvalidatebehavior: attribute, ioperationbehavior
3 {
4 Private Int Parameterindex;
5 Private Int Maxlength;
6
7 Public Lengthvalidatebehavior ( Int Parameterindex, Int Maxlength)
8 {
9 This . Parameterindex = parameterindex;
10 This . Maxlength = maxlength;
11 }
12
13 Public Void Addbindingparameters (operationdescription, system. servicemodel. channels. bindingparametercollection bindingparameters)
14 {
15 // Throw new notimplementedexception ();
16 }
17
18 Public Void Applyclientbehavior (operationdescription, system. servicemodel. Dispatcher. clientoperation)
19 {
20 // Throw new notimplementedexception ();
21 }
22
23 Public Void Applydispatchbehavior (operationdescription, system. servicemodel. Dispatcher. dispatchoperation)
24 {
25 Dispatchoperation. parameterinspectors. Add ( New Lengthparameterinspector (parameterindex, maxlength ));
26 // Throw new notimplementedexception ();
27 }
28
29 Public Void Validate (operationdescription)
30 {
31 // Throw new notimplementedexception ();
32 }
33 }

The usage in the method is as follows: check the length of the first parameter. The maximum length cannot exceed 10 characters.

View code

1 [Servicecontract]
2 Public Interface Itradeservice
3 {
4 [Operationcontract]
5 [Lengthvalidatebehavior ( 0 , 10 )]
6 String Tradesomething ( String Quotename );
7 }

After completing the preceding steps, you can perform simple parameter verification. However, this method is not suitable for multiple parameters, because in WCF, multiple behavior of the same type cannot be defined for the same method, even if allowmultiple = true is specified in attribute.

The second method is described below. The parameter verification is implemented by using the validation module of the enterprise database.

Method 2:

Subsequent Updates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.