Windows Communication Foundation
- Windows Communication Foundation – WCF (Code named Indigo) is a programming platform and runtime system for building, configuring and deploying network-distributed services.
- It is the latest service-oriented technology; Interoperability is the fundamental characteristics of WCF.
- It is unified programming model provided in .Net Framework 3.0.
- WCF is a combined feature of Web Service, Remoting, MSMQ, and COM+.
- WCF provides a common platform for all .NET communication.
Advantages of WCF
- WCF is interoperable with other services when compared to .Net Remoting where the client and service have to be.Net.
- WCF services provide better reliability and security in compared to ASMX web services.
- In Windows Communication Foundation, there is no need to make much change in code for implementing the security model and changing the binding. Small changes in the configuration will make your requirements.
- WCF has integrated logging mechanism, changing the configuration file settings will provide this functionality. In other technology, the developer has to write the code.
A WCF Service is composed of three components parts viz,
Service Class – A WCF service class implements some service as a set of methods.
Host Environment – A Host environment can be a Console application or a Windows Service or a Windows Forms application or IIS as in case of the normal asmx web service in .NET.
Endpoints – All communications with the WCF service will happen via the endpoints. Also, The endpoint composed of 3 parts (collectively called as ABC’s of the endpoint) as defines below:
- Address: The endpoints specify an Address that defines where the endpoint hosted. It’s basically url.
- Binding: The endpoints also define a binding that specifies how a client will communicate with the service and the address where the endpoint hosted.
- Contract: The endpoints specify a Contract that defines which methods of the Service class will be accessible via the endpoint; each endpoint may expose a different set of methods.
Creating simple application using WCF (Demo of the Calculator Example)
Step By Step Procedure
- Creating WCF Service
- Consuming WCF Service in Web Application using Visual Studio
Creating WCF Service
- Open Visual Studio > Projects > Select C# > WCF > WCF Service Library.
- Type name of the service file as u need. In this ex .MyWCFSolution. then click OK.
- New Window will open with one Class.cs and Interface .cs File which can be seen in Solution Explorer.
- Moreover, Add the Namespace System.ServiceModel and also add Reference in Solution Explorer.
- Type the Code in Interface file and class file as shown below.
Consuming WCF Service in Web Application
Create Web Application as shown in attachment please download the File.
After Creating Design of the Calculator web application the add reference System. Service Model namespace into a web application in Solution Explorer.
Add the WCF Service in visual studio by right click on Reference and select Add Service Reference
Give the Service hosted Address and click on Go button and select the service which u created I mean in Ex service1.
Give the namespace: Name as My service and then click OK.
- Creating WCF Client.
- In Order to call the Methods (services from WCFService) we need to Create the proxy Windows Communication Foundation Client.
- Also, Once service is consumed in Application, WCF Client Created, So Using the name of the Client Name we can call the methods in out applications
- EX. In Our WCF Service we have 4 methods (add, sub, mul, div).
- To get the WCF client name Double click on Service i.e My service and take the copy of the Client name.
- Paste the Client Name by Default.cs File get the following code then create the instance of the client. Using this proxy client u can call the services of the WCF service.
- Moreover, On paste, u get me.My service.Service1Client
- Code to be Right in Default.cs in web application(EX: Calculator)