Wednesday, May 7, 2014

WCF Interview Questions and Answers

WCF


WCF Interview Questions and Answers


Now a days mostly in every .NET project, WCF is used. So, every .NET developer must know at least basics of WCF. By keeping that in mind, I have tried to list down some basic WCF interview questions and answers which every .NET developer should know before going to the interview room. These WCF interview questions and answers cover basic concepts of WCF, SOA, difference between WCF and web services, need of WCF, endpoints in WCF like Address, Contracts and Bindings, types of Bindings in WCF, types of contracts in WCF, components of WCF, transport schemas in WCF, transactions in WCF, isolation levels in WCF, how to host WCF services, generating proxies for WCF services etc. So, lets have a look upon thes basic WCF interview questions and answers.
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)


WCF is part of .NET 3.0 and requires .NET 2.0, so it can only run on systems that support it. WCF is Microsoft’s unified programming model for building service-oriented applications with managed code. It extends the .NET Framework to enable developers to build secure and reliable transacted Web services that integrate across platforms and interoperate with existing investments.


Windows Communication Foundation combines and extends the capabilities of existing Microsoft distributed systems technologies, including Enterprise Services, System.Messaging, Microsoft .NET Remoting, ASMX, and WSE to deliver a unified development experience across multiple axes, including distance (cross-process, cross-machine, cross-subnet, cross-intranet, cross-Internet), topologies (farms, fire-walled, content-routed, dynamic), hosts (ASP.NET, EXE, Windows Presentation Foundation, Windows Forms, NT Service, COM+), protocols (TCP, HTTP, cross-process, custom), and security models (SAML, Kerberos, X509, username/password, custom).


What is SOA (Service Oriented Architecture)?


Service-oriented architecture (SOA) is an evolution of distributed computing based on the request/reply design paradigm for synchronous and asynchronous applications. An application’s business logic or individual functions are modularized and presented as services for consumer/client applications.


What is endpoint in WCF service?


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.

The Endpoint is the fusion of Address, Contract and Binding.


Explain Address,Binding and contract for a WCF Service?


Address:Address defines where the service resides.

Binding:Binding defines how to communicate with the service.

Contract:Contract defines what is done by the service.


What are the various address format in WCF?

a)HTTP Address Format:–> http://localhost:

b)TCP Address Format:–> net.tcp://localhost:

c)MSMQ Address Format:–> net.msmq://localhost:


What are the types of binding available in WCF?


A binding is identified by the transport it supports and the encoding it uses. Transport may be HTTP,TCP etc and encoding may be text,binary etc. The popular types of binding may be as below:


a)BasicHttpBinding – 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.


b)NetTcpBinding – 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.


c)WSHttpBinding – 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.


d)NetMsmqBinding – Offered by the NetMsmqBinding class, this uses MSMQ for transport and is designed to offer support for disconnected queued calls.


e)NetPeerTcpBinding – 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.


What was the code name 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+

How does WCF work?


Follows the ‘software as a service’ model, where all units of functionality are defined as services.

A WCF Service is a program that exposes a collection of Endpoints. Each Endpoint is a portal (connection) for communication with either clients (applications) or other services.


Enables greater design flexibility and extensibility of distributed systems architectures.

A WCF application is represented as a collection of services with multiple entry points for communications.


What are the types of contract available in WCF?


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

d)Message Contract Defines wheather a service can interact directly with messages


What are the various ways of hosting a WCF Service?


a)IIS

b)Self Hosting

c)WAS (Windows Activation Service)


What is Proxy and how to generate proxy for WCF Services?


The proxy is a CLR class that exposes a single CLR interface representing the service contract. The proxy provides the same operations as service’s contract, but also has additional methods for managing the proxy life cycle and the connection to the service. The proxy completely encapsulates every aspect of the service: its location, its implementation technology and runtime platform, and the communication transport.


Generating a Proxy class


a) The proxy can be generated using Visual Studio by right clicking Reference and clicking on Add Service Reference. This brings up the Add Service Reference dialog box, where you need to supply the base address of the service (or a base address and a MEX URI) and the namespace to contain the proxy.


b) Proxy can also be generated by using SvcUtil.exe command-line utility. We need to provide SvcUtil with the HTTP-GET address or the metadata exchange endpoint address and, optionally, with a proxy filename. The default proxy filename is output.cs but you can also use the /out switch to indicate a different name.


SvcUtil http://localhost/MyService/MyService.svc /out:Proxy.cs


When we are hosting in IIS and selecting a port other than port 80 (such as port 88), we must provide that port number as part of the base address:


SvcUtil http://localhost:88/MyService/MyService.svc /out:Proxy.cs

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


How can we create Proxy for the WCF Service?


We can create proxy using the tool svcutil.exe after creating the service.

We can use the following command at command line.

svcutil.exe *.wsdl *.xsd /language:C# /out:SampleProxy.cs /config:app.config


What is the difference between WCF Service and Web Service?


a)WCF Service supports both http and tcp protocol while webservice supports only http protocol.

b)WCF Service is more flexible than web service.


What is DataContract and ServiceContract?

Data represented by creating DataContract which expose the data which will be transefered /consumend from the service to its clients.

To write an operation on WCF,you have to write it as an interface,This interface contains the “Signature” of the methods tagged by ServiceContract attribute,and all methods signature will be impelemtned on this interface tagged with OperationContract attribute and to implement these serivce contract you have to create a class which implement the interface and the actual implementation will be on that class.


Code Below shows How to create a Service Contract:


Code:


[ServiceContract]

Public Interface IResource


[OperationContract]

Decimal GetTechnicalSkills(int ResourceID);



Class Resource: IResource


Decimal Get echnicalSkills ()


// Implementation of this method.


client in perspective of data communication?


A service is a unit of functionality exposed to the world.

The client of a service is merely the party consuming the service.


What are contracts in WCF?

In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does.


WCF defines four types of contracts.


Service Contract


There are two types of Service Contracts.

ServiceContract – This attribute is used to define the Interface.

OperationContract – This attribute is used to define the method inside Interface.



[ServiceContract]

interface IMyContract


[OperationContract]

string MyMethod( );


class MyService : IMyContract


public string MyMethod( )


return “Hello World”;



Data contracts


Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but we can easily define explicit opt-in data contracts for custom types.


There are two types of Data Contracts.

DataContract – attribute used to define the class

DataMember – attribute used to define the properties.



[DataContract]

class Contact


[DataMember]

public string FirstName;

[DataMember]

public string LastName;



If DataMember attributes are not specified for a properties in the class, that property can’t be passed to-from web service.


Fault contracts


Define which errors are raised by the service, and how the service handles and propagates errors to its clients.


Message contracts


Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format we have to comply with.


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.


What are different elements of WCF Srevices 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 Transport and Message Reliability?


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.


Message reliability deals with reliability at the message level independent of how many packets are required to deliver the message. Message reliability provides for end-to-end 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.


How to deal with operation overloading while exposing the WCF services?


By default overload operations (methods) are not supported in WSDL based operation. However by using Name property of OperationContract attribute, we can deal with operation overloading scenario.


[ServiceContract]

interface ICalculator


[OperationContract(Name = "AddInt")]

int Add(int arg1,int arg2);

[OperationContract(Name = "AddDouble")]

double Add(double arg1,double arg2);

What are the advantages of hosting WCF Services in IIS as compared to self-hosting?


Automatic activation

IIS provides automatic activation that means the service is not necessary to be running in advance. When any message is received by the service it then launches and fulfills the request. But in case of self hosting the service should always be running.


Process recycling

If IIS finds that a service is not healthy that means if it has memory leaks etc, IIS recycles the process. Ok let us try to understand what is recycling in IIS process. For every browser instance, a worker process is spawned and the request is serviced. When the browser disconnects the worker, process stops and you lose all information. IIS also restarts the worker process. By default, the worker process is recycled at around 120 minutes. So why does IIS recycle. By restarting the worker process it ensures any bad code or memory leak do not cause issue to the whole system.


Self-hosting gives you full control like when to start a service host, stop and so on. But, you need to recycle the service and have to programmatically handle all the required things to make your service to work in critical errors. Sometimes, it may lead to server crash.


What is one-way operation?


IsOneWay equal to true ensures that the client does not have to wait for the response. So methods marked by IsOneWay to true should always return void. In this, the caller does not get anything in return so it is called as one-way communication. Code snippet for this is given below.


namespace OneWayOperationSample



[Service Contract()]


public inteface IOneWay



[OperationContract(IsOneWay = true)]


void OneWayMeth();



public class SvcOneWay : IOneWay



Public void OneWayMeth()



//Your Code





What is poison message?


A message is sent to a receiver that is malformed or causes some kind of exception and cannot be processed by a receiver.

This problem is known as poison message and it occurs while the message is in the queue and causing problems, no other messages can get processed, so in essence the poison message clogs up the queue


What is dead letters?


A message has a time-to-live that expires before the message is processed. This problem is called as dead letters.

In WCf and MSMQ, dead letter processing is controlled by the client-side of the wcf conversation. The client oe sender, specifies the time-to-live for the message and also specifies where the dead letter message is sent to.



WCF Interview Questions and Answers

No comments:

Post a Comment