How to use WCF Services in application

Windows Communication Foundation (WCF) is a new framework  which was introduced in 3.0 .NET framework.It is framework which is used to build service -oriented applications in .NET. WCF supports unified programming model (framework).we can easily build secure reliable and integrated application across various platforms.
WCF Technology provides following main services:-
  • Message queuing 
  • .NET Remoting
  • Distribute Transactions
  • Web services in single services -oriented programming model for distributed application.
Web services is used to provide interoperability but WCF can used to  provide  interoperability as well as other programming models.You can know more differences between web services and WCF from here.
Terms of WCF
There are some main terms of WCF services in .NET.
End Point:- An end point is a resource on a network through which message can be sent.This service in WCF are responsible for enabling communication between the client and service.
  • Address:-An address is a location that define s where message can be sent.
  • Binding:-It specify protocol (way of communication mechanism )communication between WCF and client application.You already know, protocol is set of rules.
  • Contract:- It specify the service name being provide by WCF Services.
Types of Contracts used in WCF Services
1.) [Service contract]
This attribute must be specify about that type which will provide service to the client application.That service type can be interface or class.
2.) [Operation Contract]
This attribute must be define about that method which has to be exposed to the client application.
3.) [Data Contract] and [Data Member]
If there is a type (class ,structure or Enumeration) which will be a return type or parameter type for the service method.Data contract attribute must be defined about that type and Data Member must be defined about variable of properties of that type.
How to use WCF services in website
There are some steps to implement the WCF services on website which are given below:-
Step 1:-First open your visual studio --> File -->New -->Website-->Select WCF Service  as shown below -->OK.


Step 2:- Now open Solution Explorer window-->You will see following files as shown below:-


Step 3:Now Open IServices.cs file from solution Explorer -->Write the following codes as given below:-

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.
public interface Istudent_data
    string mywebsite();

 float rect_area(float v1,float v2);

    int multiplication(int p, int q);

    student GetData();
 // TODO: Add your service operations here
public class student
    public int id;
    public string sname;

Note:- In this file we declared the methods and variables of many services ( ex. rect_area , multiplication etc) .You can use it according to your requirements. 
Step 4:- Now open Service.cs file from solution explorer --> Write the following codes as given below:-

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Service : Istudent_data
    public string mywebsite()
        return "Welcome to my website:";
    public float rect_area(float v1, float v2)
        float area = v1 * v2;
        return area;
    public int multiplication(int p, int q)
        int res = p * q;
        return res;
    public student GetData()
        student obj = new student(); = 11;
        obj.sname = "Ram";
        return obj;

Note:-In this file we have defined the methods and variables which are already declared in Iservices.cs file.We have used this for security purpose. You  change the file name (Iservice.cs , Service.cs) ,which is mentioned in above codes.
Step 5:- Now Run the application (press F5)--> Copy the Browser's URL (for configuration of client application) as shown below:-


Step 6:- Now again open your visual studio -->File -->New -->Website--> Select ASP.NET Empty website -->OK --> open Solution Explorer --> Add a New Web Form (Default.aspx)--> Drag and drop Label,Text Box and Button control as shown below:-  


Step 7:Now open Solution Explorer --> Right click on project -->Add Service Reference as shown below:-


Step 8:- Now Paste the URL which i have copied in step 5 -->Now press Go Button -->Select Services as shown below-->press OK.


Step 9:- Now open web page (Default.aspx) -->write the codes the codes in Default.aspx.cs file on each button controls as Given below:-

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
    ServiceReference1.Istudent_dataClient obj = new ServiceReference1.Istudent_dataClient();
    protected void Page_Load(object sender, EventArgs e)
        string str = obj.mywebsite().ToString();
        Label1.Text = str;
    protected void Button1_Click(object sender, EventArgs e)
        float area1 = obj.rect_area(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
        Label2.Text = "Rectangle Area = " + area1;
    protected void Button2_Click(object sender, EventArgs e)
        int mult = obj.multiplication(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
        Label2.Text = "Multiplication of two Nubmer is= " + mult;
    protected void Button3_Click(object sender, EventArgs e)
        ServiceReference1.student st = obj.GetData();
        Label2.Text = "id is: " + + "and name is: " + st.sname;

Step 10:- Now Run the Application (press F5) -->Enter Required fields details then you will see output as showing below :- 


Note:- Here I have used WCF services in website.
For More...
  1. How to use web services in application
  2. How to implement caching concepts in application
  3. How to use different connection strings in application
  4. How to perform insert ,delete,update and print operations in Data List 
  5. How to use Cookies in application 
  6. Validation controls in
  7. How to send mail from application free
  8. How to add Run time controls in .NET
  9. Collection in c#
  10. How to host application on server free
  11. Exception Handling in c#
  12. How to create captcha image in application
  13. How to use Navigation controls in
  14. Web form controls in
  15. Create setup file from windows form application
  16. How to make Form based authentication in
Download Whole Attached file from below


Post a Comment

Powered by Blogger.