Common WCF logic and code 2. Check beforecall and aftercall Parameters

Source: Internet
Author: User

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 >

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.