Wednesday, 8 March 2017

WCF interview question Part 3

1. Ques: What are the main components of WCF?
Answer:
1.Service: The working logic or offering, implemented using any .Net Language(C).
2.Host: The environment where the service is parked. E.g. exe, process, windows service
3.Endpoints: The way a service is exposed to outside world.


2. Ques: Explain transactions in WCF ? What are different isolation levels provided in WCF?
Answer:
Transactions in WCF allow several components to concurrently participate in an operation. Transactions are a group of operations that are atomic, consistent, isolated and durable. WCF has features that allow distributed transactions. Application config file can be used for setting transaction timeouts.

The different isolation levels:
  • READ UNCOMMITTED: – An uncommitted transaction can be read. This transaction can be rolled back later.
  • READ COMMITTED :- Will not read data of a transaction that has not been committed yet
  • REPEATABLE READ: – Locks placed on all data and another transaction cannot read.
  • SERIALIZABLE:- Does not allow other transactions to insert or update data until the transaction is complete.

3. Ques: What is a WCF Service ?
Answer:
A WCF service is a program that exposes a collection of Endpoints (connections) for communicating with either client applications or other service applications.
     

4. Ques: What are the advantages of hosting WCF Services in IIS as compared to self-hosting?
Answer:
There are two main advantage WCF Services in IIS as compared to self-hosting:
Automatic Activation
Process Recycling


5. Ques: What is diffrence between DataContractSerializer and XmlSerializer ?
Answer:
DataContractSerializer 
  • It uses best when we want to serialize some of the properties in object.
  • It uses Opt-In approach. It means we have to explicitly specify which properties need to be serialized
  • It used for serialization/deserialization of class in WCF service to and from either JSON or XML.
  • It can serialize not only properties but also fields.
  • It can also serialize nonpublic members
  • It can serialize properties with only Get Accessor
  • It is Faster as compare to XmlSerializer 
  • It can only work in WCF
XmlSerializer 
  • It uses best when we want to serialize most of the properties in object.
  • It used Opt-out approach. It means we have to explicitly specify which properties you don't want to serialize
  • It used XmlSerializer is only for XML serialization
  • It can only serialize public properties which must have both Get and Set Accessor.
  • Slower compare to DataContractSerializer
  • It can work for WCF and Webservices (asmx)

6. Ques:  Explain different modes of security in WCF?
Answer:
In Windows Communication Foundation, we can configure to use security at different levels

Transport Level security
Transport Level security means providing security at the transport layer itself. When dealing with security at Transport level, we are concerned about integrity, privacy and authentication of message as it travels along the physical wire. It depends on the binding being used that how WCF makes it secure because most of the bindings have built-in security.

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

Message Level Security
For Tranport level security, we actually ensure the transport that is being used should be secured but in 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>


7. Ques: How can you implement operation overloading in WCF service?
Answer:
We can implement operation overloading using “Name” property of OperationContract attribute. For eg:

[ServiceContract]
Interface ICalculator
{
    [OperationContract(Name = "AddInt")]
    int Add(int arg1,int arg2);
    [OperationContract(Name = "AddDouble")]
    double Add(double arg1,double arg2);
}


8. Ques: Explain DataContract in wcf ?
Answer:
A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged.
Data contract can be explicit or implicit. Simple type such as int, string etc has an implicit data contract. User defined object are explicit or Complex type, for which you have to define a Data contract using [DataContract] and [DataMember] attribute.

A data contract can be defined as follows:

  • It describes the external format of data passed to and from service operations
  • It defines the structure and types of data exchanged in service messages
  • It maps a CLR type to an XML Schema
  • It defines how data types are serialized and deserialized. Through serialization, you convert an object into a sequence of bytes that can be transmitted over a network. Through deserialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
  • It is a versioning system that allows you to manage changes to structured data


9. Ques:  What is Message Contract in WCF ?
Answer:
A message contract is used to control the structure of a message body and serialization process. It is used to send/access the information in the soap header. By use of a Message Contract we can customize the parameters sent using a SOAP message between the client and the server.

The SOAP header is implemented in the namespace system.web.services.protocol.

    [MessageContract]
    public class AutherRequest
    {
        public string AutherId;
    }

A message is nothing but a packet and WCF uses this packet to transfer the information from source to destination. This message is contained in the header or body.

Message Header
A Message Header is applied to a member of a message contract to declare the member within the message header.

    [MessageContract]
    public class AutherRequest
    {
        [MessageHeader]
        public string AutherId; 
    }

Message Body Member
A Message Body Member is applied to the member of a message contract to declare the members within the message body.
    [MessageContract]
    public class AuthorResponse
    {
        [MessageBodyMember]
        public Auther Obj; 
    }


10. Ques: What is Fault Contracts in WCF ?
Answer:
Fault Contracts are used to communicate error information from a service to client.They help us to define the errors to be expected from particular service,which we can catch and manage conditions depending on the type of fault.

Fault Contracts use as

Fault Contracts are useful when you need report the error to the client.
It will handle an error by the service class and display in the client side.
By default when we throw any exception from service, it will not reach the client side. WCF provides the option to handle and convey the error message to client from service using SOAP Fault contract.

No comments:

Post a Comment

Thank you for comment