How to pass behaviour in WCF DataContract to client application?


Problem Statement: You want to override ToString or any method of DataContract class and method logic should be propagate to client application.

Resolution:

WCF doesn’t transfer any behavior of data contract to client side by default.
I am taking here two approaches to communicate to WCF service; through proxy class generated by Visual studio and other is creating channel instance using ChannelFactory class.In later approach, DataContract should be shared between server and clients.

Say you have data contract MarketPrice.

[DataContract]
public class MarketPrice
{
[DataMember]
public string Symbol { get; set; }

[DataMember]
public double Price { get; set; }

[OperationContract]
public override string ToString()
{
return string.Format("Symbol:{0} Price:{1}", Symbol, Price);
}
}

Approach 1:

Behavior of ToString() method will not propagate to client proxy. You will have to change in generated code like below:

wcf proxy

Approach 2:

You can read about how to create api or manual proxy class using channel factory here.

Service contract and Data contract are in shared library which is shared across server and client applications. Service contract implementation is still in service side.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: