WCF provides communication for client applications using Endpoints. Endpoints provide the configuration required for the communication and create the complete WCF service application.
So let us learn about the WCF Endpoints.
The Endpoints will look such as follows:
Key Points of Service contract
So let us learn about the WCF Endpoints.
Endpoints
Endpoints are a combination of ABC, that is Address, Binding and Contracts. Let us look at the following diagram:
These Endpoints are used to configure the communication channel between the client application and the WCF service. This configuration is done in the Web.config file.The Endpoints will look such as follows:
<endpoint address="http://localhost:3901/Service1.svc"
binding="basicHttpBinding"
contract="ServiceReference.IService1" bindingConfiguration="BasicHttpBinding_IService1"
name="BasicHttpBinding_IService1" />
As discused, Endpoints consist of
the following:
- Address
- Binding
- contracts
Now let us learn about each Endpoint.
Addresses
An address
is the URL that defines where the WCF service is hosted. Suppose I have hosted
a WCF Service Application in my local IIS then the address will look such as
follows.
Binding
Bindings are
nothing but the transport protocols that are used for the communication with
the client application. WCF provides a wide range of transport protocols that
can be used as in the requirements.
A binding
has the following characteristics:
- Transport: Defines the base protocol to be used, like HTTP, Named Pipes, TCP and MSMQ are some of the protocols.
- Encoding (Optional): Three types of encodings are available, they are:
1.
Text
Encoding
2.
Binary
Encoding
3.
Message
Transmission Optimization Mechanism (MTOM)
3. Protocol (Optional): Defines the information to be used in the binding
such as Security, transaction or reliable messaging capability.
The
following are some of the bindings that are widely used, depending on the
requirements of the application:
- BasicHttpBinding: This is the default WCF binding and this basic protocol is similar to HTTP without security.
- WebHttpBinding: This binding is used when the service is communicating with a client application using WCF REST such as get or post.
- WSHttpBinding: This is a communication protocol with security similar to the HTTPS protocol.
- WSDualHttpBinding: Supports duplex contracts and transactions.
- WSFederationHttpBinding: Binding with federated security and transactions support.
- MsmqIntegrationBinding: Communication directly with MSMQ applications along with transaction support.
- NetMsmqBinding: Communication among WCF applications using queuing and transaction support.
- NetNamedPipeBinding: Communication among WCF applications on the same computer that support duplex contracts and transactions.
- NetPeerTcpBinding: Communication among computers across peer-to-peer services that support duplex contracts.
- NetTcpBinding: Communication among WCF applications across computers, supports duplex contracts and transactions.
The
preceding are the most commonly used bindings, however many other bindings are
also supported by WCF.
For example:
<bindings>
<BasicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
Contracts
Contracts are nothing but a collection of attributes that
give special meanings to the methods and classes. Contracts are a collection of
the following.
- Service contract
- Operation contract
- Data contract
- Message contract
- Fault contract
Service contract
Service contracts are nothing but definition of the interface for the service, those interfaces are considered to be a Service contract of the WCF service marked with the Service contract attribute. It can be defined as follows:
[ServiceContract]
public
interface IService1
{
//
TODO: Add your service operations here
}
The Service contract attribute has the following
properties:
- CallbackContract: Gets or sets the type of callback contract when the contract is a duplex contract.
- ConfigurationName: Gets or sets the name used to locate the service in an application configuration file.
- HasProtectionLevel: Gets a value that indicates whether the member has a protection level assigned.
- Name: Gets or sets the name for the <portType> element in the Web Services Description Language .
- Namespace: Gets or sets the namespace of the <portType> element in the Web Services Description Language.
- ProtectionLevel: Specifies whether the binding for the contract must support the value of the ProtectionLevel property.
- SessionMode: Gets or sets whether sessions are allowed, not allowed or required.
- TypeId: When implemented in a derived class, gets a unique identifier for this attribute.
Key Points of Service contract
- Any WCF Service can have more than one Service contract.
- It must declare at least one Service contract in a service.
- The Service contract can be declared using the [ServiceContract] attribute.
- It allows defining an Operation contract under it to expose the service outside the world.
- It maps the interface and methods of your service to a platform-independent description.
- It describes message exchange patterns that the service can have with another party. Some service operations might be one-way, others might require a request-reply pattern.
Operation contract
An Operation contract defines the method that is exposed to
the client to exchange the information between the client and the server. An
Operation contract describes what functionality is to be given to the client,
such as addition, subtraction and so on. The methods that are marked with the
Operation contract attribute is termed an Operation contract.
It can be defined as:
[OperationContract]
string
GetData(int value);
An Operation contract has the
same properties as a Service contract as follows:
- CallbackContract: Gets or sets the type of callback contract when the contract is a duplex contract.
- ConfigurationName: Gets or sets the name used to locate the service in an application configuration file.
- HasProtectionLevel: Gets a value that indicates whether the member has a protection level assigned.
- Name: Gets or sets the name for the <portType> element in the Web Services Description Language.
- Namespace: Gets or sets the namespace of the <portType> element in the Web Services Description Language.
- ProtectionLevel: Specifies whether the binding for the contract must support the value of the ProtectionLevel property.
- SessionMode: Gets or sets whether sessions are allowed, not allowed or required.
- TypeId: When implemented in a derived class, gets a unique identifier for this attribute.
Message
contract
A Message contract provides an
alternate way of creating and formatting the SOAP messages. By default WCF
communicates with a client application using a SOAP message format but we can
create a custom message format using the Message contract attribute. Message
contracts are also defined in a manner similar to other contracts.
For example:
[MessageContract]
public
class Employee
{
[MessageHeader]
public string
Name;
[MessageBodyMember]
public decimal
salary;
}
Data
contract
A Data contract is similar to get
and set properties of our normal programming but it defines how data types
are serialized and desterilized. Using serialization, you convert an object
into a sequence of bytes that can be transmitted over a network. Using deserialization,
you reassemble an object from a sequence of bytes that you receive from a
calling application.
Key points
- A Data contract is included in the System.Runtime.Serialization namespace.
- A class marked with the Data contract attribute is termed a Data contract.
- Members and Data contracts are marked as a Data Member.
For example:
[DataContract]
public
class Student
{
private
string _Name;
private
string _City;
[DataMember]
public
string Name
{
get { return _Name;
}
set { _Name = value;
}
}
Fault contract
It is used to handle the
exception and know the cause of the error that occured in the WCF service. By default
when we throw an exception from a service, it will not reach the client side
due to the lack of interoperability so for that WCF provides the option to
handle and convey the error message to the client from the service using a SOAP
Fault contract.
For example:
[ServiceContract]
public interface IGetDetailsService
{
[OperationContract]
[FaultContract(typeof(Student))]
Student GetDetails(string
Name);
}
[DataContract]
public class Student
{
private string
_Name;
private string
_City;
[DataMember]
public string
Name
{
get { return _Name;
}
set { _Name = value;
}
}
[DataMember]
public string
City
{
get { return _City;
}
set { _City = value;
}
}
}
Summary
From the receding examples and explanations we have learned about the WCF endpoints. In my next article we will learn in detail about each endpoint. I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contacts me.
Post a Comment