In my previous post I described how to add multiple pushpins on Bing Map by fetching data from XML data source. In this post I will explain some basics of WCF Services to add multiple pushpins on Bing Map by fetching data from SQL Server Database.
We cannot access SQL Server directly from Silverlight. There needs to be a layer in between Silverlight and the SQL Server. You need to use
There are many resources available for WCF Services over the internet. Here I would like to describe basic concepts related with WCF required to achieve our main goal.
What is WCF
Windows Communication Foundation (WCF) is a part of the .NET Framework that provides a unified programming model for rapidly building service-oriented applications that communicate across the web and the enterprise.
WCF comes up as a replacement for Remoting and Web service in donet.
- It can be accesses only over HTTP.
- It works in stateless environment.
- It supports only the data types defined in the XSD type system, limiting the number of objects that can be serialized.
- It support interoperability across platforms, and are ideal for heterogeneous environments.
- It can be accessed over any protocol
- Provide support for both stateful and stateless environments through Singleton and SingleCall objects
- Using binary communication, .NET Remoting can provide support for rich type system.
- It requires the client be built using .NET, enforcing homogenous environment.
- WCF support of almost all protocols like TCP, MSMQ, NamedPipe , Peer2Peer and HTTP.
- WCF Service works asynchronously.
- WCF service can be hosted on self hosting application, IIS and WAS (windows application service).
- WCF is used to implement Service oriented architecture. Service oriented architecture means collection of services communicated with each other with the help of messages
ABC of WCF
- "A" stands for Address: Where is the service?
- "B" stands for Binding: How do I talk to the service?
- "C" stands for Contract: What can the service do for me?
Writing and configuring a WCF service is always a three-step process:
- You define a contract and implement it on a service
- You choose or define a service binding that selects a transport along with quality of service, security and other options
- You deploy an endpoint for the contract by binding it (using the binding definition, hence the name) to a network address.
The contract is a platform-neutral and standard way of describing what the service does.
WCF defines four types of contracts.
- Service Contracts
- Data Contracts
- Fault Contracts
- Message Contracts
Describe which operations the client can perform on the service. There are two types of Service Contracts.
ServiceContract - This attribute is used to define the Interface.
OperationContract - This attribute is used to define the method inside Interface.
Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but we can easily define explicit opt-in data contracts for custom types.
There are two types of Data Contracts.
DataContract - attribute used to define the class
DataMember - attribute used to define the properties
If DataMember attributes are not specified for a properties in the class, that property can't be passed to-from web service.
Define which errors are raised by the service, and how the service handles and propagates errors to its clients.
Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format we have to comply with.
Defining Service Contracts
A contract is defined explicitly, via a class. You add a [ServiceContract] attribute to the class. All methods you want to expose in your service, you mark as [OperationContract], as methods are called operations in services.
public class Hello
return "Hello world";
return "Hello computer";
It's always best to have an interface for our contract and have our actual service implement the interface. That's because
- Interfaces can extend/inherit other interfaces
- A single class can implement multiple interfaces
- You can modify the implementation of a service without breaking the contract
- You can version your service via old and new interfaces
The attributes also must be specified in the interface.
public interface IHello
public class Hello : IHello
return "Hello world";
return "Hello computer";
Defining Data Contracts
While a service contract defines the shape and rules for interfaces along with their associated messages and operations, the data contract defines the shape and rules for the data that is exchanged through operation's input and output messages.
See below a data contract for a person:
public class Person
public int Id;
public string FirstName;
public string LastName;
This article shows the basic concept of WCF service. Although there are many other stuff needs to be understand but I think above is sufficient to achieve our basic goal: Add multiple pushpins on the Bing Map by fetching data from SQL Server.
In the next article I will show that….