Follow by Email

Saturday, April 13, 2013

WCF creation and consuming in to an application example

//WCF Demo Application 
Goto : File --> New Project --> WCF --> WCF service Application
Give a name to your WCF application and Click on OK :

1. Goto Solution Explorer open service1.svc.cs . if u want u can rename the file. Here we will declare the methods signature in the interface.


code :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1

{
   /*In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does.
   WCF defines four types of contracts:
   ==> Data Contracts
   ==> Fault Contracts
   ==> Message Contracts
   ==> Service Contracts
   Service contracts :
   1.Describe which operations the client can perform on the service.
 
     There are two types of Service Contracts :
     1.ServiceContract - This attribute is used to define the Interface.
     2.OperationContract - This attribute is used to define the method inside Interface.
   */

    [ServiceContract]

    public interface IService1
    {
        [OperationContract]
        int addNumbers(int number1, int number2);
    }
}

2. Now after defining the interface we will go the IService1.cs file and will write the method implementation for the defined method signatures.

//Describe method for addnumbers(int number1, int number2 ) in IService1.cs

using System;

using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1

{
    public class Service1 : IService1
    {
        public int addNumbers(int number1, int number2)
        {
            return (number1 + number2);
        }
    }
}



3. Build the application and check if there is no error. Now run the application and you should get this below screen. Here we can test our service by entering the values and checking the desired result. thus we can check that our WCF service is working correctly or not. Now will see how to consume this WCF service in our application.



4. I'm taking a console application where i will consume this service.

To add a service in to our application we need to have the URL of the service. Now go back to your WCF application and copy the address i.e the URL of the application as shown in below image.

Please note we need to keep running the WCF project if we want to consume the service in our application or else host WCF application into IIS.



Now in our main application in the solution explorer right click on the project and click on add service reference.




5. Add the copied URL in the service reference window and click on Go









Give whatever name you like to the service and then click on OK.


6.  Create the object for the service added and call the addNumbers Method. Now Run the Project and check the result !!!








Protected by Copyscape Duplicate Content Detection Tool