Overview
Here I will explain how to use WCF (windows communication foundation) service in web application using c#.net.
Creating simple application using WCF
Here I am going to do WCF sample to insert new record and display user details based on email for that first create table in your database like this
After that First open Visual Studio and click file —> Select New —> Website Under that select WCF Service and give name for WCF Service and click OK
Once you created application you will get default class files including Service.cs and IService.cs
Here IService.cs is an interface it does contain Service contracts and Data Contracts and Service.cs is a normal class inherited by IService where you can define all the methods and other stuff.
Now open IService.cs, check following namespaces and it not in file, add the following namespaces
After that write the following code in IService.cs file
After that open Service.cs class file and add the following namespaces
After that write the following code in Service.cs class file,
Now set the database connection string in web.config file because in above I am getting the connection string from web.config file
Our WCF service ready to use with basicHttpBinding. Now we can call this WCF Service method console applications
After completion of WCF service creations, publish or deploy your WCF Service in your system using F5.
After completion of deploy webservice now we can see how to use WCF Service in our Web application
Calling WCF Service using Web Application
To call WCF service we have many ways like using console app, windows app and web app.
Now I will explain how to use WCF service in web application.
Create new website from visual studio select New—> Website and give some name as you like.
After Creation Website now we need to add WCF reference to our web application for that right click on your web application and select Add Service Reference
Now one wizard will open in that give your WCF service link and click Go after add your service click OK button.
After completion of adding WCF Service first open Default.aspx page and write the following code
Now open Default.aspx.cs file and add following namespaces
After adding namespaces write the following code in codebehind
After completion of adding code check your endpoint connection for WCF Service reference that should be like this
Now everything is ready run your application.
Happy Coding..!! 🙂