Wednesday 1 August 2012

WCF Extension and Custom Behavior

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
Class implementing the Interface:
Class implementing Interface
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
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
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
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
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
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

This way we can implement custom behaviors at client side.

Saturday 28 July 2012

To Promote Property in Pipeline Component

Steps to Promote Property in Pipeline Component:
1. First, Create an element in Property Schema with Property schema base value as “MessageDataPropertyBase”
(We are not associating this element with any Document schema and want to promote the value in Pipeline so that any document schema can use this Promoted Property).
Eg:
Property Schema
2. In Pipeline component, we can promote property in execute method of Icomponent Interface.
//IBaseMessage pInMsg
We want to promote property “UserName” and assign value “xyz” to it
//Second element in method is target Namespace of property Schema


pInMsg.Context.Promote(“UserName”,”Http://Test”,”xyz”)
return pInMsg;  //Returning value from Pipeline 


That’s it. This would promote property UserName in message context.
There is also a similar method called Context.Write  


pInMsg.Context.Write (“UserName”,”Http://Test”,”xyz”)


Difference between Context.Promote and Context.Write is using Context.Promote property is promoted in context of message and it can be used in Orchestration and at port level for routing.
But in Context.Write property is not promoted in message context and it can only be used in orchestration (like Distinguish property)

Property Schema: Property Schema Base

We use  Property Schema to promote a property (field)  so that we can access it in orchestration and use for Message routing.
Elements in property schema has different Property Schema Base value
Property Schema Base
MessageDataPropertyBase:
This is Default Schema base value of Element.
Promoted Property element which has MessageDataPropertyBase as Property Schema base can only be originated from associated Document Schema.
This kind of promoted element is limited to the Document schema.
In Orchestration, Document Schema element is associated with Promoted property Element using Promote – Show promotions (and also Quick Promotion) and than this promoted property can be used in Orchestration or Port Level.

MessageContextPropertyBase
Promoted Property element which has MessageDataPropertyBase as Property Schema base can be originated from associated Document Schema or promoted in Pipeline Components  or promoted using Correlation.
So this Property is not associated to particular Document Schema, any document schema can used this promoted property.

PartContextPropertyBase
It is like MessageContextPropertyBase but  promoted element with PartContextPropertyBase value can be used on a part of Multipart message.
For Example:
we have following Property Schema :
MessageContext element has Property Schema base as  MessageContextPropertyBase
MessageContext element has Property Schema base as PartContextPropertyBase
Property Schema Property Schema
Following is the Multipart Message Part
MultiPart Message
So if we want to add context to the Multipart message, we can use following code
MultiPartmsg(MyBiztalk.MessageContext) //where Multipartmsg is message with MultipartMessage Type

Now, if we want to add context to a part (called Body) of Multipart message,we can use
MultiPartmsg.Body(MyBiztalk.PartContext)


Silverlight: Combo Box showing empty value

In Silverlight 5.0, For some Combo Box selected value is becoming empty (or null) .
Only the Selected Value some how becomes null and Combo box looks like following:

image      image

There are couple of work around on this issue but i could not figure out why in the first place Combox selected value is getting null.
Workarounds:
  • First one, i got from this post, By Using ObservableCollection<T> in view code to populate the itemsource (or Datacontext) for the Combo Box.
    In this approach we would need to write code in code behind of xaml (i.e view code)
  • Second One, In Set accessor of “Selected Value” Property  put a check for null value.
public string SelectedValue
{
     get
     { return _svalue; }
     set
     {
         if (value != null)
         {
             _svalue = value;
             onPropertyChanged("SelectedValue"); //INotifyPropertyChange Event
         }
     }
}
Second Approach is easier but it more of a reactive approach.
HTH
Thanks