We have a simple WCF WebService that has operation for Division (dividing value1 by Value2) and return this division result back to client.
Service Code:
Interface:
![ServiceContract ServiceContract](http://lh5.ggpht.com/-HqYoU7rfBL0/UBZu0bVdB9I/AAAAAAAAAlA/ZDtV3hmz6uw/clip_image0016_thumb%25255B1%25255D.png?imgmax=800)
Class implementing the Interface:
![Class implementing Interface Class implementing Interface](http://lh4.ggpht.com/-b5Lnb0yiBnI/UBZu2Pyx8VI/AAAAAAAAAlQ/rfPYG9GMR54/clip_image0026_thumb%25255B1%25255D.png?imgmax=800)
In Divide Method, DivideByException is thrown if value2 is zero. So we are handling this exception and sending NaN back to client.
Client Side Code: Proxy is generated by using Service reference.
![Client Side code Client Side code](http://lh5.ggpht.com/-qQI1eJtU85U/UBZu4F5q92I/AAAAAAAAAlg/EC0-yqy8bF4/clip_image0036_thumb%25255B1%25255D.png?imgmax=800)
In Client Side, we are just calling Divide Method by passing Value1 and Value2.
Following is the Tracing:
Traces:
In Step 2, we see the values passed by client and in Step3 we see values Received by Service.
![Debug View Debug View](http://lh6.ggpht.com/-kT3aCjsiJvk/UBZu5vdhiHI/AAAAAAAAAlw/61Wi6S_uS0Q/clip_image0046_thumb%25255B1%25255D.png?imgmax=800)
Now in the case when Value2 is Zero,there is a exception in Service and we are getting back NaN.
Below is the traces
![Debug View Debug View](http://lh4.ggpht.com/-VKqSLqpt5uk/UBZu7hlL69I/AAAAAAAAAmA/SXw6qv7UHk0/clip_image0056_thumb%25255B1%25255D.png?imgmax=800)
Suppose we have requirement to make Value2 as “1” when client is sending it as Zero.
To achieve this we can create custom behavior and custom behavior can be created at client side and server side.
For this case we will create Custom behavior (with extension) to change the value at client side.
So, When value2 is Zero, we need to make it “1” and send it to Service.
At Client Side, First Part would be to create Extensions. Now Extensions are the code in which we would implement the actual logic. For this particular scenario we would use IParameterInspector extension.
We would add check for zero value in BeforeCall method of Interface. Following way.
[Keep other methods empty]
![IParameterInspector IParameterInspector](http://lh5.ggpht.com/-xyX_m1NQB88/UBZu9mus34I/AAAAAAAAAmQ/jFw0xS5iaKc/clip_image006_thumb4.png?imgmax=800)
Note: There are other two extension we could use at client side i.e IClientFormatter and IClientMessageInspector
Once the Extensions is created it is time to create custom behavior, In WCF we have 4 types of behavior
1. Service Behavior – IserviceBehavior - Only Applicable for Service.
2. EndPoint Behavior - IEndpoint Behavior
3. Contract Behavior - IContract Behavior
4. Operation Behavior – IoperationBehavior
For this scenario, we would use IOperationBehavior interface.
Because our implementation is specify to an operation. If we implement using Endpoint or Contract behavior this would impact whole Client Run time (not a particular operation).
![IOperationBehavior IOperationBehavior](http://lh4.ggpht.com/-IfsPFVUVAKE/UBZu_gpzfPI/AAAAAAAAAmg/WA3QwiQfIUY/image_thumb2.png?imgmax=800)
We have created custom runtime extension and calling this extensions from custom behavior.
Now we will add this (DivideByValueBehavior) behavior to channelFactory.
objClient.ChannelFactory.Endpoint.Contract.Operations.Find("Divide").Behaviors.Add(new DivideByalueBehavior());
Please note when we add operation behavior to ChannelFactory we have to specify the Operation (Method) Name. In this case we are calling Divide Method, so we have specify this method name.
In Traces:
In Step2 - values which client is passing and Step3 values which Service is receiving.
As you can see Service is receiving value 2 as 1 instead of 0
![clip_image009 clip_image009](http://lh5.ggpht.com/-nLsqnE6uehs/UBZvBK56F3I/AAAAAAAAAmw/aS9kQyn4Z48/clip_image009_thumb%25255B1%25255D.png?imgmax=800)
This way we can implement custom behaviors at client side.
Service Code:
Interface:
![ServiceContract ServiceContract](http://lh5.ggpht.com/-HqYoU7rfBL0/UBZu0bVdB9I/AAAAAAAAAlA/ZDtV3hmz6uw/clip_image0016_thumb%25255B1%25255D.png?imgmax=800)
Class implementing the Interface:
![Class implementing Interface Class implementing Interface](http://lh4.ggpht.com/-b5Lnb0yiBnI/UBZu2Pyx8VI/AAAAAAAAAlQ/rfPYG9GMR54/clip_image0026_thumb%25255B1%25255D.png?imgmax=800)
In Divide Method, DivideByException is thrown if value2 is zero. So we are handling this exception and sending NaN back to client.
Client Side Code: Proxy is generated by using Service reference.
![Client Side code Client Side code](http://lh5.ggpht.com/-qQI1eJtU85U/UBZu4F5q92I/AAAAAAAAAlg/EC0-yqy8bF4/clip_image0036_thumb%25255B1%25255D.png?imgmax=800)
In Client Side, we are just calling Divide Method by passing Value1 and Value2.
Following is the Tracing:
Traces:
In Step 2, we see the values passed by client and in Step3 we see values Received by Service.
![Debug View Debug View](http://lh6.ggpht.com/-kT3aCjsiJvk/UBZu5vdhiHI/AAAAAAAAAlw/61Wi6S_uS0Q/clip_image0046_thumb%25255B1%25255D.png?imgmax=800)
Now in the case when Value2 is Zero,there is a exception in Service and we are getting back NaN.
Below is the traces
![Debug View Debug View](http://lh4.ggpht.com/-VKqSLqpt5uk/UBZu7hlL69I/AAAAAAAAAmA/SXw6qv7UHk0/clip_image0056_thumb%25255B1%25255D.png?imgmax=800)
Suppose we have requirement to make Value2 as “1” when client is sending it as Zero.
To achieve this we can create custom behavior and custom behavior can be created at client side and server side.
For this case we will create Custom behavior (with extension) to change the value at client side.
So, When value2 is Zero, we need to make it “1” and send it to Service.
At Client Side, First Part would be to create Extensions. Now Extensions are the code in which we would implement the actual logic. For this particular scenario we would use IParameterInspector extension.
We would add check for zero value in BeforeCall method of Interface. Following way.
[Keep other methods empty]
![IParameterInspector IParameterInspector](http://lh5.ggpht.com/-xyX_m1NQB88/UBZu9mus34I/AAAAAAAAAmQ/jFw0xS5iaKc/clip_image006_thumb4.png?imgmax=800)
Note: There are other two extension we could use at client side i.e IClientFormatter and IClientMessageInspector
Once the Extensions is created it is time to create custom behavior, In WCF we have 4 types of behavior
1. Service Behavior – IserviceBehavior - Only Applicable for Service.
2. EndPoint Behavior - IEndpoint Behavior
3. Contract Behavior - IContract Behavior
4. Operation Behavior – IoperationBehavior
For this scenario, we would use IOperationBehavior interface.
Because our implementation is specify to an operation. If we implement using Endpoint or Contract behavior this would impact whole Client Run time (not a particular operation).
![IOperationBehavior IOperationBehavior](http://lh4.ggpht.com/-IfsPFVUVAKE/UBZu_gpzfPI/AAAAAAAAAmg/WA3QwiQfIUY/image_thumb2.png?imgmax=800)
We have created custom runtime extension and calling this extensions from custom behavior.
Now we will add this (DivideByValueBehavior) behavior to channelFactory.
objClient.ChannelFactory.Endpoint.Contract.Operations.Find("Divide").Behaviors.Add(new DivideByalueBehavior());
Please note when we add operation behavior to ChannelFactory we have to specify the Operation (Method) Name. In this case we are calling Divide Method, so we have specify this method name.
In Traces:
In Step2 - values which client is passing and Step3 values which Service is receiving.
As you can see Service is receiving value 2 as 1 instead of 0
![clip_image009 clip_image009](http://lh5.ggpht.com/-nLsqnE6uehs/UBZvBK56F3I/AAAAAAAAAmw/aS9kQyn4Z48/clip_image009_thumb%25255B1%25255D.png?imgmax=800)
This way we can implement custom behaviors at client side.