Wednesday, May 24, 2017

WCF interview questions

1. Explain the WCF architechture.

WCF architecture consists
2. Advantages of WCF

  1. Service Oriented
  2. Location Independent
  3. Language Independent
  4. Platform Independent
  5. Support Multiple operation
  6. WCF can maintain transaction like COM+ Does
  7. It can maintain state
  8. It can control concurrency
  9. It can be hosted on IIS, WAS, Self hosting, Windows services.
It has AJAX Integration and JSON (JavaScript object notation) support.
  • WCF can be configured to work independently of SOAP and use RSS instead.
  • To improve communication, transmission speed needs to be optimized. This is done by transmitting binary-coded XML data instead of plain text to decrease latency.
  • Object life-cycle management and distributed transaction management are applicable to any application developed using WCF.

What is the difference between WCF and Web services?

Explain transactions in WCF?

A transaction is a logical unit of work consisting of multiple activities that must either succeed or fail together.
The following are the types of the Transactions:
  1. Atomic
  2. Long Running

Phases of WCF Transaction:

  • Prepare Phase - In this phase, the transaction manager checks whether all the entities are ready to commit for the transaction or not.
  • Commit Phase - In this phase, the commitment of entities get started in reality.

What is one-way operation?

When an operation has no return value, and the client does not care about the success or failure of the invocation. WCF offers one-way operations to support this sort of fire-and-forget invocation,: once the client issues the call, WCF generates a request message, but no correlated reply message will ever return to the client. 
  1. [OperationContract(IsOneWay = true)]  
  2.     void Add(double n1, double n2);  

Explain what is DataContractSerializer?

WCF provides a message-oriented programming framework. Internally WCF represents all the messages by a Message class. When WCF transmits a message it takes a logical message object and encodes it into a sequence of bytes. After that WCF reads those bytes and decodes them in a logical object. The process forms a sequence of bytes into a logical object; this is called an encoding process. At runtime when WCF receives the logical message, it transforms them back into corresponding .Net objects. This process is called serialization. 

WCF supports three basic serializers:
  • XMLSerializer
  • NetDataContractSerializer
  • DataContractSerializer
WCF deserializes WCF messages into .Net objects and serializes .Net objects into WCF messages. WCF provides DataContractSerializer by default with a servicecontract. We can change this default serializer to a custom serializer like XMLSerializer.

NetDataContractSerializer is analogous to .Net Remoting Formatters. It implements IFormatter and it is compatible with [Serializable] types. It is not recommended for service oriented design.

What is Transaction Propagation? And how WCF support it?

Transaction propagation is the ability to propagate a transaction across the boundaries of a single service. Or in other words, we can say that a service can participate in a transaction that is initiated by a client. 

To enable transaction propagation, we need to set the value of the TransactionFlow property of the binding being used. This can be done programmatically as follows: 
  1. WSHttpBinding bindingBeingUsed = new WSHttpBinding();  
  2. bindingBeingUsed.TransactionFlow = "true";  
Or it can be done decoratively by updating the configuration file as follows:
  3.         "binding1" transactionFlow="true" />   


  •   The default value for the TransactionFlow property is "False".

    Do all WCF bindings support Transaction Propagation?

    No. Not all WCF bindings support transaction propagation. Only the following list of bindings support it:
    • wsHttpBinding
    • netTcpBinding
    • netNamedPipeBinding
    • wsDualHttpBinding
    • wsFederationHttpBinding

    What is WCF throttling?

    WCF throttling provides some properties that you can use to limit how many instances or sessions are created at the application level. Performance of the WCF service can be improved by creating proper instance.

    WCF throttling provides the prosperities maxConcurrentCalls, maxConcurrentInstances, and maxConcurrentSessions, that can help us to limit the number of instances or sessions are created at the application level.

    maxConcurrentCallsThis specifies the maximum number of messages processed across the service host. The default value for this property is 16 (WCF 4.0 is improved to default is 16 * Processor Count).
    maxConcurrentInstancesThis specifies the maximum number of instances of a context object that executes at one time with the service. The default is Int32.MaxValue.
    maxConcurrentSessionsThis specifies the maximum number of sessions at one time within the service host object. The default value is 10 (WCF 4.0 increases that to 100 * Processor Count).

    What is WCF Concurrency and How many modes are of Concurrency in WCF?

    WCF Concurrency means “Several computations are executing simultaneously.

    WCF concurrency helps us configure how WCF service instances can serve multiple requests at the same time.
    We can use the concurrency feature in the following three ways:
    • Single
    • Multiple
    • Reentrant
    Single: A single request will be processed by a single thread on a server at any point of time. The client proxy sends the request to the server, it process the request and takes another request.


    Multiple: Multiple requests will be processed by multiple threads on the server at any point of time. The client proxy sends multiple requests to the server. Requests are processed by the server by spawning multiple threads on the server object. 

    Reentrant: The reentrant concurrency mode is nearly like the single concurrency mode. It is a single-threaded service instance that receives requests from the client proxy and it unlocks the thread only after the reentrant service object calls the other service or can also call a WCF client through call back.

    What is Instance Context Mode in WCF?
    Answer: An Instance Context mode defines how long a service instance remains on the server.

    Whenever the client sends a request to a WCF service, what exactly happens behind the scenes? Basically after making a client request the WCF service will create a service class instance at the service that will do the operations involved and then it will return the response back to the client. In this request and response process the service instance object has been created in the process.

    The WCF framework has defined the following three Instance Context modes:

    PerCall: A new instance of the service will be created for the request from the same client or a different client, meaning every request is a new request. In this mode no state is maintained.In per-call service, every client request achieves a new dedicated service instance and its memory consumption is less as compared to other types of instance activation.

    1. [ServiceContract]  
    2. interfaceIMyContract  
    3. {...}  
    4. [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]  
    5. classMyService:IMyContract  
    6. {...}  
    Per-Session Service: A new Instance will be created for every new client session and the scope of that object will be the scope of that session.
    1. [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]  
    2. classMyService:IMyContract  
    3. {...}  
    Singleton Service: A single instance will be created for the service object that will take care of all the requests coming from the same client or a different one.

    By decorating the service with a service behavior, an Instance Context mode can be set.


    What is Security Implementation in WCF? How many are there?
    WCF supports following securities:
    • Message
    • Transport
    • TransportWithMessageCredential

    Message Security
    Message security uses the WS-Security specification to secure messages. The message is encrypted using the certificate and can now safely travel over any port using plain http. It provides end-to-end security.

    Transport Security
    Transport security is a protocol implemented security so it works only point to point. As security is dependent on protocol, it has limited security support and is bounded to the protocol security limitations.

    This we can call a mixture of both Message and Transport security implementation.

    No comments: