Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Friday, February 20, 2009

WCF Contracts

A contract is at name implies is a standard way of describing what a service can do. Meanwhile, it is platform independent. In WCF we have 4 types of contract:
· Service Contract: It defines what a client can do with the service.
· Data Contract: It defines which data types can be transferred in the service. ( by default you can pass basic types like int, string however for custom types like a custom class you need to define the class as a Data Contract to be able to pass object of that class in the service)
· Fault Contract: It defines what are the errors raised by service.
· Message Contract: It is an extra functionality allows you to send message in a customized way (It means that you need to send a message in a specific way because let’s say there is an existing system that just understands messages in a specific way. As a user of WCF you should not usually use this as you do not need it except you are working with very specific system that needs specific messaging system)

Service Contract:
To allow a WCF Client to understand what they can do with the service you need to use Service Contract which is an attribute you need to add to the class. Also for each method that you wish WCF client to access you need add OperationContract attribute take a look at this sample

Class MyService
public string MyOperation()
return “hello World”;

As you see in above example we have ServiceContract for the class and we have OperationContract for the methods. Some Points:
· You can apply ServiceContract to a class or interface
· You have to apply OperatoinContract for methods otherwise it will not be accessible to clients
· You can only apply OperationContract to methods
· It is highly recommended first you better create an interface and apply the servicecontract then create a class that implements the interface (the reason will be discussed further)
· Methods accessibility is not important to WCF (public, private or internal) since they are CLR concepts not WCF
· Always the default constructor will be called (avoid parameterized constructors)
· You can define a Namespace for the ServiceContract (and it is suggested to do so) to avoid collision
Please see the same sample by using interface and namespace
interface IMyService
string MyOperation();
Class MyService:IMyService
public string MyOperation()
return “hello World”;

Friday, February 13, 2009

WCF Fundamentals

To work with WCF you need to understand these elements in WCF:
6-Metada exchanges
7- Client side Programming

In WCF every service is associated with a unique address. Each address has two specifications. First it has the location of the service and second it has transport protocol. The location has the name of machine, a communication port and an optional specific path called URI. (URI is a unique string). WCF supports these protocols:
· HTTP : we use Http
· TCP : we use net.tcp
· Peer Network : we use net.p2p
· IPC: we use net.pipe
· MSMQ: we use net.msmq

So the address has this format:
[transport protocol]:// [machine name][:optional port number]/[optional URI]
example: Http://myserver:4040/myservice

You use each protocol based on requirement latter on when we talk about binding you will see that there are different bindings and for each binding you need to use one of the protocol then you have different kind of serialization & Interoperable functionality.