Windows Communication Foundation_Unit_01
-
Upload
prashanth-shivakumar -
Category
Technology
-
view
91 -
download
0
Transcript of Windows Communication Foundation_Unit_01
In this session, you will learn to:Identify WCF
Explore the programming model of WCF
Objectives
WCF is a unified programming model for developing service-oriented applications.
It combines the features of all the distributed technologies, such as COM+ services, .NET Remoting, and Web services.
It enables you to build a secure, reliable, and robust application.
It enables two applications to communicate across platforms in a distributed environment.
Identifying WCF
To understand the architecture of WCF, you should be aware of the following fundamentals of WCF:
Unification: WCF enhances the development of distributed applications and increases developer’s productivity by unifying the following technologies:
COM+ services
.NET Remoting
Web services
Web Services Enhancements (WSE)
Microsoft Message Queuing
Interoperability: WCF enables two applications built on the same or different platforms to communicate with each other within or across a network.
Fundamentals of WCF
Service orientation: WCF provides a highly productive programming model for building distributed systems that involve developing loosely-coupled services.
The following figure illustrates the fundamentals of WCF.
Fundamentals of WCF (Contd.)
The following figure illustrates the architecture of WCF.
Architecture of WCF
Architecture of WCF (Contd.)
The WCF architecture contains the following layers:The contract layer: It contains the various types of contracts, policies, and bindings used in WCF. The various types of contracts present in the contract layer are:
The data contract
The message contract
The service contract
Architecture of WCF (Contd.)
The service runtime layer: It specifies how the service will behave when it runs or executes. This layer contains the various types of runtime behaviors of the service. These runtime behaviors are:
Throttling behavior
Error behavior
Metadata behavior
Instance behavior
Message inspection
Transaction behavior
Dispatch behavior
Concurrency behavior
Parameter filtering
Architecture of WCF (Contd.)
The messaging layer: It consists of channels through which a message is processed and transported to a client accessing the service. This layer consists of two types of channels:
Transport channels
Protocol channels
The activation and hosting layer: It supports the execution of services in different environments, such as Windows services, IIS, and Windows Activation Service (WAS). A WCF service can be hosted in various ways. These ways are:
Self-hosting
IIS
Windows service
WAS
Exploring the Programming Model of WCF
All communication with a WCF service occurs through the endpoints of the service.
An endpoint of a WCF service acts as a gateway for communicating with other applications.
It is composed of an address, a binding, and a contract known as the ABC of endpoint, as shown in the following figure.
The address of a WCF service specifies the location where the service resides.
It is represented in the form of a URL that defines:The protocol to be used for sending and receiving messages.
The name of the system where the service runs.
The port number at which the service listens to the client requests.
The path where the service resides and the name of the service.
The following code snippet shows how you can specify the address of a WCF service:
address="http://MyComputer:3577/CarDetailsWCFService/Service.svc"
Address
It describes how a WCF service communicates with a client application.
It specifies the communication details required to connect to the endpoint of a WCF service.
It consists of the message encoder and protocol binding element.
WCF provides the following types of bindings to enable a client application to communicate with a WCF service:
basicHttpBinding
wsHttpBinding
netTcpBinding
netNamedPipeBinding
netMsmqBinding
netPeerTcpBinding
Binding
It exposes the interfaces, classes, methods, and variables of a WCF service to enable client applications to access and use them.
A WCF service may contain the following types of contracts:Service contract
Operation contract
Data contract
Message contract
Fault contract
Contract
Service Contract:Acts as an entry point to access a WCF service.
Is implemented as an interface.
Is defined by declaring the [ServiceContract] attribute in a WCF service, as shown in the following code snippet:[ServiceContract]
public interface IService
{
}
Contract (Contd.)
Operation Contract:Exposes the operations that a service can perform.
Defines the methods of a WCF service and the parameters and return types of the methods.
Is defined by declaring the [OperationContract] attribute in the WCF service, as shown in the following code snippet: [ServiceContract]
public interface IService
{
[OperationContract]
DataSet GetDetails(int Employee_ID);
}
Contract (Contd.)
Data Contract:Is used to expose user-defined data types in a WCF service.
Serializes the user-defined data types in a standard format (XML).
Is defined by using the [DataContract] attribute in the WCF service, as shown in the following code snippet:[DataContract]
public class Employees
{ [DataMember]
public int emp_id;
[DataMember]
public int emp_name;
}
Contract (Contd.)
Message Contract:Describes the structure of a message exchanged between a WCF service and a client application.
Enables you to inspect and control the information contained in a message.
Is defined by using the [MessageContract], [MessageBodyMember], and [MessageHeader]attributes, as shown in the following code snippet:[MessageContract]
public class StoreMessage
{
[MessageHeader]
public DateTime CurrentTime;
[MessageBodyMember]
public Employees emp_id; }
Contract (Contd.)
Fault Contract:Enables you to send a customized error message to a client by creating a user-defined class.
Enables you to control the situation when a WCF service encounters an error.
Contract (Contd.)
Is defined by using the [FaultContract] attribute, as shown in the following code snippet:[ServiceContract]
public interface IService
{
[OperationContract]
[FaultContract(typeof(SampleFaultException))]
DataSet GetDetails(int Employee_ID);
}
[DataContract]
public class SampleFaultException
{
[DataMember]
public string errorMessage;
}
Contract (Contd.)
Problem Statement: Luxury Drive is using an XML Web service that provides the details about the various models of the cars manufactured by the company. The Web service is used by the company’s call center and the distributors. The call center employees use a client application developed in .NET to access the Web service. However, the distributors use various applications having different platforms to access the Web service.
The management at Luxury Drive wants their Web service to be such that it can accommodate features such as reliable messaging, transactions, and security in future. The management has conveyed the same to the developers.
The developers know that they can implement all these features in the existing XML Web service. However, it would be difficult because each of these features is provided by different technologies and merging all these technologies is a time-consuming and complex task.
Activity 5.1: Creating a WCF Service
Therefore, the developers decided to create the Web service by using WCF because WCF is the unification of all these technologies and provides all the required features. You, as a distributed application developer, have to create a WCF service that provides the details about the various models of the cars manufactured by the company.
Prerequisite: You need to use the LuxuryDrive database for this activity. Ask your faculty to provide you with the database and the QueryCarDetailsClientApp client application.
Activity 5.1: Creating a WCF Service (Contd.)
Solution:To create the required WCF service, you need to perform the following tasks:
1. Create a WCF service.
2. Verify the WCF service.
Activity 5.1: Creating a WCF Service (Contd.)
In this session, you learned that:WCF is a unified programming model for building service-oriented applications. It combines the features of all the distributed technologies, such as:
COM+ services
.NET Remoting
Web services
The fundamentals of WCF are:Unification
Interoperability
Service orientation
The layers of the WCF architecture are:Contract
Service runtime
Messaging
Activation and hosting
Summary
An endpoint of a WCF service acts as a gateway for communicating with other applications.
An endpoint is composed of:Address
Binding
Contract
An address of the service specifies the location where the service resides.
Binding describes how a WCF service communicates with a client application.
Summary (Contd.)
Some of the bindings provided by WCF are:basicHttpBinding
wsHttpBinding
netTcpBinding
netNamedPipeBinding
netMsmqBinding
netPeerTcpBinding
Contracts specify the content of a message, such as methods and variables and the functionality provided by a WCF service.
The various types of contracts in a WCF service are:Service contract
Operation contract
Data contract
Message contract
Fault contract
Summary (Contd.)