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.
What is WCF?
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.
What are the main features of WCF?
- Service orientation Applications
- Service Metadata
- Data Contracts
- Multiple Message Patterns
- Reliable and Queued Message
- Durable Messages
- Multiple transports and encoding
- AJAX RST Support
- Transactions
- Extensibility
- Interoperability
- Routing
- Web HTTP programming model
- Security
- WCF discovery
What are advantages of WCF?
- WCF provides better reliability and security compared to ASMX Web services.
- WCF is a best fit for scenarios like message queues, duplex communication, end-to-end message security, one way messaging, distributed transactions, etc
- It can maintain state
- It can control concurrency
- In WCF, there is no need to make much of a change to code to use the security model and alter the binding.
- Small changes in the configuration file will match your requirements.
- WCF provides interoperability between services.
- WCF can maintain transaction like COM+ Does
- It can be hosted on IIS, WAS, Self hosting, Windows services.
Why WCF?
- A web service to exchange messages in XML format using HTTP protocol for interoperability
- A remoting service to exchange messages in binary format using TCP protocol for performance
- A secure service to process business transactions
- A service that supplies current data to others, such as a traffic report or other monitoring service
- A chat service that allows two people to communicate or exchange data in real time
- A dashboard application that polls one or more services for data and presents it in a logical presentation.
- A Silverlight application to poll a service for the latest data feeds.
What are the WCF fundamentals and WCF Architecture?
WCF fundamentals:
Interoperability
Service Orientation
Unification
- COM+ Services
- Web Services
- .NET Remoting
- Microsoft Message Queuing
WCF Architecture:
- Contracts
- Service runtime
- Messaging
- Activation and Hosting
Can you explain binding and how many types of bindings are there in WCF?
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.
Can you explain different types of WCF Tools?
- ServiceModel Metadata Utility Tool (Svcutil.exe)
- Find Private Key Tool (FindPrivateKey.exe
- ServiceModel Registration Tool (ServiceModelReg.exe)
- COM+ Service Model Configuration Tool (ComSvcConfig.exe)
- Configuration Editor Tool (SvcConfigEditor.exe)
- Service Trace Viewer Tool (SvcTraceViewer.exe)
- WS-AtomicTransaction Configuration Utility (wsatConfig.exe)
- WS-AtomicTransaction Configuration MMC Snap-in
- WorkFlow Service Registration Tool (WFServicesReg.exe)
- WCF Service Host (WcfSvcHost.exe)
- WCF Test Client (WcfTestClient.exe)
- Contract-First Tool
What is contract in WCF?
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.
What are the different types of Contracts used in WCF?
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.
Can you explain the essential components used in WCF?
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.
What is the WCF RIA?
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.
Can you explain the possible ways of hosting a WCF service?
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:
- Self-Hosting
- Windows Service
- Internet Information Services (IIS)
- Windows Activation Services (WAS)
What is a SOA?
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.
What is ABC in WCF?
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
Can you explain transactions in WCF?
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.
Which are the three types of transactions manager WCF supports?
WCF supports following three types of transactions managers:
- LightWeight
- OLE Transactions
- WSAtomic Transactions
Can you explain Transport in WCF?
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.
Can you define address header in WCF?
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.
Can you explain Message Reliability in WCF?
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.
Can you explain XML Infoset?
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.”
Can you explain duplex contracts in WCF?
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.
What are various ways of hosting WCF services?
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.
What is Per Call instance mode in WCF?
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.
What are different elements of WCF services client configuration file?
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>
What is the difference between ASMX web services and WCF?
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:
- IIS
- WAS (Windows Process Activation Services)
- Console Application
- Windows NT Services
- WCF provided Host
- ASMX web services support is limited to HTTP while WCF supports HTTP, TCP, MSMQ, NamedPipes.
- ASMX Security is limited. Normally authentication and authorization are done using IIS and ASP.NET security configuration and transport layer security. For message layer security, WSE can be used.
- WCF provides a consistent security programming model for any protocol and it supports many of the same capabilities as IIS and WS-* security protocols, additionally, it provides support for claims-based authorization that provides finer-grained control over resources than role-based security. WCF security is consistent regardless of the host that is used to implement WCF service.
- Another major difference is that ASMX web services use XmlSerializer for serialization while WCF uses DataContractSerializer which is far better in performance than XmlSerializer.
What is service proxy in WCF?
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
How will you define the service contract in WCF?
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.
How would you enable the metadata information of the service?
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.
Can you explain service host factory in WCF?
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
What is the address formats of the WCF transport schemas?
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
How to set the timeout property for the WCF service client call?
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.
Can you explain .svc File in WCF?
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”
What are the differences between WCF and Web services?
WCF
WCF is excellent for building real-time applications because it’s more reliable, fast and robust as compared to the web services.
- WCF uses DataContratSerializer that shows which fields or properties are serialized into XML. That’s why WCF is preferred over web services, as DataContratSerializer performs better than XMLSerializer.
- WCF is flexible because its services can be hosted in different types of applications. The following lists several common scenarios for hosting WCF services:
- IIS
- WAS
- Selfhosting
- Managed Windows Service
Web Services
- Web services use XMLSerializer which does not specify which fields or properties of the type are serialized into XML,
- It can be accessed only over HTTP
- It works in stateless environment
- Web services perform far better than WCF services even when they both execute same actions.
What was the codename for WCF?
The code name of WCF was Indigo .
- WCF is a unification of .NET framework communication technologies which unites the following technologies:
- NET remoting
- MSMQ
- Web services
- COM+
Which styles of models are supported in WCF?
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.
Can you define Throttling in WCF?
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.
What are the different isolation levels in WCF?
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.
What is the purpose of required property in data contracts?
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;
}
Can you explain one-way mode in WCF?
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.