Wednesday 19 September 2012

How to create wcf service in asp.net



 
In .Net framework 3.0, Microsoft introduced foundation concepts WCF (Windows Communication Foundation) is one of the foundation technology concept by Microsoft with .Net framework 3.0
  • WCF is an extension of XML Web Services
  • Using WCF application, we can develop a Web services, that means WCF application is a web application which is provides services to other applications over the Internet. So, finally WCF concept is providing a facility to a .Net programmer to develop a advanced technology web service.
  • Microsoft is providing a separate template to develop a WCF application from .Net framework 3.0 onwards.
  • WCF service will provide more security than traditional web services in .Net
  • WCF service application can be consumed by all the type of applications and all the technology applications
  • In .Net WCF application implementation will be divided into 2 phases:

1)      Creating a WCF service
2)      Creating a client application and consuming WCF service

System.ServiceModel is the base namespace for WCF application

Using WCF service application template we can develop a WCF service

By default WCF service application will come with 2 code windows

1)      IService1.cs
2)      Service1.svc

  • Extension of WCF application is .svc
  • WCF service application will provide one interface called Iservice1, which will contain some default members and this interface is representing with name called IService1.cs

                        class service1: Iservice1
{         
---
}

Interface Iservice1
{
            //default members
}



Ø  This service1 class is representing in a file called Service1.svc
Ø  Iservice1 interface and service1 class will come with some default members. If we required we can also use that members in our development
Ø  When we are developing WCF service application the development process can be divided into  2 steps
o   According to our requirement we have to declare the required member functions and properties within the Iservice1 interface
o   All the declared members we have to define(implement) within the service1 class, then build the application

Contracts:

Ø  WCF services are exposing with contracts
Ø  Contracts will define what services a client can consume
Ø  Contracts are nothing but attributes
Ø  Contracts are:
1)      [Service Contract]: It will be exposing with interface level

2)      [Operation Contract]: It will be exposing with method level

3)      [Data Contract]: It will be exposing with custom define data types

4)      [Data Member]: It will be exposing with fields, properties and events

Note: When we are declaring a member within the Iservice1.cs file, we have to expose with the concern contract.
Ø  If we didn’t mention the concern contract for a method or property, our WCF service will not include that member as a part of application

Step by Step process to create a WCF service application and Creating a client applicaton and consuming the WCF service:

Ø  Open a WCF service application using Visual studio
Ø  You can identify 2 code windows within the solution explorer
Ø  Open the Iservice1.cs file and declare the members according to our requirement
                        // Todo: Add your service operations here

[Operation contract]

int Add(int a, int b)
           
            [Operation contract]

                        string Hello(string name);

int Multiply(int a, int b)   - Here not adding contract
Step 2: Goto solution explorer and open the service1.svc file. In that file, we will have a class called service1.

            - Implement my members

                        public int Add(int a, int b)
                        {
                                    return a+b;
                        }
                       
                        public string  Hello(string name)
                        {
                                    return “welcome” + name;
                        }

                        public int Multiply(int a, int b)
                        {
                                    return a*b;
                        }
- Now build the application


Step3: Creating client application

            - Opening a windows application using Visual Studio
            - Name it as wcfclient
            - Design the windows form as for your requarement.

here i have taken text boxes for calculating the numbers.


Step 4: Adding service references

Goto Solution Explorer à Select References à Right click à Add Service References

                                                It will open Add service reference window

Go back to WCF service application and run it à Ok

Browser window will display the

            Copy the URL of the WCF service

Comeback to client application and paste that URL within the address textbox of Add Service Reference Window

            Click the Go button

            Click Ok

            Your Solution explorer will display the WCF service reference file

Step 5: Creating object for WCF proxy class

            (Write after Initialize component( ) )

                        Servicereference1.serviceclient wcfobj = new
                                                                           wcfclient.servicereference1.serviceclient( )

Step 6: Write the below code within the btnAdd_click event procedure

                        private void btnAdd_click(--)
                        {
                                    textResult.Text=wcfobj.Add(Covert.Toint32(textNum1.Text),
                                                                                    Convert.Toint32(textNum2.Text)).ToString( );
                        }

            Write the below code within the btnSubmit_click

                        private void btnsubmit_click(--)
                        {
                                    textGreetings.Text=wcfobj.Hello(textName.Text);
                        }

            Build the application
 Note: if you have any doubts in this article please drop a comment,i will try to resolve .

No comments: