Sunday 1 January 2012

wcf interview questions and answers

wcf interview questions 
advanced /frequently asked  WCF Interview Questions


What is WCF?
Windows Communication Foundation  is a programming platform and runtime system for building, configuring and deploying network-distributed services.WCF is a combined features of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.WCF provides a runtime environment for services, enabling you to expose CLR types as services, and to consume other services as CLR types.

WCF Advantages? 
1 . WCF is interoperable with other services when compared to .Net Remoting,where the client and service have to be .Net.
2 . WCF services provide better reliability and security in compared to ASMX web services.
3 . In WCF, there is no need to make much change in code for implementing the security model and changing the binding. Small changes in the configuration will make your requirements.
4 . WCF has integrated logging mechanism, changing the configuration file settings will provide this functionality. In other technology developer has to write the code.
5 . It can be hosted in IIS,WAS,seft hosting,Managed windows service.
Difference between WCF and Web services?
1.Web Services can be accessed only over HTTP
2.Web Services works in stateless environment
WCF is flexible because its services can be hosted in different types of applications. The following lists several common scenarios for hosting WCF services: 
   1. IIS (Internet information services)
   2. WAS (Windows activation services)
   3. Self-hosting
   4. Managed Windows Service
3.One-way, Request- Response are the different operations supported in web service.but One-Way, Request-Response, Duplex are different type of operations supported in WCF.
4 .System.Xml.serialization name space is used for serialization in webservice.System.Runtime.Serialization namespace is used for serialization in wcf.

WCF Fundamental ?
End Point
Bindings and Behavior
Contracts and Service host
Message and Channel
WCF client and Metadata



What is End Point in WCF ? (What is ABC in WCF)
End point is aportal for the service will communicate with the world.End poins have three components.
Address
This will be the URL where the wcf has hosted.Using this url clint will comunicate to the service.
Binding
How the clint will comunicate with services.
Contract
Collection of operation that specifies what the endpoint will communicate with outside world. Usually name of the Interface will be mentioned in the Contract, so the client application will be aware of the operations which are exposed to the client. 
Example
<system.serviceModel>
<services>
      <service name="MathService"
        behaviorConfiguration="MathServiceBehavior">
       <endpoint
         address="http://localhost:8090/MyService/MathService.svc" contract="IMathService"
          binding="wsHttpBinding"/> 
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MathServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Types of Binding in wcf?


BasicHttpBinding      
            Basic Web service communication. No security by default
WSHttpBinding    
          Web services with WS-* support. Supports transactions
WSDualHttpBinding 
         Web services with duplex contract and transaction support
WSFederationHttpBinding
 Web services with federated security. Supports transactions

MsmqIntegrationBinding
Communication directly with MSMQ applications. Supports transactions



NetMsmqBinding
Communication between WCF applications by using queuing. Supports transactions


NetNamedPipeBinding
Communication between WCF applications on same computer. Supports duplex contracts and transactions
NetPeerTcpBinding
Communication between computers across peer-to-peer services. Supports duplex contracts
NetTcpBinding



Types of Contract in WCF ?


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




What are the various ways of hosting a WCF service?
Activation and Hosting

- Services can be hosted or executed, so that it will be available to everyone accessing from the client. WCF service can be hosted by following mechanism
IIS
Internet information Service provides number of advantages if a Service uses Http as protocol. It does not require Host code to activate the service, it automatically activates service code.
Windows Activation Service
(WAS) is the new process activation mechanism that ships with IIS 7.0. In addition to HTTP based communication, WCF can also use WAS to provide message-based activation over other protocols, such as TCP and named pipes.
Self-Hosting
WCF service can be self hosted as console application, Win Forms or WPF application with graphical UI.
Windows Service
WCF can also be hosted as a Windows Service, so that it is under control of the Service Control Manager (SCM).
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+

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
What is Proxy and how to generate proxy for WCF Services?
WCF Services client configuration file contains endpoint, address, binding and contract. A sample client config file looks like
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.
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.
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
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.

What is duplex contracts in WCF? 
WCF Services can communicate with client through a callback - is called duplex messaging pattern. Duplex messaging in WCF can be done over different transports, like TCP, Named Pipes and even HTTP.


What is REST in WCF ?
Rest concept and overview you can read from here. 


What is InstanceContextMode in WCF? 
This property value indicate when new service objects are created ? the property has three value
the default is PerSession
1. PerCall:
When WCF service is configured for Per-Call instance mode, Service instance will be created for each client request. This Service instance will be disposed after response is sent back to client.

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
    public class MyService:IMyService
    {
        static int m_Counter = 0;

        public int MyMethod()
        {
            m_Counter++;
            return m_Counter;
        }       
    }

2. PerSession:
When WCF service is configured for Per-Session instance mode, logical session between client and service will be maintained. When the client creates new proxy to particular service instance, a dedicated service instance will be provided to the client. It is independent of all other instance.

 [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
    public class MyService:IMyService
    {
        static int m_Counter = 0;

        public int MyMethod()
        {
            m_Counter++;
            return m_Counter;
        }       
    }

3. Single:
When WCF service is configured for Singleton instance mode, all clients are independently connected to the same single instance. This singleton instance will be created when service is hosted and, it is disposed when host shuts down.

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class MyService:IMyService
    {
        static int m_Counter = 0;

        public int MyMethod()
        {
            m_Counter++;
            return m_Counter;
        }       
    }


What is ConcurrencyMode in WCF? 
This property value indicate how service supports thread? the property has three value
the default is PerSession
1. Single:
2. Multiple :
3. Reentrant :

Can we apply more than one fault contract to one method in WCF?
Yes we can apply more than one faultContract attribute to a method in WCF
Like we apply more than one catch block for one try.



Does falult contract in WCF is Inherited ?
No falult contract in WCF is not Inherited



Can we apply a fault Contract to an Interface or class in WCF?
No,we Can not apply a fault Contract to an Interface or class in WCF .The fault contract can only be applied to a method only.



What is difference between Communication exception and Fault exception in WCF?
Communication exception is the common base type for several eception types in WCF ,all of them derive from Communication exception.


Fault Exception is one of the Exception types in WCF that derive from 
Communication exception









1 comment: