Pages

C3 Health Services

Visit Official Website 9278982994

Expert Healthcare at Your Doorstep

Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Tuesday, 11 March 2014

Basic WCF Interview Questions and Answers for .NET Developers

Basic WCF Interview Questions and Answers for .NET Developers

If you are preparing of .NET interview, must brush up WCF concepts also. Mostly in every .NET project, WCF is used. So, every .NET developer must know at least basics of WCF. By keeping that in mind, I have tried to list down some basic WCF interview questions and answers which every .NET developer should know before going to the interview room. These WCF interview questions and answers cover basic concepts of WCF, SOA, difference between WCF and web services, need of WCF, endpoints in WCF like Address, Contracts and Bindings, types of Bindings in WCF, types of contracts in WCF, components of WCF, transport schemas in WCF, transactions in WCF, isolation levels in WCF, how to host WCF services, generating proxies for WCF services etc. So, lets have a look upon thes basic WCF interview questions and answers.

1. What is WCF (Windows Communication Foundation)?

Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows. WCF provides a runtime environment for services, enabling you to expose CLR types as services, and to consume other services as CLR types.

WCF is part of .NET 3.0 and requires .NET 2.0, so it can only run on systems that support it. WCF is Microsoft’s unified programming model for building service-oriented applications with managed code. It extends the .NET Framework to enable developers to build secure and reliable transacted Web services that integrate across platforms and interoperate with existing investments.

Windows Communication Foundation combines and extends the capabilities of existing Microsoft distributed systems technologies, including Enterprise Services, System.Messaging, Microsoft .NET Remoting, ASMX, and WSE to deliver a unified development experience across multiple axes, including distance (cross-process, cross-machine, cross-subnet, cross-intranet, cross-Internet), topologies (farms, fire-walled, content-routed, dynamic), hosts (ASP.NET, EXE, Windows Presentation Foundation, Windows Forms, NT Service, COM+), protocols (TCP, HTTP, cross-process, custom), and security models (SAML, Kerberos, X509, username/password, custom).

2. What is SOA (Service Oriented Architecture)?

Service-oriented architecture (SOA) is an evolution of distributed computing based on the request/reply design paradigm for synchronous and asynchronous applications. An application's business logic or individual functions are modularized and presented as services for consumer/client applications.

3. What is the difference between 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.

4. What was the code name for WCF?

The code name of WCF was Indigo . 

WCF is a unification of .NET framework communication technologies which unites the following technologies:- 

NET remoting 
MSMQ 
Web services 
COM+

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

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

7. What is the endpoint in WCF? 

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.

1. Address: Specifies the location of the service which will be like http://Myserver/MyService.Clients will use this location to communicate with our service.

2. Contract: Specifies the interface between client and the server.It’s a simple interface with some attribute.

3. Binding: Specifies how the two paries will communicate in term of transport and encoding and protocols.

8. What is the binding in WCF and how many types of bindings are there in WCF?

A binding defines how an endpoint communicates to the world. A binding defines the transport (such as HTTP or TCP) and the encoding being used (such as text or binary).

A binding can contain binding elements that specify details like the security mechanisms used to secure messages, or the message pattern used by an endpoint.

WCF supports nine types of bindings.

1. Basic binding:

Offered by the BasicHttpBinding class, this is designed to expose a WCF service as a legacy ASMX web service, so that old clients can work with new services. When used by the client, this binding enables new WCF clients to work with old ASMX services.

2. TCP binding:

Offered by the NetTcpBinding class, this uses TCP for cross-machine communication on the intranet. It supports a variety of features, including reliability, transactions, and security, and is optimized for WCF-to-WCF communication. As a result, it requires both the client and the service to use WCF.

3. Peer network binding:

Offered by the NetPeerTcpBinding class, this uses peer networking as a transport. The peer network-enabled client and services all subscribe to the same grid and broadcast messages to it.

4. IPC binding:

Offered by the NetNamedPipeBinding class, this uses named pipes as a transport for same-machine communication. It is the most secure binding since it cannot accept calls from outside the machine and it supports a variety of features similar to the TCP binding.

5. Web Service (WS) binding:

Offered by the WSHttpBinding class, this uses HTTP or HTTPS for transport, and is designed to offer a variety of features such as reliability, transactions, and security over the Internet.

6. Federated WS binding:

Offered by the WSFederationHttpBinding class, this is a specialization of the WS binding, offering support for federated security.

7. Duplex WS binding:

Offered by the WSDualHttpBinding class, this is similar to the WS binding except it also supports bidirectional communication from the service to the client.

8. MSMQ binding:

Offered by the NetMsmqBinding class, this uses MSMQ for transport and is designed to offer support for disconnected queued calls.

9. MSMQ integration binding:

Offered by the MsmqIntegrationBinding class, this converts WCF messages to and from MSMQ messages, and is designed to interoperate with legacy MSMQ clients.

9. What are the 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.

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

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

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

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

14. What are various ways of hosting WCF Services?

There are three major ways of hosting a WCF services 

1. Self-hosting the service in his own application domain. This we have already covered in the first section. The service comes in to existence when you create the object of Service Host class and the service closes when you call the Close of the Service Host class. 

2. Host in application domain or process provided by IIS Server

3. Host in Application domain and process provided by WAS (Windows Activation Service) Server. 

15. What is Transport and Message Reliability?

Transport reliability (such as the one offered by TCP) offers point-to-point guaranteed delivery at the network packet level, as well as guarantees the order of the packets. Transport reliability is not resilient to dropping network connections and a variety of other communication problems. 

Message reliability deals with reliability at the message level independent of how many packets are required to deliver the message. Message reliability provides for end-to-end guaranteed delivery and order of messages, regardless of how many intermediaries are involved, and how many network hops are required to deliver the message from the client to the service.

16. What is service and client in perspective of data communication?

A service is a unit of functionality exposed to the world. The client of a service is merely the party consuming the service.

17. What is Proxy and how to generate proxy for WCF Services?

The proxy is a CLR class that exposes a single CLR interface representing the service contract. The proxy provides the same operations as service's contract, but also has additional methods for managing the proxy life cycle and the connection to the service. The proxy completely encapsulates every aspect of the service: its location, its implementation technology and runtime platform, and the communication transport. 

The proxy can be generated using Visual Studio by right clicking Reference and clicking on Add Service Reference. This brings up the Add Service Reference dialog box, where you need to supply the base address of the service (or a base address and a MEX URI) and the namespace to contain the proxy. 

Proxy can also be generated by using SvcUtil.exe command-line utility. We need to provide SvcUtil with the HTTP-GET address or the metadata exchange endpoint address and, optionally, with a proxy filename. The default proxy filename is output.cs but you can also use the /out switch to indicate a different name. 

SvcUtil http://localhost/MyService/MyService.svc /out:Proxy.cs 

When we are hosting in IIS and selecting a port other than port 80 (such as port 88), we must provide that port number as part of the base address: 

SvcUtil http://localhost:88/MyService/MyService.svc /out:Proxy.cs

Saturday, 15 June 2013

Types of Bindings in WCF: Encoding Methods and Protocols

Types of Bindings in WCF: Encoding Methods and Protocols

Bindings in WCF define that how to communicate with the service. So, WCF Binding specifies that which communication protocol and encoding method should be used to communicate with the service.

WCF comes with a number of built-in bindings that we can use to expose our service, but WCF is extensible so we can define our own custom bindings to fulfill specific needs. WCF Binding can be BasisHttpBinding, WsHttpBinding, WsDualHttpBinding, WsFederationHttpBinding, NetNamedPipeBinding, NetTcpBinding, NetPeerTcpBinding, NetMsmqBinding. Lets look at them in detail:

Types of WCF Bindings

1. BasicHttpBinding is designed to replace ASMX Web services. It supports both HTTP and Secure HTTP. As far as encoding is concerned, it provides support for Text as well as MTOM encoding methods. BasicHttpBinding doesn’t support WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging.

2. WsHttpBinding also supports interoperability. With this binding, the SOAP message is, by default, encrypted. It supports HTTP and HTTPS. In terms of encoding, it provides support for Text as well as MTOM encoding methods. It supports WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging. By default, reliable sessions are disabled because it can cause a bit of performance overhead.

3. WsDualHttpBinding has all features of WsHttpBinding with addition that it supports Duplex MEP (Message Exchange Pattern). In this MEP, service can communicate with client via callback. Its basically a two way communication.

4. WsFederationHttpBinding is a specialized form of WS Binding that offers support for federated security.

5. NetNamedPipeBinding is secure and reliable binding on a single WCF computer across process communication. It provides support for binary encoding which is the best 

6. NetTcpBinding supports reliability, transactions and security. It also supports TCP protocol and binary as encoding method. We can say that it’s the most optimized or fastest binding because both client and service are on the same WCF technology.

7. NetPeerTcpBinding supports features as that of netTcpBinding but it provides secure binding for peer-to-peer environment with WCF Services.

8. NetMsmqBinding is required in a cross machine environment with secure and reliable queued communication. This uses MSMQ as transport.

Monday, 15 April 2013

What is the difference between Webservice and WCF in .NET?

What is the difference between Webservice and WCF in .NET?
 
If you are going for .NET technical interview and have mentioned WCF in your CV/Resume, I am 100% sure that you will be asked the question "What is the difference between webservice and WCF"? So be ready with this technical interview question.
 
WCF is a .NET 3.5 technology introduced by Microsoft. It unifies all communication options available in earlier versions of .NET framework. You can make a WCF application that would act exactly the same as a WebService, but it will be easier to implement and manage.
 
Here is the list of differences between Webservice and WCF
 
1. WCF has been designed by Microsoft from the scratch to comply with SOA (Service Oriented Architecutre). while Webservices do not comply with the Service Oriented Architecutre.
 
2. Webservice can only be hosted on IIS while WCF can be hosted on any server like WAS (Windows Activation Service), Self-hosting, Managed Windows Service.
 
3. WCF can be accessed via various protocols like SOAP over HTTP, Binary over TCP, Named Pipes, P2P, MSMQ etc. while webservices can only be accessed via HTTP. 
 
4. [WebService] attribute has to be added to the class and [WebMethod] attribute to be added to the method exposed to client in case of webservice while [ServiceContraact] attribute is added to the class and [OperationContract] attribute is added to the method exposed to client in case of WCF. 
 
5. System.Xml.Serialization namespace is used for serialization in webservice while System.Runtime.Serialization namespace is used for serialization in case of WCF.
 
6. WCF can be multithreaded via ServiceBehavior class while webservices cannot be multithreaded.

Tuesday, 15 May 2012

Basic Points of SOA (Service Oriented Architecture)

1. SOA stands Service Oriented Architecture.

2. SOA is not a specific technology, nor a specific language. It is just a blueprint, or a system design approach.

3. The key concepts of SOA are services, high interoperability and loose coupling.

4. SOA states that every component of a system should be a service, and the system should be composed of several loosely-coupled services.

5. A service means a unit of a program that serves a business process. A service is typically hosted on a remote machine (provider), and called by a client application (consumer) over a network. After the provider of a web service publishes the service, the client can discover it and invoke it. The communications between a web service and a client application use XML messages.

The client application is unaware of how the service is implemented, or of the signature that should be used when interacting with those services. The client application interacts with these services by exchanging messages. What a client application knows now is only the interfaces, or protocols of the services, such as the format of the messages to be passed in to the service, and the format of the expected returning messages from the service.

6. Web services are the most popular and practical way of realizing SOA. Each web service has a unique URL, and contains various methods. When calling a web service, you have to specify which method you want to call, and pass the required parameters to the web service method. Each web service method will also give a response package to tell the caller the execution results.

7. "Loosely-coupled"  means that these services should be independent of each other. A change to one service does not affect any other service. Also, the deployment of a new service does not affect any existing service. This greatly eases release management and makes agility possible.

8. As OOPs replaced various procedural techniques, SOA replaced various existing communication technologies like RPC, DCOM, and CORBA

Monday, 14 May 2012

WCF: A SOA based Service Framework


WCF stands for Windows Communication Foundation (Code Name: Indigo). Windows Communication Foundation (WCF) is a framework for building service-oriented applications. It is unified programming model provided in .Net Framework 3.0. WCF is meant for designing and deploying distributed applications under Service Oriented Architecture (SOA) implementation. WCF accomodates functionalities of its older communication technologies like:

1. Web Service (ASMX)
2. Web Service Enhancement (WSE)
3. Microsoft Message Queuing (MSMQ)
4. Component Object Model (COM+)
5. .Net Remoting

Components of WCF:

1. Service: A service is basically a class written in a .Net compliant language which contains some methods that are exposed through the WCF service. A service may have one or more endpoints – an endpoint is responsible for communication from the service to the client.

2. End Points: The End Points consists Address (Where), Contract (What) and Binding (How).

Address (Where): Indicates where a webservice could be found. Technically speaking, URL of the webservice.

Contract (What): Contract is an agreement between two or more parties. It defines the protocol how client should communicate with your service. Technically speaking, it describes parameters and return values for a web method in a web service.

Binding (How): Binding specifies what communication protocols are used to access the service, whether security mechanisms are to be used, and the like. WCF includes predefined bindings for most common communication protocols such as SOAP over HTTP, SOAP over TCP, and SOAP over Message Queues, etc. Interaction between WCF endpoint and client is done using a SOAP envelope. SOAP envelopes are in simple XML form that makes WCF platform independent.

The mnemonic "ABC" can be used to remember address / binding / Contract.

3. Hosting Environment: WCF can be hosted on IIS or on other environment (Self-hosting). There are two main advantages of using IIS over self-hosting:-

Automatic activation: IIS provides automatic activation that means the service is not necessary to be running in advance. When any message is received by the service it then launches and fulfills the request. But in case of self hosting the service should always be running.

Process recycling: If IIS finds any memory leaks etc in web service, IIS recycles the process.

Features of WCF:

Service Orientation: One consequence of using WS standards is that WCF enables you to create service oriented applications. Service-oriented architecture (SOA) is the reliance on Web services to send and receive data. The services have the general advantage of being loosely-coupled instead of hard-coded from one application to another. A loosely-coupled relationship implies that any client created on any platform can connect to any service as long as the essential contracts are met.

Interoperability: WCF implements modern industry standards for Web service interoperability.

Multiple Message Patterns: Messages are exchanged in one of several patterns. The most common pattern is the request/reply pattern, where one endpoint requests data from a second endpoint. The second endpoint replies. There are other patterns such as a one-way message in which a single endpoint sends a message without any expectation of a reply. A more complex pattern is the duplex exchange pattern where two endpoints establish a connection and send data back and forth, similar to an instant messaging program.

Service Metadata: WCF supports publishing service metadata using formats specified in industry standards such as WSDL, XML Schema and WS-Policy. This metadata can be used to automatically generate and configure clients for accessing WCF services. Metadata can be published over HTTP and HTTPS or using the Web Service Metadata Exchange standard.

Data Contracts: Because WCF is built using the .NET Framework, it also includes code-friendly methods of supplying the contracts you want to enforce. One of the universal types of contracts is the data contract. In essence, as you code your service using Visual C# or Visual Basic, the easiest way to handle data is by creating classes that represent a data entity with properties that belong to the data entity. WCF includes a comprehensive system for working with data in this easy manner. Once you have created the classes that represent data, your service automatically generates the metadata that allows clients to comply with the data types you have designed.

Security: Messages can be encrypted to protect privacy and you can require users to authenticate themselves before being allowed to receive messages. Security can be implemented using well-known standards such as SSL or WS-SecureConversation.

Multiple Transports and Encodings: Messages can be sent on any of several built-in transport protocols and encodings. The most common protocol and encoding is to send text encoded SOAP messages using is the HyperText Transfer Protocol (HTTP) for use on the World Wide Web. Alternatively, WCF allows you to send messages over TCP, named pipes, or MSMQ. These messages can be encoded as text or using an optimized binary format. Binary data can be sent efficiently using the MTOM standard. If none of the provided transports or encodings suit your needs you can create your own custom transport or encoding.

Reliable and Queued Messages: WCF supports reliable message exchange using reliable sessions implemented over WS-Reliable Messaging and using MSMQ.

Durable Messages: A durable message is one that is never lost due to a disruption in the communication. The messages in a durable message pattern are always saved to a database. If a disruption occurs, the database allows you to resume the message exchange when the connection is restored. You can also create a durable message using the Windows Workflow Foundation (WF).

Transactions: WCF also supports transactions using one of three transaction models: WS-AtomicTtransactions, the APIs in the System.Transactions namespace, and Microsoft Distributed Transaction Coordinator.

AJAX and REST Support: REST is an example of an evolving Web 2.0 technology. WCF can be configured to process "plain" XML data that is not wrapped in a SOAP envelope. WCF can also be extended to support specific XML formats, such as ATOM (a popular RSS standard), and even non-XML formats, such as JavaScript Object Notation (JSON).

Extensibility: The WCF architecture has a number of extensibility points. If extra capability is required, there are a number of entry points that allow you to customize the behavior of a service.

Relation between SOA and WCF

1. In SOA, a service must have End Points. WCF has all these end points: Address, Contract and Binding

2. Versioning of Services in WCF: In SOA, services can be versioned and you can host those services at new end points.

For example: You have a service named 'YourService' at end point “ep1”. Now you make enhancements in your service and launch a new service 'YourService2'. You can use it at another end point say "ep2". So the client who is consuming the service at end ep1 continues and at the other end, you have evolved your service by adding new ends ep2.

3. In SOA, the client who is consuming the service does not need to know how the implementation of the service is done. Services use Schemas to represent data and Contracts to understand behavior. They do not use language dependent types or classes in order to understand data and behavior. XML is used to define schemas and contracts.Same strategy is followed in WCF.

Advantages of WCF over older communication technologies:

1. WCF is interoperable with other services when compared to .Net Remoting, where the client and service have to be .Net.
2. WCF services provide better reliability and security in compared to ASMX web services.
3. WCF has integrated logging mechanism, changing the configuration file settings will provide this functionality. In other technology developer has to write the code.
4. WCF is faster than previous communication technologies by microsoft.
 

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.