Explain the difference between classic web services (ASMX) and WCF?
- easy and simple to write and configure
- only available in IIS
- only callable from HTTP
WCF can be:
- hosted in IIS, a Windows Service, a Winforms application, a console app - you have total freedom
- used with HTTP (REST and SOAP), TCP/IP, MSMQ and many more protocols
How can we host a service on two different protocols on a single server?
Let’s first understand what this question actually means. Let’s say we have made a service and we want to host this service using HTTP as well as TCP.
You must be wondering why to ever host services on two different types of protocol. When we host a service it’s consumed by multiple types of client and it’s very much possible that they have there own protocol of communication. A good service has the capability to downgrade or upgrade its protocol according the client who is consuming him.
Let’s do a small sample in which we will host the ServiceGetCost on TCP and HTTP protocol.
Once we are done the server side coding its time to see make a client by which we can switch between the protocols and see the results. Below is the code snippet of the client side for multi-protocol hosting
Explain the components used in WCF
Below are the essential components of WCF –
- Service class
- End point
- Hosting Environment
WCF Service is composed of three components:
Service class: It implements the service needed.
Host environment:It is an environment that hosts the developed service.
Endpoints: This are the connection points for the clients to connect to the service. Clients find the end points through three components like service contract, binding, and address.
Define Service Contracts and Operation Contracts in WCF?
A Service Contract basically describes the operations a service exposes to another party (in other words a client). We can map a WCF Service Contract to a Web Service Description Language (WSDL).
It's recommended to apply the ServiceContract attribute to an interface, although it can be applied to a class as well. Applying it to an interface provides us a clear separation of contract and its implementation.
- What operations are exposed by the service
- Platform independent description of the interface as well as methods of our service
- A Message Exchange Pattern (MEP) between the parties, in other words Request/Response, One-Way or Duplex. Please follow here for a detailed description of MEPs.
To define a Service Contract, we will apply the ServiceContract attribute to a .NET Interface and OperationContract attribute to methods
Explain dependency properties?
What is Self Hosting in WCF?
We have our project (it may be Windows or a web application or something else) and we want to host one WCF service within this solution locally. This type of hosting is called Self-Hosting. To implement self-hosting we need to include System.Service.Model.ServiceHost namespace.
We can host a WCF service in IIS and a Windows service also. A service can also be in-process, in other words the client and service are in the same process. Now let's create the WCF Service that is hosted in a console application.
Define Clients and Servers?
- A Client is generally a single user PC or workstation that provides a highly user friendly interface to the end user. It runs client processes, which send service requests to the server.
- A Server is generally a relatively large computer that manages a shared resource and provides a set of shared user services to the clients. It runs the server process, which services client requests for use of the resource managed by the server. The network may be a single LAN or WAN or an internet of networks.
A client is the requesting program or user in a client/server relationship. For example, the user of a Web browser is effectively making client requests for pages from servers all over the Web. The browser itself is a client in its relationship with the computer that is getting and returning the requested HTML file. The computer handling the request and sending back the HTML file is a server.
A server is a computer designed to process requests and deliver data to other(client) computers over a local network or the internet.
WCF ChannelFactory caching
What is WCF Messaging Layer?
Messaging layer is composed of channels. A channel is a component that processes a message in some way. Channels are the core abstraction for sending messages to and receiving messages from an Endpoint.
WCF uses messages to pass data or exchange information from one point to another. All messages are SOAP messages. The basic structures of a SOAP message are made up of three components.
The SOAP envelope is a container for the two most important pieces of a SOAP message, the SOAP header and the SOAP body. A SOAP envelope contains several pieces of key information in the form of elements.
Using a SOAP header, we can pass useful information about the services to the outer world if needed; it's just for information sharing. Any child elements of the header element are called "header blocks". This provides a mechanism for grouping logical data together.
This element contains the actual SOAP message for communication with the SOAP receiver; a message can contain zero or more bodies. Any information intended to be exchanged when the message reaches the intended destination goes in the message body.
Explain what are the MEPs available in WCF?
MEP stand for Message Exchange Pattern, three types of message exchanged patterns are allowed :-
- Data Gram
- Request and Response