WCF Basics

This article is about how to create and consume WCF Service

Basics of WCF Service

WCF- WCF stands for Windows Communication Foundation. It is a programming platform to build, configure and deploy network distributed Services. It is a combined feature of the Web Services, Remoting, MSMQ (Message Queuing) and COM+.

Since there are various ways to create Service in .NET Framework, then why WCF?

Why WCF?

WCF Service is flexible, as it can be hosted in IIS, Windows activation Services, Managed Windows Services and supports Self hosting. It supports HTTP, WS-HTTP, TCP, Custom, Named pipes, P2P, MSMQ etc. It can send/receive messages through any Transport protocol message format. By default, it uses SOAP for communication. It is more secure than ASP.NET Web Service. It doesn’t return unhandled exceptions to the client as SOAP faults.

Advantages

  1. WCF provides better security than ASMX Web Service.
  2. Small changes in the configuration are required to change the implementation of security model and change the binding.
  3. Integrated logging mechanism.
  4. Interoperability with the other Services.

End Point

Endpoint is a portal to communicate with the world. WCF exposes the collection of endpoints.

End point consists of three components, which are Address, Binding and Contract.

  1. Address- URL specifies where the Service is hosted. URL is used by the client to connect with our Service.
    e.g: http://localhost:62470/Service1.svc
    This URL will be used by the client to connect with our WCF Service Service1.

  2. Binding- Binding means how the client will communicate. We can mention the protocol type to be used for the communication, as we have various protocols for WCF to communicate.

Binding includes Transport, Encoding and Protocol.

Below is the list of protocols supported by WCF.

BindingDescription
BasicHttpBindingBasic Web Service communication. No security by default
WSHttpBindingWeb Services with WS-* support. Supports transactions
WSDualHttpBindingWeb Services with duplex contract and transaction support
WSFederationHttpBindingWeb Services with federated security. Supports transactions
MsmqIntegrationBindingCommunication directly with MSMQ Applications. Supports transactions.
NetMsmqBindingCommunication between WCF Applications by using queuing. Supports transactions.
NetNamedPipeBindingCommunication between WCF Applications on the same computer. Supports duplex contracts and transactions.
NetPeerTcpBindingCommunication between the computers across peer-to-peer Services. Supports duplex contracts.
NetTcpBindingCommunication between WCF Applications across the computers. Supports duplex contracts and transactions.

Contract

It specifies what the endpoint will communicate with the outside world. Interface name will be mentioned in the contract, so that it will make client aware of the operations that are exposed to the client.

Endpoint example
  1. <endpoint
  2. address="http://localhost:62470/Service1.svc" contract="ISumService"
  3. binding="wsHttpBinding"/>

Creating a WCF Service

We will create WCF Service and host it inside an ASP.NET Website.

Let’s begin by following the step by step procedure given below.
  1. Open Visual Studio and select New project. In the dialog box, select WCF -- > SCF Service Application.



  2. Let’s first create a Contract Class (IClass) :DataContract class.

     

    Service Contract



    In this interface, we created DataContract Class and ServiceContract.

    ServiceContract serves as the operations that will be available for client. Now, we will implement this into our class (Class), as shown below.

     

We have implemented the IClass and now our Service can add the two numbers, subtract the two numbers and multiply the two numbers. We have specified the Data Contract that our Service is exposing. We have also specified the Service contract and the operations of this Service.

Now, as our Service is ready with the interface and its implementation, we will now configure other things. We have to make this Service visible to our client and let the client Application use this service. We will add the Service behavior in web.config.

 

We have stated httpGetEnabled to true, which enables the Service. Now, if we build and run this, we will get the output, as shown below.

 

Consuming a Service

First of all, we will create an ASP.NET Website and add a service Reference.

 

It will create an address and binding part in the web.config, which are used to communicate with the Service.

Now, if we see the web.config of the Website, it will be, as shown below.

We will see that endpoint is defined in the web.config file.

A proxy class CalcArihmeticServiceClient has been generated when we added a Service reference. Now, we will use this proxy class to call the methods of the WCF Servce and then see the results.

We will us the methods of the proxy class, as shown below.

 

Now, we have done all the configuration and linking. We will now build and run this Website to check the results.

We got the interface, as shown below.

 

Now, we will pass the input and check the result. After passing the inputs, it will be, as shown below.

We got the desired results.This article was about the basics on how can we create and consume WCF Service.

Please provide your valuable feedback after going through this tutorial.