Wednesday, 28 July 2021

 Windows Communication Foundation


1.     What is WCF?

Windows Communication Foundation (WCF) is a framework for building service-oriented applications.WCF allows applications to communicate with each other in a distributed environment. WCF is a set of technologies that covers ASMX web services, Web Services Enhancements (WSE), .NET Remoting and MSMQ. The purpose of WCF is to provide a single programming model that can be used to create services on the .NET platform for organizations.

 

2.     What are the features and advantage of WCF?

Features of WCF**

Windows Communication Foundation (WCF) is a secure, reliable, and scalable messaging platform for the .NET Framework 3.0,

·        Service Orientation

·        Interoperability

·        Multiple Message Patterns

·        Service Metadata

·        Data Contracts

·        Security

·        Multiple Transports and Encodings

·        Reliable and Queued Messages

·        Durable Messages

·        Transactions

·        AJAX and REST Support

·        Extensibility

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.

 

3.     What is SOA?

SOA stands for Service Oriented Architecture. It is an architectural design pattern which states that every component of a system should be a service (here service means a unit of a program that serves a business process), and the system should be composed of several loosely-coupled services means services should be independent of each other, so that changing one of them should not affect any other services.

4.     What are Service Oriented Design Principles?

There are four Service-oriented principles (tenets) in SOA as given below :

1.     Boundaries are explicit: This SOA principle states that a service can be deployed anywhere and be easily and freely accessed by other services, regardless of the environment or development language of the other services.

2.     Services are autonomous: This SOA principle states that each service must be managed and versioned differently so that they do not affect other services in the process. Contracts, once published, should not be changed. Services need to be isolated and decoupled to accomplish the goal of making them autonomous.

3.     Services share schema and contract, not class: This SOA principle states that services should not pass classes and types; they pass schemas (data) and contracts (behaviors). This allows for a loosely coupled system where the service does not care what type of environment the other service is executing on. The information being passed is 100% platform independent.

4.     Service compatibility is determined based on policy: This SOA principle states that each service should have its own compatibility level and knows how it would interact with other services. Services look at each other’s policy, looking for similarities so that they can start communicating. If two services can’t satisfy each other’s policy requirements, they are not compatible with each other.

5.     What is SOAP?

SOAP stands for Simple Object Access Protocol which is used for exchanging data in XML-based format over HTTP. SOAP is widely-used by web services for exchanging data. A simple SOAP message structure is given below:

 <soap:Envelope xmlns:soap=”http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
 <soap:Body xmlns:m="http://www.mysite.com/books">
 <m:Book>
 <m:BookName>WCF and Web Services Interview Questions and Answers</m:BookName>
 </m:Book>
 </soap:Body>
 </soap:Envelope>

A SOAP message has an optional header and a required body element. A SOAP message header can contain the application-specific information like authentication or authorization etc. Also, a SOAP message header must be the first child element of the SOAP Envelope element. The SOAP body element contains the actual SOAP message. Moreover, SOAP is a combination of HTTP protocol and XML i.e. SOAP = HTTP + XML

6.     What is WS -* Protocols?

The SOAP protocol is used for message exchange between Web Services. But SOAP defines very basic semantics for message headers. It does not always define meaning to the header. Hence to extend SOAP, WS-* protocols specifications have been developed with semantics which can then be reused in various application scenarios.

WS -* protocols are a set of protocols (means standards and specifications) that helps us to implement certain needs and behaviors of a service. These protocols describe how to exchange messages in a secure, transactional, and reliable way by using the headers in the SOAP message. WCF can implement the WS -* protocols by using WsHttpBinding. This binding makes use of some of the WS -* protocols and adds the needed behaviors, such as transactional message calls, reliability, discovery, and addressing.

7.     What are the differences between ASP.NET Web Service and WCF?

The differences between ASP.NET Web Service and WCF are given below:

 

S.No

WCF

Asp.net Web Service

1.

ServiceContract and OperationContract attributes are used for defining WCF service.

WebService and WebMethod attributes are used for defining web service.

2.

Supports various protocols like HTTP, HTTPS, TCP, Named Pipes and MSMQ.

Supports only HTTP, HTTPS protocols.

3.

Hosted in IIS, WAS (Windows Activation Service), Self-hosting, Windows Service.

Hosted only in IIS.

4.

Supports security, reliable messaging, transaction and AJAX and REST supports.

Support security but is less secure as compared to WCF.

5.

Supports One-Way, Request-Response and Duplex service operations.

Supports One-Way and Request-Response service operations.

6.

WCF are faster than Web Services.

Web Services are slower than WCF

7.

Hash Table can be serialized.

Hash Table cannot be serialized. It can serializes only those collections which implement IEnumerable and ICollection.

8.

Unhandled Exceptions does not return to the client as SOAP faults. WCF supports better exception handling by using FaultContract.

Hash Table cannot be serialized. It can serializes only those collections which implement IEnumerable and ICollection

9.

Supports XML, MTOM, Binary message encoding.

Supports XML and MTOM (Message Transmission Optimization Mechanism) message encoding.

10.

Supports multi-threading by using ServiceBehaviour class

Doesn’t support multi-threading.

 

5.     What is WCF ABC?

WCF ABC stands for Address, Binding, and Contract.

Address (Where)

WCF Address specifies to a specific location, where the service is hosted. A WCF Address is specified as a URI. URI’s first part specifies the transport protocol (HTTP, TCP, Net.pipe and MSMQ), and the other parts specify the host machine address and service path as shown in below fig.

WCF address example

Others sample Addresses are given below:

 
 http://www.mywebsite/MyService
 net.tcp://www.mywebsite/MyService
 net.pipe://www.mywebsite/MyPipeService
 net.msmq://www.mywebsite/MyMsMqService

Binding (How)

WCF binding specifies how the service and client will communicate with each other in terms of transport protocols and encoding (such as text or binary). WCF binding is a set of binding elements and each element specify, how the service and client will communicate with each other. Each binding must have at least one transport element and one message encoding element.

 

1.               Contract (What)

WCF contract specifies what the service contains. WCF has five types of contracts: service contract, operation contract, data contract, message contract and fault contract. Each contract defines certain behavior.

2.     How to define multiple endpoints for a WCF service?

A WCF service configuration with multiple endpoints is given below :

 <system.serviceModel>
 <services >
 <service name = "MyWCFService">
 <endpoint address = "http://localhost:90/MyWCFService" 
 binding = "wsHttpBinding" contract = "IMyWCFContract"/>
 <endpoint address = "net.tcp://localhost:91/MyWCFService"
 binding = "netTcpBinding" contract = "IMyWCFContract"/>
 </service> 
 </services>
 </system.serviceModel>

3.     What are default Endpoints?

WCF offers some default endpoints to the service if a service host doesn’t define any endpoints but has at least one base address. For example, WCF uses the basic binding for the Http address.

4.     What are standard Endpoints?

WCF provides a set of pre-defined endpoints known as Standard Endpoints for metadata exchange, discovery and web. You can configure the standard endpoints by using a config file and programmatically. Here is the list of standard endpoints :

·        mexEndpoint

·        webHttpEndpoint

·        webScriptEndpoint

·        workflowControlEndpoint

·        announcementEndpoint

·        discoveryEndpoint

·        udpAnnouncementEndpoint

·        udpDiscoveryEndpoint

5.     What are the different WCF contracts?

WCF contract specifies the service and its operations. WCF has five types of contracts:

·        Service contract

·        Operation contract

·        Data contract

·        Message contract

·        Fault contract.

 

6.     What is a service contract in WCF?

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.

 

7.     What is Message Contract in WCF?

 

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.

 

8.     What is an Operation Contract in WCF?

An operation contract is defined within a service contract. It defines the parameters and return type of an operation. An operation contract can also defines operation-level settings, like as the transaction flow of the operation, the directions of the operation (one-way, two-way, or both ways), and fault contract of the operation.

 

9.      What is Data Contract?

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.

 

10.  What is Fault Contract?

Fault Contract can be associated with a service operation to denote errors that can be returned to the caller. An operation can have zero or more faults associated with it. These errors are SOAP faults that are modeled as exceptions in the programming model.

 

11.  What are the various ways of hosting a WCF service?

There are four ways of hosting a WCF service.

·        Self-Hosting

·        Windows services hosting

·        IIS hosting

·        Windows Activation Services hosting (WAS)

12.  What is Binding?

WCF binding specifies how the service and client will communicate with each other in terms of transport protocols and encoding (such as text or binary). WCF binding is a set of binding elements and each element specify, how the service and client will communicate with each other. Each binding must have at least one transport element and one message encoding element.

13.  What are the different types of bindings in WCF?

WCF supports the following types of built-in bindings:

1.     BasicHttpBinding

2.     WSHttpBinding

3.     WSDualHttpBinding

4.     WSFederationHttpBinding

5.     MsmqIntegrationBinding

6.     NetMsmqBinding

7.     NetNamedPipeBinding

8.     NetPeerTcpBinding

9.     NetTcpBinding

14.  What are the ways to create a WCF Client?

There are two ways to create a WCF Client or calling a WCF Service as:

·        WCF Proxy

·        Channel factory

15.  What is Instance Management in WCF?

WCF manages the session by creating the instance of the service class. This created instance(s) handle the incoming service request. In WCF, the session is the way of managing the services instance(s) so that the server can use these instances in an optimized way. At the server side, the InstanceContext class is used to manage service class instance. There are following instance management ways :

·        Per Call

·        Per Session

·        Single

16.  What is service proxy in WCF?

WCF Proxies are used to communicate between client and server. The communication takes place by exchanging the messages in the form of requests and responses. It will have the details like Service Path, Protocol details and so on.

 

17.  What is "Per Call" instance mode in WCF?

When a request has made to service, it creates a new instance of service for each method call and this will be disposed of once the response goes to the client. This whole process is known as per call instance mode.

 

18.  What is "Per Session" instance mode in WCF?

Per session instance mode creates a logical session between service and client and it will be maintained till the end of the session. When client requests from service the session will be created and it is dedicated to the instance for that client and it will be going to the end when client session ends.

 

19.  What is "Singleton" instance mode in WCF?

In "Singleton" mode all the clients are connected to the single instance of the service and when service configured for "Singleton" mode, an instance will be created when service is hosted and it will be disposed of once it shuts down.

20.  What is Concurrency Management in WCF?

Concurrency management is closely related to the Instance management in WCF but both are two different things. Instance management specifies how the service instances are created while Concurrency management specifies how many concurrent requests are handled by the service instances. By using concurrency, you can make your service instance thread-safe. By default, a per-call service instance is thread-safe since each request is served by a new service instance. A per-session service instance is not thread-safe since multiple requests of a client are served by a single service instance. Hence, it’s required concurrency management. A single service instance is not thread-safe since multiple requests of all clients are served by a single service instance. Hence, it’s required concurrency management.

21.  What is Impersonation?

Impersonation is a way to authorize a caller (client) identity to access the service domain’s resources. The service resources may be local files or database tables and should be on the same machine on which service is hosted. By default, impersonation is disabled and resources are accessed by using the WCF service's process identity.

22.  What is service versioning?

After the initial deployment of the WCF service, you may need to change the service for a variety of reasons like as changing business needs or fixing others issues. Each change in your existing service introduces a new version of the service. Service versioning is helpful in backward compatibility with your existing clients.

23.  What is WCF Data Service?

WCF Data Services uses OData (Open Data Protocol) protocol for querying or manipulating the data. WCF Data Services is built on top of WCF REST Services. It is a RESTful service to support CRUD operations on the database using the HTTP protocol. It supports all database operations using URI. DATA protocol can expose data from the relational database, File systems, Web sites, services etc. It supports XML or JSON format for exposing the data.

 

24.  Difference between WCF and ASP.NET Web API?

1.     Web API supports both REST and SOAP services but it is ideal for developing restful services, on the other hand WCF was originally created to enable SOAP-based services but it also supports REST services with limitations.

2.     WCF Supports multiple Protocols i.e HTTP, TCP, UDP and IPC but Web API supports only HTTP protocol.

3.     Web API Supports MVC feature i.e routing, controllers, action and results but WCF does not.

4.     Both Supports multiple data formats.

5.     Web API is based on request/response but additional patterns can be supported through SignalR and WebSockets integration while WCF Supports Request-Reply, Half and full Duplex communication.

6.     Both can be hosted with in the application or on IIS but WCF can also be hosted using windows service.

7.     Web API is light weight architecture so it is good for devices which have low bandwidth like mobile devices.

8.     For quick development, Web API is better choice than WCF.

9.     WCF supports more security options i.e WS-Security W3C standard but Web API can also be secured by using different security practices such as encryption.

 

25.  What are the essential components used in WCF?

Service class

The service runtime layer contains the behaviors that occur only during the actual operation of the service, that is, the runtime behaviors of the service. Throttling controls how many messages are processed, which can be varied if the demand for the service grows to a preset limit.

Endpoint

WCF Service is a program that exposes a collection of the endpoints. Each Endpoint is a portal for communicating with the world. All the WCF communications are taken place through the endpoint. An endpoint consists of three components

Hosting Environment

A service must be hosted in some process. A host is an application that controls the lifetime of the service. Services can be self-hosted or managed by an existing hosting process.

 

26.  Which are the different isolation levels in WCF?

Following is a list of different isolation levels in WCF:

·       Read Uncommitted: Also known as dirty isolation level. It makes sure that corrupt Data cannot be read. This is the lowest isolation level.

·       Read Committed: It ensures not to read the data that has been changed by any other application and is not yet committed. It is the default level.

·       Repeatable Read: It stops the usage of dirt read and non-repeatable read. It states that data fetched through a query will be locked and will not be updated by any other transaction.

·       Serializable: It does not allow any modification and addition of new data till the transaction is completed. This is considered to be a very restrictive level.

·       Snapshot: It raises an error on modifying a data that has already been changed by any transaction.

 

27.   Give the address format of all the bindings in WCF.

A list of address formats and their respective bindings:

TCF Address Format - net.tcp://local host:portnumber

HTTP Address Format - http://local host:portnumber

MSMQ Address Format - net.msmq://local host:portnumber

28.  What are the transport schemas supported by WCF?

WCF supports the following transport schemas:

·       HTTP

·       TCP

·       PEER network

·       IPC (Inter Process Communication)

·       MSMQ

29.  What are the different types of transaction managers supported by WCF?

These are three types of transaction managers supported by WCF:

·       Light Weight

·       WS-Atomic Transaction

·       OLE Transaction

30.  What is Throttling in WCF?

            In WCF, "Throttling" is used to limit the sessions or instances to be created at an application                     level. It is used to boost the performance.

 

 

 

 

 

 

No comments:

Post a Comment