WCF Interview Questions and Answers
by Nithyanandham, on Sep 10, 2022 4:37:10 PM
Q1. What is WCF? Briefly explain.
Ans: Windows Communication Foundation is a Software development kit (SDK) to develop and deploy services in Windows. WCF provides a runtime environment for services. It enables Common Language Runtime (CLR) kinds of services to be exposed, while consuming other services as CLR types. WCF is a part of the .NET 3.0 framework and it requires .NET 2.0, and it can run only on systems that support it.
Q2. What are the main components of WCF?
Ans: We need to define three main components in WCF:-
- Service class.
- Hosting environment
- End point
Q3. Explain how does WCF works?
Ans: WCF follows the “Software as a Service” model, where all units of functionality are defined as services. For communication, each point is a portal or connection either with the client or other services. It is a program that exposes a collection of endpoints.
Q4. What are the protocols used in Message Layer in WCF ?
Ans: There are some functionality of protocols which are used in messaging layer in WCF.
Ws-Security Channel:- It enables .to secure the message by implementing the ws-security specification in the messaging layer of a service.
Ws-Reliable Messaging Channel:- This protocols gives guarantee to deliver the message over channels.
Encodes:- It provides the number of encodes for message.
HTTP Channel:- It specifies that HTTP is used for delivering a message.
TCP Channel:- It specifies that TCP is used for delivering the message.
Transaction flow Channel:- It define the pattern in which a message is transacted.
Named Pipe Channel:- It enables inter-process communication (IPC).
MSMQ Channel:- It enables services to inter-operate the MSMQ Applications.
Q5. What are different ways in which WCF can be hosted ?
Ans: The different hosting techniques are
1.IIS hosting--This is the most commonly used hosting technique.Here IIS is
used as a server and has following
advantages -> starts automatically on the first client request,process recyling.
Disadvantage-- It supports only http protocol
2.Self hosting-This is hosting the WCF urself either in console/windows
application also in windows service .
The host process should be running before a client makes a call to servc.
Its easier to debug and deploy.
Lifetime of services can be controlled using Open and Close methods.
WAS hosting(Windows Activation Server hosting)--WAS was introduced
with windows vista and it is shipped with IIS 7.0. it is more powerful than
3.IIS 6.0 as it can support http,tcp and named pipes whereas IIS 6.0 can
support only http.
4.Windows Service Hosting--Here the service can be programmed to start
when the system starts.
Q6. What is the difference WCF and Web services?
Ans: Web services can only be invoked by HTTP. While Service or a WCF component can be invoked by any protocol and any transport type. Second web services are not flexible. However, Services are flexible. If you make a new version of the service then you need to just expose a new end. Therefore, services are agile and which is a very practical approach looking at the current business trends.
Q7. What is the Message Layer in WCF ?
Ans: The message layer specifies the pattern of exchanging data between channels that are used during service communication.
There are two types of channels used in WCF.
Transport layer:- HTTP,Named pipes,TCP and MSMQ are used in transport layer.
Protocol :- WS-Security and WS-Reliability are protocols that used in WCF Applications.
Q8. How transaction works in WCF?
Ans: When we say Transaction, it means more than one party will be involved in order to complete one single logical operation. In case of SOA party means service.
Normally WCF transaction follow two faces. In Phase 1 Transaction manager checks whether all services have completed their work and ready to commit. This phase is termed as prepare phase. If it’s done then in Phase 2 actual commit happens which is termed as Commit Phase.
Q9. What is message Contract in WCF ?
Ans: A Message contract provides control over the simple object access protocol (SOAP).Message that are produced and consumed by WCF. It helps to direct access to the SOAP header and bodies to change the SOAP Message as for your requirement.The most important feature of SOAP Message is interoperability.
Q10. What is .svc file?
Ans: Its the file where service is defined and it will be the point of contact from the consumers.It contains name of service and code behind file name.It is used to know about the service.
Q11. What are different bindings supported by WCF?
Ans: WCF includes predefined bindings. They cover most of bindings widely needed in day-to-day application. However, just incase you find that you need to define something custom WCF does not stop you. So let us try to understand what each binding provides.
BasicHttpBinding: - This binding is used when we need to use SOAP over HTTP. This binding can also be configured to be used as HTTPS. It can be also configured to send data in plain text or in optimized form like MTOM.
WsHttpBinding: - It is same like BasicHttpBinding. In short, it uses SOAP over HTTP. But with it also supports reliable message transfer, security and transaction. WS-Reliable Messaging, security with WS-Security, and transactions with WS-Atomic Transaction supports reliable message.
NetTcpBinding: - This binding sends binary-encoded SOAP, including support for reliable message transfer, security, and transactions, directly over TCP. The biggest disadvantage of NetTcpBinding is that both server and client should be also made in .NET language.
NetMsmqBinding: - This binding sends binary-encoded SOAP over MSMQ. This binding can only be used for WCF-to-WCF communication.
NetNamedPipesBinding:-Ths binding Sends binary-encoded SOAP over named pipes. This binding is only usable for WCF-to-WCF communication between processes on the same Windows-based machine.
Note: - An interprocess control (IPC) protocol is used for exchanging information between two applications, possibly running on different computers in a network. The difference between Named pipes and TCP is that named pipes have good performance in terms of communication with in processes. But when it comes to communicate across network TCP holds the best choice. So if you are using WCF to communicate with process it’s the best choice to use in terms for performance. Named pipes do not perform when the traffic is heavy as compared to TCPIP.
Q12. Where the transaction manager does resides, in service side or client side?
Ans: At client side
Q13. What is Service Contract in WCF ?
Ans: A service contract is a contract that defines the operations or methods available at end points .It also defines the message exchange pattern such as behavior of the message in one way,duplex or request/reply.
Q14. What do you mean by ABC in WCF ?
Ans: ABC in WCF means -
Address -> A stands for Address. It indicates where you service is located.A URL is used to point to the location.Depending on whethere the service is hosted in http,tcp... the address varies. eg-
http://localhost/Test
net.tcp//localhost/Test
Binding ->B stands for Binding which specifies how the client should communicate with the service.
Contract->C stands for contracts. It exposes the operation provided by the Service. It is a contract.
Q15. What is SOAP in WCF ?
Ans: SOAP stands for Simple Object Access Protocol. A Message contract provides control over the simple object access protocol (SOAP).The most important feature of SOAP Message is interoperability.
Q16. What are the various ways of hosting a WCF service?
Ans: There are three major ways to host a WCF service:-
- 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.
Q17. What is Activation and hosting Layer in WCF?
Ans: The activation and hosting layer renders various options for the services in which they can be activated as well as hosted. we can host asp.net application services in two ways.
- Self hosting (ex. window application,window service,console application.)
- External server hosting (ex. IIS ,WAS)
Q18. How it is possible?
Ans: Because both will be using WS-Automic protocol for managing services.
Q19. What is an endpoint in WCF ?
Ans: ABC together constitute an endpoint.Endpoint provides the client access to the functionality exposed by the service.
Q20. What is MEPs in WCF ?
Ans: MEPs stands for Message Exchange Patterns. There are three types of Message Exchange Patterns as given below:-
- Request and Response
- Data Gram
- Duplex (half and full)
Q21. What do you mean by contarcts. What are the different types of contracts ?
Ans: Contracts are standard way of describing the operations performed by a Service.Diff types of contracts are--
1.Service Contracts
It describes the operations provided by the service.WCF service
should have atleast one ServiceContract.it has two attribute-
[ServiceContract] --Used to define the interface.Client can talk to services via interfaces.
Similar to [Webservice] attribute in Webservice
[OperationContract]-Used to define methods inside the interface.Similar to
[WebMethods]
eg-
[ServiceContract()]
public interface ISimpleCalculator
{
[OperationContract()]
int Add(int a, int b);
}
DataContract
This defines the data which is exchanged between the client and the service.
these are used manily for used defined objects.They are of two types
[DataContract]--defines the class
[DataMember] --defines the properties.
eg-
[DataContract]
class Name
{
public string Fullname;
[DataMember]
public string FirstName;
[DataMember]
public string SurName;
}
Inbuilt types like string (Fullname) are defined impicitly and they do not need
data contracts.
3.Fault Contracts.
These are used to handle errors in a service.When a service throws an error it
does not reach the client side. But by using fault contract we can come to know the
error that is raised by the service.
eg-
[ServiceContract()]
public interface ISimpleCalculator
{
[OperationContract()]
[FaultContract(typeof(ArgumentException))]
int Add(int a, int b);
}
Raise the exception as
throw new FaultException("error") ;
4.Message Contracts
Message Contract defines the way messages are transferred using SOAP messages. It
is used if you want to customize the SOAP format.
eg-
[MessageContract]
public class CustomerDetails
{
[MessageHeader]
public string CustID;
[MessageBodyMember]
public string Name;
Q22. What is Policy binding in WCF ?
Ans: The Policy and Binding component of the contract layer specifies important information.Such as security and protocol of a WCF services. Binding is a process used to enable communication between services and clients.There are some protocols which are very necessary to communications with the end point of services.ExHTTP,TCP,MSMQ.
Q23. What is WCF Method Overloading
Ans: Hello folks, I have already explained regarding Transfer large file using WCF and The remote server returned an unexpected response: (413) Request Entity Too Large. Today I am going to explain another good concept of WCF Method Overloading.
As you all know method overloading is supported by .net Framework. So WCF also supports method overloading, but how could we achieve WCF method Overloading!!!
Q24. What all things we have to do in order to achieve WCF Transaction?
Ans: 1.All operation contracts should be decorated with TransactionFlow attribute
2.While implementing Operation Contract specify OperationBehavior attribute setting TransactionScopeRequired to true.
3.Enable transaction flow for binding in Web.Config
4. While invoking every service at client side use TransactionScope
Q25. What is Service RunTime Layer in WCF ?
Ans: The service runtime layer specifies and manage different behavior of a service that occur during its operation such as consuming or hosting the services.
Q26. What’s the difference between Allowed and Mandatory in TransactionFlow attribute?
Ans: TransactionFlowOption.Allowed means client can call the service in transaction if he/she wants.
TransactionFlowOption.Mandatory means client must call the service in transaction.
Q27. What are the behavior managed by Service RunTime Layer in WCF ?
Ans: Throttling behavior:- The number of processed messages varies according to demand of services.
Error Behavior:- It specifies the action to be taken, it only of the message give error during service runtime.
Instance behavior:- It specifies the number of service instance that are available to process a message.
Meta Data Behavior:- It specifies ,meta data is available or not across the network.
Message Inspection:- It inspects all the message during service runtime.
Transaction behavior:- It roll backs the transactions, if any process fails during service runtime.
Dispatch Behavior:- It determines ,how a message is handled and processed by the WCF infrastructure.
Concurrency behavior:- It specifies whether the message are processed sequentially or concurrently by the service.
Parameter filtering:- It filters the message header and executes preset actions.
Q28. What namespace is used to access the WCF ?
Ans: System.ServiceModel