WCF (Windows Communication Foundation) is a one of the Programming Platform. The initial name for WCF was Indigo and it was designed for building service-oriented apps and offers interoperability too. It is a Microsoft framework to design applications or services, distributed framework over the network on a variety of platforms. Microsoft first introduced WCF in .Net framework 3.0, and later used extensively in frameworks 3.5 and 4.0 respectively. Before the introduction of WCF, developers were already using Asp.Net Web Services to design service-oriented applications, for distributed environments.
WCF stand for Windows Communication Foundation. It is a Microsoft framework for building Service oriented Applications. It focuses on Connecting XML to Programs that are built using development languages supported by Microsoft(C# and VB.Net). It can be used to create or deploy services that are easily accessible by multiple clients worldwide.
WCF fundamentals: Interoperability Service Orientation Unification WCF Architecture:
A binding defines how an endpoint communicates to the world. A binding defines the transport (such as HTTP or TCP) and the encoding being used (such as text or binary). A binding can contain binding elements that specify details like the security mechanisms used to secure messages, or the message pattern used by an endpoint. WCF supports nine types of bindings. Basic binding: It is offered by the BasicHttpBinding class, this is designed to expose a WCF service as a legacy ASMX web service, so that old clients can work with new services. When used by the client, this binding enables new WCF clients to work with old ASMX services. TCP binding: It is offered by the NetTcpBinding class, this uses TCP for cross machine communication on the intranet. It supports a variety of features, including reliability, transactions, and security, and is optimized for WCF to WCF communication. As a result, it requires both the client and the service to use WCF. Peer network binding: It is offered by the NetPeerTcpBinding class, this uses peer networking as a transport. The peer network enabled client and services all subscribe to the same grid and broadcast messages to it. IPC binding: It is offered by the NetNamedPipeBinding class, this uses named pipes as a transport for same machine communication. It is the most secure binding since it cannot accept calls from outside the machine and it supports a variety of features similar to the TCP binding. Web Service (WS) binding: It is offered by the WSHttpBinding class, this uses HTTP or HTTPS for transport, and is designed to offer a variety of features such as reliability, transactions, and security over the Internet. Federated WS binding: It is offered by the WSFederationHttpBinding class, this is a specialization of the WS binding, offering support for federated security. Duplex WS binding: It is offered by the WSDualHttpBinding class, this is similar to the WS binding except it also supports bidirectional communication from the service to the client. MSMQ binding: It is offered by the NetMsmqBinding class, this uses MSMQ for transport and is designed to offer support for disconnected queued calls. MSMQ integration binding: It is offered by the MsmqIntegrationBinding class, this converts WCF messages to and from MSMQ messages, and is designed to interoperate with legacy MSMQ clients.
Contract is a platform-neutral and standard way of describing what the service does .The main objective of defining contracts is to make agree with both client and server for the same operations, structures, data types etc. This is important to establish the secure communication between the two. This is a formal agreement to define the platform neutrality and standards as well as what is done by the services.
Here is given most common types of contracts that are frequently used within WCF technology. Service Contract: Service contracts describe the operation that service can provide. For Eg, a Service provide to know the temperature of the city based on the zip code, this service is called as Service contract. It will be created using Service and Operational Contract attribute. Operation Contract: An operation contract is defined within a service contract. Operation contact defines the parameters and return type of an operation. An operation contract can also defines operation-level settings, like as the transaction flow of the op-eration, the directions of the operation (one-way, two-way, or both ways), and fault contract of the operation. Data Contract: Data contract describes the custom data type which is exposed to the client. This defines the data types that are passed to and from service. Data types like int, string are identified by the client because it is already mention in XML schema definition language document, but custom created class or data types cannot be identified by the client e.g. Employee data type. By using Data Contract we can make client to be aware of Employee data type that are returning or passing parameter to the method. Message Contract: Default SOAP message format is provided by the WCF runtime for communication between Client and service. If it is not meeting your requirements then we can create our own message format. This can be achieved by using Message Contract attribute. Fault Contract: Suppose the service I consumed is not working in the client application. I want to know the real cause of the problem. How I can know the error? For this we are having Fault Contract. Fault Contract provides documented view for error occurred in the service to client. This helps us to easy identity, what error has occurred.
Service class: The service runtime layer contains the behaviours that occur only during the actual operation of the service, that is, the runtime behaviours of the service. Throttling controls how many messages are processed, which can be varied if the demand for the service grows to a preset limit. Endpoint: WCF Service is a program that exposes a collection of the endpoints. Each Endpoint is a portal for communicating with the world. All the WCF communications are taken place through the endpoint. An endpoint consists of three components. Hosting Environment: A service must be hosted in some process. A host is an application that controls the lifetime of the service. Services can be self-hosted or managed by an existing hosting process.
WCF RIA is the framework for developing the n-tier application for RIA (Rich Internet App). It is used in Rich Internet Apps like Silverlight, AJAX etc. WCF RIA is used to solve the major problems like Tight Coupling.
A WCF service can be self-hosting using a console application or Windows Forms applications. Hosting a WCF service in any managed .Net application is called self-hosting. Now the following is the procedure for creating a WCF service and hosting it in a console application. Hosting Environment Requirements: Availability: When do you want to be able to reach your service? Reliability: What happens when your service somehow breaks? How does this affect other consumers? Manageability: Do you need easy access to information about what is happening on the host where WCF services live? Versioning: Do you need to support older versions of the service? Do you know who is consuming your services? Deployment: What is your deployment model? Are you installing through the Microsoft Installer process and Visual Studio deployment packages, or is xcopy sufficient? State: Are your services stateless? Do you need sessions? The following are the various options available for hosting a WCF Service:
SOA stand s for Service oriented Architecture .It helps to organize apps as Services. This is a group of methods that define the business logic and helps to connect with DB and other services quickly. Take an example of the restaurant where you go and order food. Your order will go from counter to the kitchen and it is served on your table finally.
ABC (Address Bindings Contracts) is the three building blocks of WCF and they are known as A Address (Where): Address tells us where to find the services, like URL B Bindings (How): Bindings tells us how to find the services or using which protocols finds the services (SOAP, HTTP, TCT etc.) C Contacts (What): Contracts are an agreement between the consumer and the service providers that explains what parameters the service expects and what return values it
A transaction is a logical unit that defines multiple activities in WCF either pass or fails. The two common types of transactions are – Atomic and Long Running. Two phases of transactions are Prepare Phase and the Commit Phase.
WCF supports following three types of transactions managers:
Transport reliability (such as the one offered by TCP) offers point to point guaranteed delivery at the network packet level, as well as guarantees the order of the packets. Transport reliability is not resilient to dropping network connections and a variety of other communication problems.
Address Header contains the information which is sent with every request, it can be used by either end point service or any intermediate device for determining any routing logic or processing logic.
Message reliability deals with reliability at the message level independent of how many packets are required to deliver the message. Message reliability provides for endtoend guaranteed delivery and order of messages, regardless of how many intermediaries are involved, and how many network hops are required to deliver the message from the client to the service.
The XML Information Set defines a data model for XML. It is an abstract set of concepts such as attributes and entities that can be used to describe a valid XML document. According to the specification, “An XML document’s information set consists of a number of information items; the information set for any wellformed XML document will contain at least a document information item and several others.”
Duplex messaging or call-back is used to communicate with the client. Over different transport system Duplex messaging in WCF is done like TCP, Named pipe and even HTTP. Collectively this is known as duplex contracts in WCF. Basically duplex service contract is a message exchange pattern in which both endpoints can send messages to the other independently. A duplex service, therefore, can send messages back to the client endpoint, providing event-like behavior. Duplex communication occurs when a client connects to a service and provides the service with a channel on which the service can send messages back to the client. Note that the event-like behavior of duplex services only works within a session.
There are three major ways of hosting a WCF services Self hosting the service in his own application domain. This we have already covered in the first section. The service comes in to existence when you create the object of Service Host class and the service closes when you call the Close of the Service Host class. Host in application domain or process provided by IIS Server. Host in Application domain and process provided by WAS (Windows Activation Service) Server.
When a request has made to service, it creates a new instance of service for each method call and this will be disposed of once the response goes to the client. This whole process is known as per call instance mode.
WCF Services client configuration file contains endpoint, address, binding and contract. A sample client config file looks like <system.serviceModel> <client> <endpoint name = “MyEndpoint” address = “http://localhost:8000/MyService/” binding = “wsHttpBinding” contract = “IMyContract” /> </client> </system.serviceModel>
The main difference between WCF and ASMX web service is that ASMX is designed to send and receive messages using SOAP over HTTP only while WCF facilitates you to send and receive messages using any format over any transport protocol. ASMX web services can be hosted only in IIS while WCF service has all the following hosting options:
In WCF, Proxies are used to communicate between client and server. The communication takes place by exchanging the messages in the form of requests and responses. It will have the details like Service Path, Protocol details and so on
The Service Contract is a collective mechanism where the capabilities and requirements of service are specified for its consumers. It will explain the list of operations that will be performed by the service when it is executed. It will define the data types, protocols, and operation locations for the service. Defining a service contract for WCF is not easy but developers should have the right skills and experience to complete this task.
Following are two ways to enable the metadata for WCF: For Default Endpoint: <serviceMetadata> tag is used in web.config file without specifying the endpoint. For Custom Endpoint: <serviceMetadata> tag is used in web.config file with specifying the defined endpoint.
Service host factory is the mechanism by which we can create the instances of service host dynamically as the request comes in. This is useful when we need to implement the event handlers for opening and closing the service. WCF provides ServiceFactory class for this purpose
Address format of WCF transport schema always follow [transport]://[machine or domain][:optional port] format. for example: HTTP Address Format http://localhost:8888 The way to read the above url is “Using HTTP, go to the machine called localhost, where on port 8888 someone is waiting” When the port number is not specified, the default port is 80. TCP Address Format net.tcp://localhost:8888/MyService When a port number is not specified, the default port is 808: net.tcp://localhost/MyService NOTE: Two HTTP and TCP addresses from the same host can share a port, even on the same machine. IPC Address Format net.pipe://localhost/MyPipe We can only open a named pipe once per machine, and therefore it is not possible for two named pipe addresses to share a pipe name on the same machine. MSMQ Address Format net.msmq://localhost/private/MyService net.msmq://localhost/MyService
The timeout property can be set for the WCF Service client call using binding tag. <client> <endpoint … binding = “wsHttpBinding” bindingConfiguration = “LongTimeout” … /> </client> <bindings> <wsHttpBinding> <binding name = “LongTimeout” sendTimeout = “00:04:00″/> </wsHttpBinding> </bindings> If no timeout has been specified, the default is considered as 1 minute.
IN WCF, .svc file is a text file. This file is similar to our .asmx file in web services. This file contains the details required for WCF service to run it successfully. This file contains following details: Language (C# / VB) Name of the service Where the service code resides Example of .svc file <%@ ServiceHost Language=”C#/VB” Debug=”true/false” CodeBehind=”Service code files path” Service=”ServiceName”
WCF WCF is excellent for building real-time applications because it’s more reliable, fast and robust as compared to the web services. Web Services
The code name of WCF was Indigo .
WCF service supports 2 styles of models: RPC style: In RPC style we can use the serialize types and it provides the feature that is available for local calls. Message style: In message style WCF allows the message header to be customized and it also allows us to define the security for body and header messages.
In WCF, “Throttling” is used to limit the sessions or instances to be created at an application level. It is used to boost the performance.
Read Uncommitted: Also known as dirty isolation level. It makes sure that corrupt Data cannot be read. This is the lowest isolation level. Read Committed: It ensures not to read the data that has been changed by any other application and is not yet committed. It is the default level. Repeatable Read: It stops the usage of dirt read and non-repeatable read. It states that data fetched through a query will be locked and will not be updated by any other transaction. Serializable: It does not allow any modification and addition of new data till the transaction is completed. This is considered to be a very restrictive level. Snapshot: It raises an error on modifying a data that has already been changed by any transaction.
Data Contracts, is used to define Required or NonRequired data members. It can be done with a property named IsRequired on DataMember attribute. [DataContract] public class test { [DataMember(IsRequired=true)] public string NameIsMust; [DataMember(IsRequired=false)] public string Phone; }
In WCF one-way mode, a client sends a request to the server but does not wait till the response comes. It does not care if the request is failed or succeeded. The Client will not be blocked in this case until it receives the response.
What is WCF?
What are the main features of WCF?
What are advantages of WCF?
Why WCF?
What are the WCF fundamentals and WCF Architecture?
Can you explain binding and how many types of bindings are there in WCF?
Can you explain different types of WCF Tools?
What is contract in WCF?
What are the different types of Contracts used in WCF?
Can you explain the essential components used in WCF?
What is the WCF RIA?
Can you explain the possible ways of hosting a WCF service?
What is a SOA?
What is ABC in WCF?
Can you explain transactions in WCF?
Which are the three types of transactions manager WCF supports?
Can you explain Transport in WCF?
Can you define address header in WCF?
Can you explain Message Reliability in WCF?
Can you explain XML Infoset?
Can you explain duplex contracts in WCF?
What are various ways of hosting WCF services?
What is Per Call instance mode in WCF?
What are different elements of WCF services client configuration file?
What is the difference between ASMX web services and WCF?
What is service proxy in WCF?
How will you define the service contract in WCF?
How would you enable the metadata information of the service?
Can you explain service host factory in WCF?
What is the address formats of the WCF transport schemas?
How to set the timeout property for the WCF service client call?
Can you explain .svc File in WCF?
What are the differences between WCF and Web services?
What was the codename for WCF?
Which styles of models are supported in WCF?
Can you define Throttling in WCF?
What are the different isolation levels in WCF?
What is the purpose of required property in data contracts?
Can you explain one-way mode in WCF?