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;
}
}