In this tutorial we will create a web service, which will be hosted by the Host, and the invoking of the web service in client application.
The Database "EmployeeDB" has to be made with the following structure.
The following steps are to be followed to create a Web Service Project.
- Open Microsoft Visual Studio 2008.
- Click File -> New Project -> Choose Visual C# from the Left pane.
- Choose Web and from the templates available, choose WCF Service Application.
- For our tutorial we will use the name of the project as ServiceDB.
- Create a connection to the database "EmployeeDB".
- Add one Linq to SQL Classes from the templates; name it as "DataClasses1.dbml".
- Add the above tables to the designer of the Linq to SQL.
- In the class file "IService1.cs" add your custom method as [Operation Contract]. Add the following code to the interface “IService1”.
- //Custom Method
- [OperationContract]
- string AllEmployees();
-
In "Service1.svc.cs" add the above method's implementation.
- public string AllEmployees()
- {
- DataContext db = new DataContext(@ "Data Source=DIPTI\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");
- Table < Employee > emp = db.GetTable < Employee > ();
- var query = from c in emp select c;
- string result = "<EmployeeDB>";
- foreach(var e in query)
- {
- result += "<Employee><EmpId>" + e.EmpId +
- "</EmpId><FirstName>" + e.FirstName +
- "</FirstName><LastName>" + e.LastName +
- "</LastName><Email>" + e.EmployeeInfo.Email +
- "</Email><Address>" + e.EmployeeInfo.Address +
- "</Address><Phone>" + e.EmployeeInfo.Phone +
- "</Phone></Employee>";
- }
- result += "</EmployeeDB>";
- return result;
- }

Jasbeer SinghPosted Feb 6, 2017, 12:47 AM
Thanks vishal for valuable feedback
Jasbeer SinghPosted Feb 6, 2017, 12:46 AM
Thanks Rakesh for feedback
Vishal PrajapatiPosted Feb 5, 2017, 10:54 PM
Nice one..Easy to understand..
RakeshPosted Feb 5, 2017, 1:03 PM
Good Explain Jasbeer Singh.........................