This sectionCodeYou can run it directly in wcf4.0. Copy the desired person...
I often forget the code... make a backup.
Although. Net already has built-in types for checking input parameters such as datalengthattribute
However, after all, the function is limited, so implementing your own parameter check will be more powerful.
Some items cannot be implemented with built-in functions, such as checking permissions during input, request, and recording running time.
The following is the C # code (this is only an implementation. iparameterinspector can also be applied to other places, such as endpoint and attribute)
View code
Public Class Validationbehaviorsection: behaviorextensionelement, iservicebehavior
{
Private Const String Enabledattributename = " Enabled " ;
[Configurationproperty (enabledattributename, defaultvalue = True , Isrequired = False )]
Public Bool Enabled
{
Get { Return ( Bool ) Base [Enabledattributename];}
Set { Base [Enabledattributename] = value ;}
}
Protected Override Object Createbehavior ()
{
Return This ;
}
Public Override Type behaviortype
{
Get
{
Return GetType ();
}
}
Private Validationparameterinspector getinstance ()
{
Return New Validationparameterinspector ();
}
Void Iservicebehavior. addbindingparameters (servicedescription, servicehostbase, collection <serviceendpoint> endpoints, bindingparametercollection bindingparameters)
{
}
Void Iservicebehavior. applydispatchbehavior (servicedescription, servicehostbase)
{
If (! Enabled)
{
Return ;
}
Validationparameterinspector inspector = getinstance ();
Foreach (Channeldispatcher chdisp In Servicehostbase. channeldispatchers)
{
Foreach (Endpointdispatcher epdisp In Chdisp. endpoints)
{
// Epdisp. dispatchruntime. messageinspectors. Add (New validationparameterinspector ());
Foreach (Dispatchoperation op In Epdisp. dispatchruntime. Operations)
Op. parameterinspectors. Add (inspector );
}
}
}
Void Iservicebehavior. Validate (servicedescription, servicehostbase)
{
}
}
Public Class Validationparameterinspector: iparameterinspector
{
Private Datetime starttime;
Public Void Aftercall ( String Operationname, Object [] Outputs, Object Returnvalue, Object Correlationstate)
{
}
Public Object Beforecall ( String Operationname, Object [] Inputs)
{
Starttime = datetime. now;
Return Null ;
}
}
The following is the configuration of Web. confg.
< Extensions >
< Behaviorextensions >
< Add Name = "Validator" Type = "{Namespace}. validationbehaviorsection, {DLL name }" />
</ Behaviorextensions >
</ Extensions >
< Behaviors >
< Servicebehaviors >
< Behavior >
< Validator />
</ Behavior >
</ Servicebehaviors >
</ Behaviors >