How to use WCF Services in asp.net application

By
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 Asp.net website
There are some steps to implement the WCF services on asp.net website which are given below:-
Step 1:-First open your visual studio --> File -->New -->Website-->Select WCF Service  as shown below -->OK.


wcf_services

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

files

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.
[ServiceContract]
public interface Istudent_data
{
    [OperationContract]
    string mywebsite();

 [OperationContract]
 float rect_area(float v1,float v2);

    [OperationContract]
    int multiplication(int p, int q);

    [OperationContract]
    student GetData();
 // TODO: Add your service operations here
}
  [DataContract]
public class student
{
    [DataMember]
    public int id;
    [DataMember]
    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:http://www.msdotnet.co.in/";
    }
    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();
        obj.id = 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:-


URL FILE

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:-  


WEB_FORM

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


service_reference

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


service_references

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: " + st.id.ToString() + "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 :- 

output

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

0 comments:

Post a Comment

Powered by Blogger.