Create WCF service
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.
1) Open Microsoft Visual Studio 2008.
2) Click File -> New Project ->
3) Choose Visual C# from the Left pane.
4) Choose Web and from the templates available, choose WCF Service Application.
5) For our tutorial we will use the name of the project as ServiceDB.
6) Create a connection to the database "EmployeeDB".
Initial Preparation of the Project in the solution:-
Delete IService1.cs and Service1.svc, which we get by default at the time of project creation.
Download Microsoft Sample Database – PUBS from HERE, else we can create our own database for operation.
Extract the Database, Open the SQL file (.sql) of Pubs database -> instpubs.sql in the VS 2010, once opened -> Right click anywhere on the text zone and select -> Execute SQL (this creates our sample database).
Now in Server Explorer (view –> server explorer) of VS 2010, Right Click -> Data Connections, Select -> Add Connection.
In the open Dialog, Select –> ServerName (typically SQLEXPRESS), and also Select -> Database Name from the DropDownList (pubs).
CLick OK. With this, the database part of the project is ready.
7) Add one Linq to SQL Classes from the templates; name it as "DataClasses1.dbml".
To add LINQ to SQL classes to a project
From within a Visual Basic or C# application, on the Project menu, click Add New Item.
Click the LINQ to SQL Classes template.
Change the name to Northwind.dbml.
Click Add.
The Northwind.dbml file is added to the project and the Object Relational Designer (O/R Designer) opens.
In Server/Database Explorer, under Northwind, expand Tables and drag the Customers table onto the Object Relational Designer (O/R Designer).
A Customer entity class is created and appears on the design surface.
Repeat step 6 for the Orders, Order_Details, and Products tables.
Right-click the new .dbml file that represents the LINQ to SQL classes and click View Code.
8) Add the above tables to the designer of the Linq to SQL.
9) 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();
10) 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;
}
The following steps are to be followed to create a Client Application based on the Web Service. Here we will create a Windows application.
1) Open Microsoft Visual Studio 2008.
2) Click File -> New Project ->
3) Choose Visual C# from the Left pane.
4) Choose Windows and from the templates available, choose Windows Forms Application.
5) For our tutorial we will use the name of the project as "ClientApplication".
6) Run the Web Service application and copy the URL of the service.
7) Add Service reference to the URL. Choose the name of the service reference as "ServiceReference1".
8) In the Form's design window add the following controls to see the data from the database.
a. Button as btnView
b. Data Grid View as dataGridView1
c. Binding Source as bindingSource1
9) In the click event of the Button add the following code.
private void btnView_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client obj = new Service1Client();
string list = obj.AllEmployees();
DataSet DS = new DataSet();
StringReader sr = new StringReader(list);
DS.ReadXml(sr, XmlReadMode.InferSchema);
DS.AcceptChanges();
bindingSource1.DataSource = DS.Tables[0];
this.dataGridView1.DataSource = bindingSource1;
}
To run the application the following steps has to be followed.
1) Run the Service Application. (Don't close the service application)
2) Run the Client Application.
3) Click the Button to see the results.
No comments:
Post a Comment