WCF Interview Questions

Q1. What is WCF?

WCF stands for Windows Communication Foundation. It is a Software development kit for developing services on Windows. WCF is introduced in .NET 3.0. in the System.ServiceModel namespace. WCF is based on basic concepts of Service oriented architecture (SOA)

Q2. What is endpoint in WCF service?

The endpoint is an Interface which defines how a client will communicate with the service. It consists of three main points: Address,Binding and Contract.
Every service must have Address that defines where the service  resides, Contract that defines what  the service  does and a Binding that defines how to communicate with the service. In WCF the relationship between Address, Contract and Binding is called Endpoint.

Q3. Explain Address,Binding and contract for a WCF Service?
  1. Address: Address defines where the service resides.
  2. Binding: Binding defines how to communicate with the service.
  3. Contract: Contract defines what is done by the service.
Q4. Please explain different modes of security in WCF? Or Explain the difference between Transport and Message Level Security.
  • Transport Level security
  • Message Level Security 
For Tranport level security, we actually ensure the transport that is being used should be secured.

<netTcpBinding>
  <binding name="netTcpTransportBinding">
    <security mode=”Transport”>
      <Transport clientCredentialType=”Windows” />
    </security>
  </binding>
</netTcpBinding>

Message level security, we actually secure the message. We encrypt the message before transporting it.

<wsHttpBinding>
  <binding name="wsHttpMessageBinding">
    <security mode=”Message”>
      <Message clientCredentialType=”UserName” />
    </security>
  </binding>
</wsHttpBinding>

It totally depends upon the requirements but we can use a mixed security mode also as follows:

<basicHttpBinding>
  <binding name="basicHttp">
    <security mode=”TransportWithMessageCredential”>
      <Transport />
      <Message clientCredentialType=”UserName” />
    </security>
  </binding>
</basicHttpBinding>

Q5. What are the main components of WCF?
The main components of WCF are
  1. Service class
  2. Hosting environment
  3. End point
Q6. Where we can host WCF services?
Every WCF services must be hosted somewhere. There are three ways of hosting WCF services.
They are
  1.  IIS
  2.  Self Hosting
  3. WAS (Windows Activation Service)
Q7. What is 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). WCF supports nine types of bindings.( Basic binding, TCP binding, Peer network binding, IPC binding, Web Service (WS) binding, Federated WS binding, Duplex WS binding, MSMQ binding, MSMQ integration binding).

Q8. What are the types of contract available in WCF?
In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does.
The main contracts are:
a)Service Contract: Describes what operations the client can perform.
b)Operation Contract :  defines the method inside Interface of Service.
c)Data Contract: Defines what data types are passed to and from the service
d)Message Contract: Defines whether a service can interact directly  with messages
e)Fault Contract: Fault Contract provides documented view for error occurred in the service to client.
 to Read More about Contracts Follow the Link: Read about Contracts

Q9. What is the proxy for WCF Service?
A proxy is a class by which a service client can Interact with the service.
By the use of proxy in the client application we are able to call the different methods exposed by the service

Q10. How to test the WCF Service?
We can use the WCF Test client to test the WCF Service. Tool enables users to input test parameters, submit that input to the service, and view the response.
We can use the following command at command line. wcfTestClient.exe URI1 URI2 …

Q11.  What is IIS?
IIS Manager is a graphical interface for configuring your application pools or your Web, FTP, SMTP, or NNTP sites. With IIS Manager, you can configure IIS security, performance, and reliability features. You can add or delete sites; start, stop, and pause sites.

Q12.  Difference between API and WebService.
An API is an Application Programming Interface. An example of an API would be ADO.NET or Google APIs . An API encapsulates functionality into a library for you to access. It contains the operations, properties, etc. necessary to perform your actions.
A web service is a middle tier operation that you write on your own or consume someone Else's. A web service contains functionality that does some sort of operation. 

Q13.  What is SOAP?
Simple Object Access Protocol is a XML based protocol. It enables applications to communicate with each other.
  • SOAP stands for Simple Object Access Protocol
  • SOAP is an application communication protocol
  • SOAP is a format for sending and receiving messages
  • SOAP is platform independent
  • SOAP is based on XML
  • SOAP sits on top of HTTP
The main purpose of using SOAP is to enable communication between two independent applications using different technology, hosted on different environment.

Q14.  What is the difference between the SOAP and HTTP protocol. When we say "SOAP over HTTP", what does that mean.?
SOAP is messaging protocol concerned with packaging of messages in XML protocol and transfer then using HTTP . You can serve any content over HTTP such as HTML, images, sound, video, etc. SOAP is an XML-based encoding of messages that are typically sent over HTTP.

Q15. What is the protocol on which secure pages are generally requested?
HTTPS, "Hyper Text Transfer Protocol" with Secure Sockets Layer (SSL)

Q16. What is Secure Sockets Layer (SSL)?
The Secure Sockets Layer (SSL) ensures that the data sent over the Internet can’t be read by others. When a user requests a secure Web page, the server generates an encryption key for the user’s session and then encrypts the page’s data before sending a response. On the client side, the browser uses that same encryption key to decrypt the requested Web page and to encrypt new requests sent from that page.

Q17. Difference between WCF and Web service
Web service is a part of WCF.  WCF offers much more flexibility and portability to develop a service when comparing to web service. Still we are having more advantages over Web service, following table provides detailed difference between them.

Features
Web Service
WCF
Hosting
It can be hosted in IIS
It can be hosted in IIS, windows activation service, Self-hosting, Windows service
Programming
[WebService] attribute has to be added to the class
[ServiceContraact] attribute has to be added to the class
Model
[WebMethod] attribute represents the method exposed to client
[OperationContract] attribute represents the method exposed to client
Operation
One-way, Request- Response are the different operations supported in web service
One-Way, Request-Response, Duplex are different type of operations supported in WCF
XML
System.Xml.serialization name space is used for serialization
System.Runtime.Serialization namespace is used for serialization
Encoding
XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom
XML 1.0, MTOM, Binary, Custom
Transports
Can be accessed through HTTP, TCP, Custom
Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
Protocols
Security
Security, Reliable messaging, Transactions

Q18. Serialization is the process of converting an object instance to a portable and transferable format. So, whenever we are talking about web services, serialization is very important.

Serialization is the process of taking an object and converting it to a format in which it can be transported across a network or persisted to a storage location. The storage location could be as simple as using a file or a database. The advantage of serialization is the ability to transmit data across the network in a cross-platform-compatible format.

There are three formats of serialization
• Binary Serialization 
• SOAP Serialization 
• XML Serialization 

The format is controlled based upon what object is used to perform the serialization. The XML format is produced by using the System.Xml.Serialization.XmlSerializer class. The SOAP and binary formats are produced by using classes under the System.Runtime.Serialization.Formatters namespace.

Q19. What is WCF REST Service ?
If the WCF service is using REST/POX, then it is not necessary for the client to consume the WCF service proxy. For REST, WCF introduced a new binding i.e. WebHttpBinding. A client application that can do HTTP communication and can process XML, could now directly make a call to the WCF service and perform operations using XML. 

Q20.  How to define a Service as REST based service in WCF?
 WCF 3.5 provides explicit support for RESTful communication using a new binding named WebHttpBinding. 

The below code shows how to expose a RESTful service 
[ServiceContract]
interface IStock
{
   [OperationContract]
   [WebGet]
   int GetStock(string StockId);
}

By adding the WebGet Attribute, we can define a service as REST based service that can be accessible using HTTP GET operation.

Q21. 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.

Q22. What is the use of ServiceBehavior attribute in WCF ?

ServiceBehaviour attribute is used to specify the InstanceContextMode for the WCF Service class (This can be used to maintained a state of the service or a client too) 

There are three instance Context Mode in the WFC ::
PerSession : This is used to create a new instance for a service and the same instance is used for all method for a particular client. (eg: State can be maintained per session by declaring a variable) 
PerCall : This is used to create a new instance for every call from the client whether same client or different. (eg: No state can be maintained as every time a new instance of the service is created) 
Single : This is used to create only one instance of the service and the same instance is used for all the client request. (eg: Global state can be maintained but this will be applicable for all clients)

Q23. What is the Messaging Pattern? Which Messaging Pattern WCF supports?

Messaging Pattern : Messaging patterns describes how client and server should exchange the message. There is a protocol between client and server for sending and receiving the message. These are also called Message Exchange Pattern. 
WCF supports following 3 types of Message Exchange Patterns 
1. request - reply (default message exchange pattern) 
2. OneWay (Simplex / datagram) 
3. Duplex(CallBack) 

Q24. What is .svc file 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 : 
1. Language (C# / VB) 
2. Name of the service 
3. Where the service code resides 

Example of .svc file 
<%@ ServiceHost Language="C#/VB" Debug="true/false"
CodeBehind="Service code files path" Service="ServiceName" >

We can also write our service code inside but this is not the best practice.


Q25. What is DataContractSerializer in WCF?

DataContractSerializer is new serialization engine in WCF. It translates the .NET framework objects into XML and vice-versa. 
By default WCF uses DataContractSeriazer.

Q26. What is the purpose of base address in WCF service? How it is specified?

When multiple endpoints are associated with WCF service, base address (one primary address) is assigned to the service, and relative addresses are assigned to each endpoint. Base address is specified in <host> element for each service. 

<configuration>
    <system.servicemodel>
      <Services>
<service name=”MyService>
  <host>
  <baseAddresses>
 <add baseAddress =”http://localhost:6070/MyService”>
</baseAddresses>
</host>
</service>
<services>
</system.servicemodel>
</configuration>

Q27. Which protocol is used for platform-independent communication?

SOAP (Simple Object Access Protocol), which is directly supported from WCF (Windows Communication Foundation).

Q28. What are tha advantages of hosting WCF service in WAS?

WAS (Windows Activation Service) is a component of IIS 7.0. Following are few advantages : 
  1. We are not only limited to HTTP protocol. We can also use supported protocols like TCP, named pipes and MSMQ 
  2. No need to completely install IIS. We can only install WAS component and keep away the WebServer. 
Q29.  What is service host factory in WCF?

1. Service host factory is the mechanism by which we can create the instances of service host dynamically as the request comes in. 
2. This is useful when we need to implement the event handlers for opening and closing the service. 
3. WCF provides ServiceFactory class for this purpose.

Q30. What are Contracts in WCF?

A Contract is basically an agreement between the two parties i.e. Service and Client. In WCF, Contracts can be categorized as behavioral or structural.

Behavioral Contracts
define that what operations client can perform on a service.
Service Contract attribute is used to mark a type as Service contract that contains operations.
Operation Contract attributes is used to mark the operations that will be exposed.
Fault Contract defines what errors are raised by the service being exposed.

Structural Contracts
Data Contract  attribute define types that will be moved between the parties.
Message Contract attribute define the structure of SOAP message.

Q31. What is WSDL? 
WSDL stands for Web Service Description Language. WSDL is an XML based document that provides technical details about the web service. Some of the useful information in WSDL document are:

method name,
port types,
service end point,
binding,
method parameters etc.



1 comment:

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives