Thank you everyone for my liking and sharing my last post on WCF. We are now aware of assemblies and namespace required to create the service. In this post we will explore ABC of WCF.
WCF offers much more functionality than Web services, few features are Security, Multiple Transport and Encoding and Transaction etc. There are multiple bindings option available , which supports different features, let’s explore them in more details. Before creating our WCF service we will see ABC of WCF.
ABC(Address, Binding and Contract):
The most important thing to remember in WCF, the client and service communicate with each other by agreeing on this 3.
Address: This let’s you know the location on server. Different bindings support different address types.
Binding: Defines which protocol is being used.
Contract: This defines the each method exposed from service.
Let’s see each one in more detail:
As described above contracts are nothing but, what service is going to share with outside world.There are multiple types of attributes, which completes the service.
An interface is decorated with ServiceContract attribute to participate in WCF service. It defines below things:
- Name and namespace for the service.
- Signature of the service.
- Location of service.
- Data Type of the message.
It offers below Properties which can be used:
- CallbackContract: It is used in case of duplex binding, to setup the callback functionality.
- ConfigurationName: Default name is name of service implementation call. It is optional.
- ProtectionLevel: Allows us to specify the degree to which we need the encryption, default is none, we can provide digital signatures as well.
- SessionMode: Used to specify if the service supports session are allowed, not allowed or required.
2. [OperationContract] Attribute
Methods are decorated with OperationContract attribute,so they can participate in service.
Below are the properties which it provides:
- AsyncPattern: Indicates whether the operation is implemented asynchronously.
- IsInitiating: Specifies whether the operation can be initial operation in session.
- IsOneWay: Specifies if operation is single input message with no output.
- IsTerminating: Specifies whether the runtime should attempt to terminate the current session after the operation completes.
Data Contract is nothing but the entities/ classes with properties, we need to decorate them with DataContract attribute so they can be serialized and deserialized.
The properties within classes are decorated with [DataMember] attribute, so they can take part in service data exchange. Your return type can be any of the variable type like string, int and Boolean etc, but if we are using the complex data type in this case our own class, it should be marked with [DataContract] attribute.
Below are the properties which it supports:
- IsReference : value that indicates whether to preserve object reference data.
In most of the cases a Data Contract is sufficient to control the service, but in case we need better control over our SOAP message being created we can use Message Contract. using this we can define header and body, which can be another data contract.
Once we are done with the defining contracts, next step is to choose a hosting agent. There are several option to choose from, which depends on our need. Each binding is for specific needs, below are some of the characteristics:
- The transport layer used
- The channel used
- Encoding mechanism
- Web service protocol support
We can divide the bindings in 3 types HTTP based, TCP based and MSMQ based, let’s see each one
- HTTP-based: If we want our service to be accessed across multiple OS or multiple programming architectures, HTTP based bindings are our oblivious choice. Let’s see the bindings supported.
- BasicHttpBinding: It is used in case of web service, offers backward compatibility, the message encoding used is Text/XML, supports WS-basic profile.
- WsHttpBinding: It gives all functionality which BasicHttpBinding offers, apart from that it offers transaction support, reliable message and WS-Addressing.
- WSDualHttpBinding: Offers all functionality offered by WsHttpBinding, but the main purpose is to be used with duplex communication.
- WsFederationHttpBinding:This is used when security within the organization is most important aspect.
- TCP-based: If we want to share the data in compact binary format, these bindings are of best use.
- NetNamedPipeBinding: This is best biding to be used if our service and client both are hosted on same machine, use TCP protocol to exchange data.It supports transaction, reliable session and secure communication.
- NetPeerTcpBinding: This binding provided secure binding for P2P network, offers all functionality fro NetNamedPipeBinding.
- NetTcpBinding: Used for secure and optimized binding for cross-machine communication between .net application.
- MSMQ-based: If we want to use MSMQ server to exchange data, we can use these bindings
- MsmqIntegrationBinding: We can use this binding to send and receive data from existing MSMQ application that use COM, C++.
- NetMsmqBinding: This is used to communicate between cross-machine using queue. This is preferred binding when using MSMQ.
Once we are done with defining contracts and finalizing the binding, final information left is the address. This is most important aspect as client will not be able to access our service without an proper address. The format of the service address depends upon the type on binding we are using.
If we see from high level below are the things which our address represents:
- Scheme: The transport protocol (HTTP, TCP etc)
- MachineName: Fully qualified domain name of the machine.
- Port: This is optional if we are using default 80 port
- Path: The path of WCF service.
The information above can be used to define a common template for address.
In Case of HTTP-based binding, this can be represented as:
In case of TCP- based binding apart from name pipes, this can be represented as:
MSMQ bindings are bit different as we can choose from public of private queue:
In case of name pipes, it goes like:
Now we are aware of ABC of WCF service as well as the namespace and core assemblies used to create the WCF service. While creating any WCF service, we need to choose the type of binding carefully, as the list of features we can use depends upon them.
TCP bindings are most secure and reliable of them, but i we need to expose the service to multiple client HTTP would be obivious choice.
In our next post we will create our first WCF service.
You can follow my official facebook page , also subscribe my blog for more information.
You can also mail me on email@example.com in case you have any questions.