Friday, 4 December 2015

WCFQS

 Explain what is WCF?
WCF (Windows Communication Framework) is Microsoft framework to make inter-process communication easier.  Through various means, it lets you do the communication like MS messaging Queuing, Services, Remoting and so on.  It also allows you talk with other.NET apps, or non-Microsoft technologies (like J2EE).
2)      Mention what are the main components of WCF?
Main components of WCF are
  • Service:  The working logic
  • Host: The path where the data is saved. E.g., .exe, process, windows service
  • Endpoints: The way the service is exposed to the outside world
3)      Explain how does WCF works?
WCF follows the “Software as a Service” model, where all units of functionality are defined as services.  For communication, each point is a portal or connection either with the client or other services. It is a program that exposes a collection of endpoints.
4)      Explain what is the difference between ASMX web services and WCF?
The difference between WCF and ASMX or ASP.net web service is that ASMX is designed to send and receive messages using SOAP over HTTP only. While the WCF can exchange messages using any format over any transport protocol
5)      Mention what is the endpoint in WCF and what are the three major points in WCF?
Every service must have an address that determines where the service is located, contract that defines what the service does and binding that tells how to communicate with the service.
  • Address: It specifies the location of the service which will be like http://Myserver/Myservice.  To communicate with our service client it will use this location
  • Contract: It specifies the interface between the server and client. It’s a simple interface with some attribute
  • Binding: It decides how two parties will communicate with each other in terms of transport and encoding and protocols
6)      Explain how many types of contract does WCF defines?
WCF defines four types of Contracts
  • Service Contracts
  • Data Contracts
  • Fault Contracts
  • Message Contracts
7)      What are the transport schemas does WCF supports?
It supports
  • HTTP
  • TCP
  • Peer network
  • IPC ( Inter Process Communication)
  • MSMQ
8)      Mention what are the ways of hosting a WCF service?
The ways of hosting a WCF service are
  • IIS
  • Self-Hosting
  • WAS (Windows Activation Service)
9)      Mention the address syntax and the different formats of WCF transport scheme?
Address syntax of WCF transport scheme is
[transport]:// [machine or domain] [: optional port] format
10)   In WCF what are duplex contracts?
Duplex messaging or call-back is used in WCF to communicate with the client. Over different transport system Duplex messaging in WCF is done like TCP, Named pipe and even HTTP.  Collectively this is known as duplex contracts in WCF.
11)   Mention what are the different instance modes in WCF?
To a particular service instance WCF binds an incoming message request, so the available modes are
  • Per Call: This instance is created for each call, efficient in terms of memory but need to maintain session
  • Per Session: For a complete session of a user instance are created
  • Single: One instance is created which is shared among all the users and shared among all.  In terms of memory it is least efficient.
12)   Explain what is a Service Proxy in windows Communication Foundation?
In WCF, a service proxy enables applications to interact with WCF service by sending and receiving messages.  It’s a class that covers service details like service path, service implementation technology, platform and communication protocol and so on.  So, when the application interact the service through proxy, it gives the impression that it’s communicating a local object.
13)   Explain what is SOA?
SOA (Service Oriented Architectural) is a collection of services that determines how two computing entities will communicate with each other to achieve certain business functionality and also how one entity can work on behalf of another entity.
14)   What are the types of Data Contracts in WCF?
There are two types of Data Contracts
  • Data Contract: Attribute used to define the class
  • Data Member: Attribute used to define the properties
15)   What are the three types of transaction manager WCF supports?
The types of the transaction manager that WCF supports are
  • Light Weight
  • WS- Atomic Transaction
  • OLE Transaction
16)   List out the difference between XMLSerializer and the DataContractSerializer?
                 DataContractSerializer                            XMLSerializer
–          For WCF, DataContractSerializer is the default serializer–          XMLSerializer is not a default serializer
–          Compare to XMLSerializer it is faster–          XMLSerializer is slower
–          It is used for simple schemes–          It is used for complex schemes
17)   Explain what are the MEPs available in WCF?
MEP stand for Message Exchange Pattern, three types of message exchanged patterns are allowed.
  • Data Gram
  • Request and Response
  • Duplex
18)    Name the namespace that is used to access WCF service?
System.ServiceModel is used to access WCF service
19)   List out the types of binding available in WCF?
The types of binding available in WCF are
  • BasicHttpBinding
  • NetTcpBinding
  • WSHttpBinding
  • NetMsmqBinding
20)   Explain what is DataContractSerializer?
The process of changing an object instance to a portable and transferable format is known as the Serialization, and serialization of data is referred as DataContractSerializer.
21)   Mention what are the various address format in WCF?
The various address format in WCF are
  • HTTP Address Format: à http:// local host:
  • TCP Address Format:à net.tcp://local host:
  • MSMQ Address Format:ànet.msmq://localhost:
22)   What is REST and what is the problem with WCF REST and how it can be resolved?
REST stands for Representational State Transfer, for designing network application REST is used. It relies on a stateless, client server, cacheable communications protocol.  The reason behind creating WCF is to support SOA and not REST. It requires a lot of configuration in order to create HTTP REST service using WCF.  To overcome this tedious task, ASP.NET web API was introduced.
23)   List out what are the different isolation levels provided in WCF?
The different isolation levels provided in WCF are
  • Read Uncommitted
  • Read Committed
  • Repeatable Read
  • Serializable
What is 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.
1. Service contracts : Describe which operations the client can perform on the service.

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

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

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

What is address in WCF and how many types of transport schemas are there in WCF?
Address is a way of letting client know that where a service is located. In WCF, every service is associated with a unique address. This contains the location of the service and transport schemas. 

WCF supports following transport schemas
1. HTTP
2. TCP
3. Peer network
4. IPC (Inter-Process Communication over named pipes)
5. MSMQ

The sample address for above transport schema may look like

http://localhost:81
http://localhost:81/MyService
net.tcp://localhost:82/MyService
net.pipe://localhost/MyPipeService
net.msmq://localhost/private/MyMsMqService
net.msmq://localhost/MyMsMqService

What is the difference WCF and Web services?
1. Web services can only be invoked by HTTP. While Service or a WCF component can be invoked by any protocol and any transport type.

2. Second web services are not flexible. But Services are flexible. If you make a new version of the service then you need to just expose a new end point. So services are agile and which is a very practical approach looking at the current business trends.

How can we host a service on two different protocols on a single server?
Let’s first understand what this question actually means. Let’s say we have made a service and we want to host this service using HTTP as well as TCP.

You must be wondering why to ever host services on two different types of protocol. When we host a service it’s consumed by multiple types of client and it’s very much possible that they have there own protocol of communication. A good service has the capability to downgrade or upgrade its protocol according the client who is consuming him.

Let’s do a small sample in which we will host the ServiceGetCost on TCP and HTTP protocol.

Once we are done the server side coding its time to see make a client by which we can switch between the protocols and see the results. Below is the code snippet of the client side for multi-protocol hosting

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 main components of WCF?
1.Service: The working logic or offering, implemented using any .Net Language©.

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.

Explain transactions in WCF.
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.

What are different isolation levels provided in WCF?
The different isolation levels:

1. READ UNCOMMITTED: – An uncommitted transaction can be read. This transaction can be rolled back later.

2. READ COMMITTED :-
 Will not read data of a transaction that has not been committed yet

3. REPEATABLE READ: – Locks placed on all data and another transaction cannot read.

4. SERIALIZABLE:- Does not allow other transactions to insert or update data until the transaction is complete.

How do I serialize entities using WCF?
LINQ to SQL supports serialization as XML via WCF by generating WCF serialization attributes and special serialization specific logic during code-generation. You can turn on this feature in the designer by setting serialization mode to ‘Unidirectional’. Note this is not a general solution for serialization as unidirectional mode may be insufficient for many use cases.

What is End point ?
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.

Thursday, 3 December 2015

WCF

1.  For what purpose WCF is used in your current project?
Answer: Be specific to your answer. Interviewer is trying to understand your understanding on technology as well your interest on your project. Don't try to explain the features of WCF but just give specific requirement for which WCF was chosen in your project.
2.  What is binding in WCF?
Answer: Here, don't just answer "Binding defines How part of a service". Try to answer with practical aspect and elaborate it. Binding is an attribute of endpoint, used to specify communication details for a WCF service using 3 elements- transport protocol, messageencoding, and securityThis binding object is comprised of several binding-elements that are correspondent to a particular channel-stack of a channel. Our specified details in these binding elements are evaluated by service-runtime whenever an endpoint is constructed. There are many system provided bindings ready for our use. We can customize the behavior of a binding by customizing/changing its properties. If these system provided bindings are not fit for our service, we can create our own binding using "CustomBinding" class. 
3.   So, how many bindings are there? Have you ever used netMsmqBinding or netNamedPipeBinding?
Answer: Be prepared for this question as it covers your understanding on different bindings. I would recommend you to go through this MSDN link to understand in which scenario you should pick which binding.
4.  See, my hosted service is chargeable and I want to track the record that who has called/ accessed my service how many time, how can I do it?
Answer :  With this question, interviewer just wanted to know whether you are aware about WCF Extensibility or not. WCF Extensibility is a great feature that enable us to customize & manage service calls. For this question, the answer is: "Parameter Inspector" extension point is used to check and manage WCF service calls. In that we can use BeforeCall() and AfterCall() methods. Parameter Inspector is a WCF extension point. We can inject it in WCF pipeline at client side or server side where we want to validate or customize the call. We can check, modify and reject the call at both places.
Somewhere, you may be ask with a different name "Interceptors in WCF". Interceptor in WCF is nothing but a component that we can plugged into any pipeline stage of WCF.
5.  I want to restrict the concurrent access of my service to limited users, how can I do that? Where this setting is done?
Answer: WCF provide us a good way of extensibility to customize various WCF capabilities. We can customize Encoding message, Intercepting parameters etc. and extend the behaviour. Throttling behaviour of service plays an important role and can be set behaviour properties according to our business needs. Below are three important properties for optimizing our WCF service behaviour
  • maxConcurrentCalls limits the total number of calls that can currently be in progress across all service instances. The default is 16.
  • maxConcurrentInstances limits the number of InstanceContext objects that execute at one time across a ServiceHost. The default is Int32.MaxValue.
  • maxConcurrentSessions limits the number of sessions a ServiceHost object can accept. It is a positive integer that is 10 by default.


<behaviors>
 <serviceBehaviors>
   <behavior name="ServiceBehavior">
     <serviceThrottling maxConcurrentCalls="500" maxConcurrentInstances ="100" maxConcurrentSessions ="200"/>
   </behavior>
6.  How session management is done in WCF?
Answer: In WCF, session management is quite different than asp.net. In asp.net, session is manage & maintained at server but in WCF, a session is initiated /terminated by client application. Client application get processed message from server within a session that was initiated by it. This session can be used as an extensibility point by server application. Since, it is not maintained at server side, whatever messages are received by service at server, get processed in the order they reached. Also, in asp.net, we have data store facility for session (session variables) but this facility is not available in WCF. A service contract is required to be decorated with SessionMode attribute for session handling setting.
7.  How WCF service is secured?
Answer : Refer MSDN - Securing Services 
8.  Is there any way to give service access permission to role specific user group?
Answer : 
WCF provides us the Role Based Authentication mechanism that can be used for setting service access permission for a specific group. To make it work, we need to do some settings in web.config file as well we need to decorate our those services on which we want to implement this access security.
It can be done in 3 simple steps as :
1)   Enable “aspNetCompatibilityEnable”

2)   Then, we need to specify the Group for which we want to apply security in web.config as:

3)   And finally specify the security with class that implement your service. This decoration of service is require because, asp.net Compatibility Mode is set to false by default. RequirementMode is set to Allowed.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
This asp.net compatibility mode setting is done to use ASP features like identity impersonation. This setting applies at the application level (through root web.config file) and can’t be overridden at any folder level web.config files. By default, AspNetCompatibilityRequirementMode is : Allowed.



9.  Is overloading possible in WCF? If yes then how?
Answer : Yes, by using [OperationContract(Name="OverloadedName")] But, still outer world will get them with unique and different name. Overloading is a technique to write manageable/maintainable code. But in case of WCF service, uniqueness is required for generating WSDL, and that can be done either by using unique method name or unique OperationContract's name attribute value. Interfaces plays an important role and provides much flexibility for this. We can write many implementations of an interface, and it gives more flexibility plus concise. So why to think on overloading for WCF service methods?  

New Questions added on 05-April-2015:

10. What is MAC in WCF ?
11. How Event Logging is done in WCF ?