WCF Interview Questions and Answers

WCF Interview Questions and Answers

In this article, I am going to discuss the most frequently asked WCF Interview Questions and Answers. In the next article, I am going to discuss WCF Experienced Interview questions and answers. Let start the WCF Interview Questions and Answers discussion with the most basic question that asked in almost all interviews i.e. What is WCF.

What is WCF?

WCF stands for Windows Communication Foundation (WCF) and is a part of .NET Framework 3.0 which is considered as the Microsoft Service-Oriented Architecture (SOA) platform for building distributed and interoperable applications. WCF unifies ASMX, Remoting, and Enterprise Services stacks and provides a single programming model. WCF services are interoperable and support all the core Web services standards. 

What is the version of the .NET framework in which WCF is released?

WCF – Windows Communication Foundation is released as part of .NET Framework 3.0. WPF (Windows Presentation Foundation), WF (Workflow Foundation), and Card Space are also part of .NET Framework 3.0.

What is a distributed application?

In simple terms, we can say that a distributed application is an application where parts of it run on 2 or more computers. Distributed applications are also called connected systems.

Examples: A web application running on one machine and a web service that this web application is consuming is running on another machine. 

WCF Interview Questions and Answers  

An enterprise web application may have the following tiers and each tier may be running on a different machine

  1. Presentation tier
  2. Business tier 
  3. Data Access tier 
WCF Interview Questions and Answers  
Why we build distributed applications?

There are several reasons for this

  1. An enterprise application may need to use the services provided by other enterprises. For example, an eCommerce application may be using the Paypal service for payments.
  2. For better scalability. An enterprise web application may have the Presentation tier, Business tier, and Data Access tier and each tier may be running on a different machine.
What is an interoperable application?

An application that can communicate with any other application that is built on any platform and using any programming language is called an interoperable application. Web services are interoperable whereas .NET remoting services are not. Web services can communicate with any application built on any platform whereas a .NET remoting service can be consumed only by a .net application.

What technology choices did we have before WCF to build distributed applications?
  1. Enterprise Services
  2. Dot Net Remoting
  3. Web Services
What are the core components of WCF Service?

A WCF service has at least the following core components.

  1. Service Class:  A service class implementing in any CLR-based language and expose at least one method.
  2. Hosting Environment: a managed process for running service.
  3. Endpoint: a client uses it to communicate with the service.
What are the new features introduced in WCF 4.5?

Windows Communication Foundation v4.5 was released with a number of cool features. These important features are:

  1. Simplified Configuration files
  2. ToolTip and Intellisense support
  3. Task-based asynchronous Programming Model
  4. BasicHttpsBinding support
  5. Single WSDL Document
  6. WCF Configuration Validation
  7. ASP.NET Compatibility Mode changed
  8. Contract First Development
  9. Multiple Authentication Types
  10. XmlDictionaryReaderQuotas default value changed
  11. UDP Transport Support and many more…
What is the difference between WCF and ASMX Web services?

The basic difference is that the ASMX web service is designed to send and receive messages using SOAP over HTTP only. While WCF service can exchange messages using any format (SOAP is the default) over any transport protocol (HTTP, TCP/IP, MSMQ, Named Pipes, etc).

ASMX Web Services:
  1. Can only be hosted in IIS.
  2. It only supports HTTP Protocol
  3. Limited Security
  4. It uses XmlSerializer
WCF:
  1. Multiple Hosting options such as WAS, Console, IIS, WCF provide host, etc.
  2. Supports HTTP, TCP. MSMQ, NamedPipes.
  3. A consistent security programming Model
  4. Uses DataContractSerializer
What is the difference between Service EndPoint and Client Endpoint?

As we already understood the concept of an Endpoint and its ABC (Address, Binding, Contract). Both Service and Client Endpoint has the same ABC but we think in different perspective while working with them. We can differentiate between Service and Client Endpoint with respect to Address, Binding, and Contract as:

Service EndPoint:
  1. WHERE: URL of hosted service. (Address)
  2. WHAT: bindings being used. (Binding)
  3. CONTRACT: Service Contract i.e. interfaces (Contract)
Client EndPoint:
  1. WHERE: where to connect with hosted service.
  2. WHAT: binding supported by service.
  3. CONTRACT: what to pass and expect while communicating with service.
Why should we use WCF?

Let’s take this scenario. We have 2 clients and we need to implement a service for them. 

  1. The first client is using a Java application to interact with our service so for interoperability this client wants messages to be in XML format and the protocol to be HTTP.
  2. The second client uses .NET so for a better performance this client wants messages formatted in binary over TCP protocol.
Without WCF

To satisfy the first client requirement we end up implementing an ASMX web service and to satisfy the second client requirement we end up implementing a remoting service. These are 2 different technologies and have completely different programming models. So, the developers have to learn different technologies. So, to unify and bring all these technologies under one roof Microsoft has come up with a single programming model that is called as WCF – Windows Communication Foundation.

With WCF,

We implement one service and we can configure as many endpoints as want to support all the client needs. To support the above 2 client requirements, we would configure 2 endpoints. In the endpoint configuration, we can specify the protocols and message formats that we want to use. 

What is the advantage of using WCF over other distributed programming models like Web Services (ASMX), .NET Remoting, Enterprise Services stack, etc.?

This is one of the frequently asked WCF Interview Questions and Answers. To understand the advantage of using WCF over other distributed programming models like Web Services (ASMX), .NET Remoting, Enterprise Services stack, etc, let’s consider the following scenario. We have developed an application using web services. As we know web services use HTTP protocol and XML SOAP formatted messages, they are good for developing interoperable applications in a heterogeneous environment. We have a new client. Our new client is using .NET and he wants binary formatted messages over TCP protocol because interoperability is not a concern and binary formatted messages over TCP protocol are much faster than XML SOAP formatted messages over HTTP. To satisfy the requirement of this client, now we cannot use our existing web service. So, we have to develop a brand-new remoting application from the scratch. The business functionality is the same in web services and remoting applications. Since our different clients have different requirements, we ended up creating the same business application using web services and remoting technologies. This approach has several disadvantages as listed below.

  1. Developers have to be familiar with two different technologies (Web Services and Remoting).
  2. We end up creating duplicate business applications with different technologies which also leads to maintenance overhead.

On the other hand, WCF unifies Web Services, .NET Remoting, and Enterprise Services stacks under one roof. For the same requirement that we have seen until now, we just create one application and expose multiple endpoints to satisfy the requirements of multiple clients. In WCF configuration drives protocol choices, messaging formats, process allocation, etc. WCF services are loosely coupled, meaning that a WCF service is not bound to a particular protocol, encoding format, or hosting environment. Everything is configurable.

Why WCF Services are considered loosely coupled?

WCF Services are considered loosely coupled because WCF services are not tightly bound to a particular protocol, encoding format, or hosting environment. All of these are configurable. At the time of designing WCF services, we do not have to worry about what protocol, encoding format, or hosting environment to use to expose the service. We can worry about all these at the time of deployment.

What are the 3 things that a WCF Services endpoint must-have? OR
What is the ABC of a WCF service?
  1. Address – The address where the WCF Service is hosted. In other words, what is the location of the service? An address could be an IP Address, server name, URL, and so on.
  2. Binding – The binding that decides the protocol, message encoding, and security to use. Binding also decides whether to use reliable messaging and transaction support. That means it’s all about how the messages are handled on the service side and the client-side.
  3. Contract – The service contract defines what service operations are available as part of that service to the client for consumption.

So, the Address (A), Binding (B), and Contract (C) are called the ABC of the service endpoint. To reach an endpoint, the Address (Where), the Binding (How) and the Contract (What) are really important for establishing communication.

What is the role of WSDL in WCF? OR
What is WSDL?

This is one of the frequently asked WCF Interview Questions and Answers. WSDL stands for Web Service Description Language. The WCF service exposes the WSDL document for the clients to generate proxies and the configuration file. The WSDL file provides the following information for the consumers of the WCF service.

  1. Provides the information about the service contract and operations available.
  2. Provides information about all the endpoints exposed by the WCF service.
  3. Provides information about the messages and types that can be exchanged between the client and the WCF service.
  4. WSDL also provides any information about the policies used.
Is it possible for a WCF service to implement multiple service contracts?

Yes, we make the service class implement multiple service interfaces, and then expose each service using a different endpoint. 

In how many ways metadata can be exposed to the clients in WCF?

In WCF Metadata can be exposed in 2 ways.

  1. WSDL – Default format and it doesn’t support non-http protocols.
  2. Mex – New improved version and supports non-http protocols and it has some additional security features.

It will expose metadata via WSDL through the default URL, even if we don’t have defined a mex endpoint for our service

What is the tool that a client application can use to generate the proxy for a WCF service?

Service Utility (svcutil.exe) can be used by the clients to generate the proxy and configuration file. For the client to be able to generate proxies, the service should enable metadata exchange.

Define Service Contracts and Operation Contracts in WCF?

Service Contract- An interface that exposes the service operations is usually decorated with the ServiceContract attribute. Always provide meaningful Namespace and Name to a service contract. A service contract defines the operations which are exposed by the service to the outside world. A service contract is the interface of the WCF service and it tells the outside world what the service can do. It may have service-level settings, such as the name of the service and namespace for the service.

Operation Contract – All methods in a service contract should have the OperationContract attribute. We can also provide explicit Name, Action, and ReplyAction. An operation contract is defined within a service contract. It defines the parameters and returns type of an operation. An operation contract can also define operation-level settings, like the transaction flow of the operation, the directions of the operation (one-way, two-way, or both ways), and the fault contract of the operation.

Can we apply the ServiceContract attribute to a class rather than an interface in WCF?

Yes, a ServiceContract attribute can be applied either to a class or an interface but defining service contracts using interfaces rather than classes has the following benefits.

  1. Defining service contracts using interfaces remove coupling to service implementation. Later the implementation can be changed at will without affecting the clients.
  2. Defining service contracts using interfaces, also allows a service to implement more than 1 contract.
What is the purpose of the MessageParameter attribute in WCF?

MessageParameter attribute is used to control the parameter and returned object names from a service operation.

What is serialization with respect to WCF?

With respect to WCF, Serialization is the process of converting an object into an XML representation. The reverse process that is reconstructing the same object from the XML is called Deserialization. 

What are the different options available to serialize complex types that are sent and received between clients and services in WCF?

This is one of the frequently asked WCF Interview Questions and Answers. The following are the different options available to serialize complex types that are exchanged between clients and services in WCF. These options have their own advantages and disadvantages. A data contract is a preferred way to serialize complex types in WCF.

  1. Serializable types – Use the Serializable attribute on the type that we want to serialize
  2. Data contracts – Use the DataContract attribute on the type and the DataMember attribute on every member of the type that we want to serialize. We can apply the DataMember attribute either on a field or a property.
  3. Known types – Use Known types to enable polymorphic behavior in service contracts.
  4. IXmlSerializable – IXmlSerializable types provide XSD schema to Web Services Description Language (WSDL) and metadata exchange (MEX).

By default, WCF uses DataContractSerializer. For a complex type like Customer, Employee, Student to be serialized, the complex type can either be decorated with 

  1. SerializableAttribute or
  2. DataContractAttribute

With .NET 3.5 SP1 and above we don’t have to explicitly use DataContract or DataMember attributes. The DataContractSerializer will serialize all public properties of our complex type in alphabetical order. By default, private fields and properties are not serialized.

If we decorate a complex type with [Serializable] attribute the DataContractSerializer serializes all fields. With the [Serializable] attribute we don’t have explicit control on what fields to include and exclude in serialized data. And it cannot serialize properties.

If we decorate a complex type with [Datacontract] attribute, the DataContractSerializer serializes the fields marked with the [DataMember] attribute. The fields that are not marked with the [DataMember] attribute are excluded from serialization. The [DataMember] attribute can be applied either on the private fields or public properties.

In WCF, the most common way of serialization is to mark the type with the DataContract attribute and mark each member that needs to be serialized with the DataMember attribute. 

If we want to have explicit control on what fields and properties get serialized then use DataContract and DataMember attributes. 

  1. Using DataContractAttribute, we can define an XML namespace for our data
  2. Using DataMemberAttribute, we can define Name, Order, and whether if a property or field IsRequired or not. Also, serialize private fields and properties
What is the disadvantage of using the Serializable attribute to serialize a complex type that is sent and received between clients and services in WCF?

When we decorate a class with the Serializable attribute, all the fields of the class are serialized regardless of the accessibility. We do not have control over what to serialize and what not to serialize. Also, we will not have any control over naming conventions or data types.

What is the preferred way for serializing complex types in WCF?

This is one of the frequently asked WCF Interview Questions and Answers. The preferred way for serializing complex types in WCF is to use data contracts. Using Data Contracts provides us with the following advantages.

  1. Using the DataMember attribute, we can control which members of the class to serialize.
  2. We can also control the order in which members are serialized using the Order parameter of the DataMember attribute.
  3. We can also provide explicit Names to the serialized members using the Name parameter of the DataMember attribute.
  4. We can also specify if a member is required or optional using the IsRequired parameter of the DataMember attribute.

Consider the example below which uses Name, IsRequired, and Order parameters of the DataMember attribute to serialize CustomerId property. By the way, the DataMember attribute can be used with either fields or properties. If you do not specify the order in which members are serialized, then by default alphabetical ordering is done by the DataContractSerializer.

WCF Interview Questions and Answers

Why we use Known Type in WCF?

If we have classes related by inheritance the WCF service generally accepts and returns the base type. If we expect the service to accept and return inherited types then use the KnownType attribute.

Why Message Contract?

With Data Contracts we have very limited control over the SOAP XML request and response messages that are generated. Use Message Contracts if we want to have full control over the generated XML SOAP messages.  Few examples of when Message Contracts can be handy

  1. Include some custom data in the SOAP header. In general SOAP headers are used to pass user credentials, license keys, session keys, etc.
  2. Change the name of the wrapper element in the SOAP message or remove it altogether.
Explain the significance of the MessageContract attribute? OR
Why and when do we use the MessageContract attribute in WCF?

This is one of the frequently asked WCF Interview Questions and Answers. There are several advantages of using the MessageContract attribute in WCF. MessageContract attribute can be used for 

  1. Adding custom headers to the message.
  2. Controlling message wrapping.
  3. Controlling signing and encryption of messages.

MessageContract attribute provides us with greater control over message headers and body elements. MessageContract attribute converts a type to a SOAP message. The example below shows how to use IsWrapped and ProtectionLevel parameters of the MessageContract attribute. We may also set an explicit Name and Namespace.

Why and when do we use the MessageContract attribute in WCF?

MessageContract attribute is supported by MessageHeader attribute and MessageBodyMember attribute. We can apply the MessageHeader attribute to fields or properties of the message contract. This is a simple technique for creating custom headers. We can provide Name, Namespace, and ProtectionLevel. We may also set SOAP protocol settings like Relay, Actor, Must Understand.

MessageBodyMember attribute can also be applied to fields or properties of message contracts. It can have several body elements. This is equivalent to multiple parameters in operation and this is the only way to return multiple complex types. It is suggested as a good practice to always supply Order. You can also set Name, Namespace, ProtectionLevel. The example below shows how to use MessageHeader and MessageBodyMember attributes.

Why and when do we use the MessageContract attribute in WCF?

What is WS-Policy? OR
What is Web Services Policy?

Web Services Policy or WS-Policy is an interoperable standard for describing policies that influence communication with the clients. Usually, WS-Policy is included in the WSDL contract exposed by the WCF service, although it is optional.

What is the use of WS-Policy?

WS-Policy is generally used for

  1. Describing protocols for accessing operations
  2. Security
  3. Reliable messaging
  4. Transactions 
  5. Message encoding (Message Transmission Optimization Mechanism [MTOM])
  6. Other protocols

We can specify the above settings in WSDL directly without a policy section, but the disadvantage is that once published, the WSDL contract is final. If the clients have to communicate with a WCF service that has changed the settings in the WSDL, the clients need to rebuild the proxy and configuration or at least the changes to the WSDL contract must support backward compatibility.

The advantage of using WS-Policy is that it can change over time, and the clients can discover the changed policy to communicate via metadata exchange. But keep in mind that, you can only change the policy safely if clients are positioned to handle dynamic changes.

What are SOAP Faults in WCF?

This is one of the frequently asked WCF Interview Questions and Answers. Common language runtime (CLR) exceptions do not flow across service boundaries. At the maximum, a CLR exception may propagate up to the service tier from business components. Unhandled CLR exceptions reach the service channel and are serialized as SOAP faults before reporting to clients. An unhandled CLR exception will fault the service channel, taking any existing sessions with it. That is why it is very important to convert the CLR exceptions into SOAP faults. Where possible, throw fault exceptions

SOAP faults are standards-based and interoperable. There are 2 formats used by SOAP faults, SOAP 1.1 and SOAP 1.2. SOAP format depends on the binding used.

What happens if there is an unhandled exception in WCF?

This is one of the frequently asked WCF Interview Questions and Answers. If there is an unhandled exception in WCF, the service model returns a general SOAP fault that does not include any exception-specific details by default. However, we can include exception details in SOAP faults, using the IncludeExceptionDetailsInFaults attribute. If IncludeExceptionDetailsInFaults is enabled, exception details including stack trace are included in the generated SOAP fault. IncludeExceptionDetailsInFaults should be enabled for debugging purposes only. Sending stack trace details is risky.

What are bindings in WCF?

Bindings in WCF define the configuration of the communication channels between clients and services. Binding specifies 

  1. Transport Protocol
  2. Message encoding
  3. Security Mechanism
  4. Reliable Messaging
  5. Transactions Support
What are different Transport protocols available in WCF?
  1. TCP
  2. HTTP
  3. Named Pipes
  4. MSMQ
What message encoding formats are available in WCF?
  1. Text – For interoperability
  2. MTOM (Transmission Optimization Mechanism) – For transferring large objects
  3. Binary – For speed
What are the 2 ways available for configuring bindings in WCF?
  1. Declaratively using a configuration file
  2. Programmatically in the code.
  3. Using a configuration file has several advantages.
List a few standard bindings available in WCF?
  1. BasicHttpBinding
  2. NetPeerTcpBinding
  3. WSFederationHttpBinding
  4. NetNamedPipeBinding
  5. WSHttpBinding
  6. WSDualHttpBinding
  7. NetTcpBinding
  8. NetMsmqBinding
  9. MsmqIntegrationBinding
Can we create a customized binding in WCF?

Yes, bindings in WCF are completely extensible and we can create a customized one

What is the different message exchanging patterns available in WCF?

This is one of the frequently asked WCF Interview Questions and Answers. There are 3 main different message exchanging patterns available in WCF.

  1. Request-Reply – In the request-reply pattern, a client application sends a message to a WCF service and then waits for a reply. This is the classic and most commonly used message exchange pattern in WCF.
  2. One-Way – In a one-way message exchange pattern no response is sent back, even if there is an exception. In the one-way message exchange pattern, a client application sends a message to a WCF service but the service does not send a reply message to the client. We can use this pattern when a client requests the service to take an action but does not need to wait for a reply.
  3. Duplex – In the request/reply and one-way message exchange patterns, only the client can initiate communication. In the duplex pattern, both the client and the service can initiate communication. The client calls a method of the service. The service can then use a client callback to call a method in the client. We can use this pattern when we want the service to send a notification or alert to the client after the client has called the service.
What is the default message exchange pattern used in WCF?

Request/Reply

How do you set up a one-way operation?

Set OperationContractAttribute’s IsOneWay property to true. An example is shown below.

How do you set up a one-way operation?
What is MTOM?

MTOM stands for Message Transmission Optimization Mechanism and is an Interoperable standard that reduces the overhead of large binary data transfer. Removes bloat and processing overhead of base64 encoded data. Improves overall message transfer performance.

What are the instancing modes available in WCF?
  1. Percall
  2. PerSession
  3. Single
What are the advantages of using Percall instancing mode in WCF?

In a Percall instance mode, a new service instance (object) is created for each call. The following are the advantages and Disadvantages of Percall instance mode.

Advantages:
  1. Less memory consumption
  2. Service instances are freed
  3. Concurrency is not an issue
  4. PerCall services increase overall throughput
Disadvantages:
  1. State not maintained between calls
What are the four-session types available in WCF?
  1. Transport session
  2. Reliable sessions
  3. Secure sessions
  4. Application sessions
What are the advantages and disadvantages of using PerSession instancing mode in WCF?

A new service object gets created for each client/proxy. The following are the advantages and Disadvantages of the PerSession instance mode.

Advantages:
  1. State maintained by the service instance
Disadvantages:
  1. Less throughput, greater memory consumption
  2. Concurrency issues for multithreaded clients
Name a few bindings that can support PerSession instancing mode?
  1. NetTcpBinding
  2. NetNamedPipeBinding
  3. WSHttpBinding
  4. WSFederationHttpBinding
  5. WSDualHttpBinding
What is the default Application Session timeout in WCF?

Session lifetime lasts 10 minutes by default, for Application Session.

What are the advantages and disadvantages of using Single, Instancing mode in WCF?

In a single instancing mode, a single service object is created for all calls from all clients and sessions this type of WCF service is also called a singleton service. The following are the advantages and Disadvantages of the Single instance mode.

Advantages:
  1. State maintained by the service instance
Disadvantages:
  1. Least throughput
  2. Potentially greater memory consumption
  3. Concurrency issues
What are the general guidelines for choosing an instancing mode?

In general, for scalability and throughput prefer to use PerCall services where ever possible. Use PerSession services only when necessary, but keep in mind the overhead of sessions and session timeouts. Try to avoid singletons almost always. Singleton services could be useful on client machines for shared functionality. These are only general guidelines, and your selection depends on what you are trying to achieve.

What are the different concurrency modes available in WCF?
  1. Single
  2. Reentrant
  3. Multiple
Are WCF services protected from concurrent access by default?

Yes, WCF services are protected from concurrent access by default, as the concurrency mode of a WCF service is set to Single by default.

Explain Single Concurrency Mode?

In a Single concurrency mode, only one request is processed by the same service instance. A lock is acquired while a request is processed by a service instance. Other threads, if any, are queued until they timeout. Once the lock is released when the current request completes, the next thread in the queue can then access the objects. 

What effect does the Single concurrency mode setting have on PerCall, PerSession, and Singleton services?
  1. PerCall services and Single Concurrency mode: A new service instance is allocated for each thread. Here, concurrency is not an issue and Single-mode setting has no impact on the throughput and concurrent calls can be processed.
  2. PerSession services and Single Concurrency mode: Service instances are protected against multithreaded clients. Single-mode impacts throughput of the single client but multiple clients can get through and concurrent calls can be processed.
  3. Singleton services and Single Concurrency mode: Service instances are protected against any concurrent calls. Single concurrency mode impacts the throughput of the singleton. Multiple threads and clients cannot get through and no concurrent calls can be processed
Explain Reentrant Concurrency Mode?

Reentrant concurrency mode is useful when services issue callbacks to clients If callback operations are not one-way. Services release the acquired lock upon exit to make the callback and another thread is able to acquire the lock. In this case, the return from the callback will queue.

What effect does the Reentrant concurrency mode setting have on PerCall, PerSession, and Singleton services?
  1. PerCall services and Reentrant Concurrency mode: In a case where PerCall services may need reentrancy, if we set the concurrency mode to Single, deadlock is guaranteed, whereas if it is In Reentrant mode, we will have no problem.
  2. PerSession services and Reentrant Concurrency mode: PerSession services allow a multithreaded client to access the service instance
  3. Singleton services and Reentrant Concurrency mode: Singleton services allow any threads to access the service instance
What is the effect of Multiple concurrency mode on PerSession services with multithreaded clients?

PerSession services with multithreaded clients can have increased throughput, with Multiple concurrency mode, as no locks are acquired when requests are being processed by a service instance. However, care should be taken to protect shared resources.

What are different .NET multithreading techniques available to protect a shared resource?
  1. Monitor
  2. Mutex
  3. Semaphore
  4. ReadWriterLock
  5. Interlocked
What are the factors that can influence overall throughput for a service, when multiple concurrency modes are enabled?
  1. Instancing mode 
  2. Concurrency mode
  3. Throttling behavior
What is ServiceThrottleBehavior?

ServiceThrottleBehavior provides several settings for throughput control, as shown below.

  1. MaxConcurrentCalls: Maximum concurrent requests allowed. The default is 16.
  2. MaxConcurrentInstances: Maximum concurrent service instances allowed. The default is int.MaxValue.
  3. MaxConcurrentSessions: Maximum concurrent active sessions. This includes transport, reliable, secure, and application sessions. The default is 10.
How to control the WCF service session timeout?

This is one of the frequently asked WCF Interview Questions and Answers. The default session timeout is 10 minutes. If we want to increase or decrease the default timeout value

Step 1: Set receiveTimeout attribute of the respective binding element as shown below. In the example below, we have configured the session timeout for 10 seconds.

<bindings>
           <netTcpBinding>
                  <binding name=”netTCP” receiveTimeout=”00:00:10″></binding>
           </netTcpBinding>
</bindings>

Step 2: Associate the binding element with the endpoint using bindingConfiguration attribute as shown below.

<endpoint address=”SimpleService” binding=”netTcpBinding” contract=”SimpleService.ISimpleService” bindingConfiguration=”netTCP”/>

What happens when the session timeout is reached?

When the session timeout is reached, the connection to the WCF service is closed. As a result, the communication channel gets faulted and the client can no longer use the same proxy instance to communicate with the service. This also means that along with the session, the data in the service object is also lost. After the session has timed out, 

1. On the first attempt to invoke the service using the same proxy instance would result in the following exception.
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was ’00:00:59.9355444′.

2. On the second attempt, the following exception will be thrown

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

How to fix the communication channel is in a faulted state exception
  1. Put the line that calls the service should in the try block
  2. Catch the CommunicationException
  3. Check if the communication channel is in a faulted state and create a new instance of the proxy class.
Can we have multiple endpoints for different binding types in order to serve different types of clients?

Yes, we can have multiple endpoints for different binding types. For example, an endpoint with wsHttpBinding and another one with netTcpBinging.

What are the hosting options for WCF Services? Explain.

For a service to host, we need at least a managed process, a ServiceHost instance, and an Endpoint configured. Possible approaches for hosting a service are:

Hosting in a Managed Application/ Self Hosting

  1. Console Application
  2. Windows Application
  3. Windows Service

Hosting on Web Server

  1. IIS 6.0 (ASP.NET Application supports only HTTP)
  2. Windows Process Activation Service (WAS) i.e. IIS 7.0 supports HTTP, TCP,
    NamedPipes, MSMQ.

In the next article, I am going to discuss WCF Experienced Interview questions with answers. Here, in this article, I try to explain the most frequently asked WCF Interview Questions and Answers. I hope you enjoy this WCF Interview Questions and Answers article. I would like to have your feedback. Please post your feedback, question, or comments about this WCF Interview Questions and Answers article.

Leave a Reply

Your email address will not be published.