Focus on the extension of WCF behavior

Source: Internet
Author: User

WCF is convenient for developers with its flexible extensible architecture, where the expansion of behavior may be the most common in applications. Custom extensions to behavior are not complicated, but there are still a lot of details to be aware of. On the server side, the DispatchRuntime and dispatchoperation are generally extended, and the extension points include checks for parameters and messages, as well as Operation invocations, which correspond to the interfaces of Iparameterinspector, Idispatchmessageinspector and IOperationInvoker. On the client side, the ClientRuntime and ClientOperation are extended, and the extension points include checks for parameters and messages, the corresponding interfaces are Iparameterinspector and Iclientmessageinspector respectively. These interface types are defined under the System.ServiceModel.Dispatcher namespace, where the Iparameterinspector interface can act concurrently on both the server and the client.

Implementations of these interfaces are somewhat analogous to AOP implementations, which can inject additional logic before and after the method invocation, so these extensions are often referred to as listeners. For example, the Iparameterinspector interface defines the following methods:

void Aftercall (String operationname, object[] 

outputs, Object returnvalue, Object correlationstate); 

Object Beforecall (String OperationName, object[] 

inputs);

The Beforecall method is called before the target method of the service object is invoked, and the Aftercall method is invoked after the call. For example, we can verify that the parameter of the calculation method is less than 0 before the method call, or throw an exception if less than 0:

public class Calculatorparameterinspector:iparameterinspector

{public

void Beforecall (String operationname        , object[] inputs)

{

int x = inputs[0] as int;

int y = inputs[1] as int;

if (x <0 | | y < 0)

{

throw new FaultException ("The number can not be 

less than zero."        );

return

null;

} public

void Aftercall (String operationname, object[] outputs, 

object ReturnValue, Object correlationstate)   

{

//empty;

}

}

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.