1 Creating a Windows Phone 7 Application consuming data using a WCF Service The objective of this article is to create a WCF service that retrieves data from the database using LINQ to SQL classes and a Windows Phone 7 application that consumes that service to display the data. Introduction: The objective of this article is to create a WCF service that retrieves data from the database using LINQ to SQL classes and a Windows Phone 7 application that consumes that service to display the data. The article contains three main parts : 1. Creating the database 2. Creating the WCF Service 3. Creating the Windows Phone 7 application that consumes the WCF service The above mentioned scenario has been illustrated with an example. In the example I have created a table that contains some details about an employee (Employee ID, Employee Name and Phone No). When the user of our Windows Phone 7 application enters an employee id, he will be able to retrieve the details of that employee from the database through the WCF service. 1.-Creating the Database The following steps have been followed to create the database. 1. Open the SQL Server Management Studio and connect to the server. 2. Create a new database as shown below. (Right Click on the Database and select New Database)
19
Embed
Creating a Windows Phone 7 Application Consuming Data Using a WCF Service
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Creating a Windows Phone 7 Application consuming
data using a WCF Service
The objective of this article is to create a WCF service that retrieves data from the database using LINQ to SQL classes and a Windows Phone 7 application that consumes that service to display the data.
Introduction:
The objective of this article is to create a WCF service that retrieves data from the
database using LINQ to SQL classes and a Windows Phone 7 application that
consumes that service to display the data.
The article contains three main parts:
1. Creating the database
2. Creating the WCF Service
3. Creating the Windows Phone 7 application that consumes the WCF service
The above mentioned scenario has been illustrated with an example. In the
example I have created a table that contains some details about an employee
(Employee ID, Employee Name and Phone No). When the user of our Windows
Phone 7 application enters an employee id, he will be able to retrieve the details of
that employee from the database through the WCF service.
1.-Creating the Database
The following steps have been followed to create the database.
1. Open the SQL Server Management Studio and connect to the server.
2. Create a new database as shown below. (Right Click on the Database and
select New Database)
2
3. Give a name to the database (Here I have given the name as MyDatabase)
and click OK.
4. Now we can see our database (MyDatabase).
5. Now create a new table as shown below. (Right Click on table and select
New Table)
3
6. We have created a table that contains four columns
o EmpID (Primary Key) [nchar(10)] o EmpFirstName [nvarchar(50)] o EmpLastName [nvarchar(50)] o PhoneNo [numeric(10,0), Allow null]
Save the table (ctrl+s) and give it a name (In our case, it is MyEmployee)
4
7. Now some data are added to the table.
Now we have created our database.
2.-Creating the WCF Service
We have followed the steps given below to create the WCF Service.
1. Open the Visual Studio 2010 & create a new WCF Service Application. (In
our case, the name of the WCF service is MyService)
5
2. Right Click on the project name and then add a new item.
3. Now add a LINQ to SQL class to the project.
4. Now go to server explorer and add a new data connection. (Right click on
Data Connection and select Add Connection)
6
5. Give the server name, select the database and click test connection. Then
click OK.
6. Now from server explorer select your database and table and drag the table
to the middle pane.
7
7. Now open the IService1.cs and delete all the default codes. Write down the
following code there. I have explained the code later.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Collections.Generic;
namespace MyService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
List<MyEmployee> FindEmployee(string uid);
}
}
8
Explanation of the code :
The interface Iservice1 is the service contract of our WCF service. We have
declared only one function (FindEmployee) as our operation contract. This
function takes a string as an argument (which is the employee ID entered by
the user) and return a List of MyEmployee which is our data model class.
8. Now open the Service1.svc.cs and delete all the default codes. Write down
the following code there. I have explained the code later.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace MyService
{
public class Service1 : IService1
{
public List<MyEmployee> FindEmployee(string uid)
{
DataClasses1DataContext context = new
DataClasses1DataContext();
var res = from r in context.MyEmployees where r.EmpID == uid
select r;
return res.ToList();
}
}
}
9
Explanation of the code :
The class Service1 is our service that implements the service contract
IService1. In this class we have defined the operation contract
FindEmployee. In this method, we have created a data context object.
Then we have written a simple LINQ to SQL query that fetches the
details of a particular employee whose employee id was passed as an
argument of the operation contract. The method returns a list of objects of
MyEmployee class. (We could have returned only one object of
MyEmployee class also as we are fetching data using the primary key)
9. Right click on service1.svc and select the "view in browser" option.
10. Our service is running now (In Cassini server).
10
11. Copy the URL of the service.
3.-Creating the Windows Phone 7 application that consumes the WCF service
1. Open the Microsoft Visual Studio 2010 Express for Windows Phone and
create a Windows Phone Application. (In our case the name of the Windows
Phone 7 application is MyClientWin7)
11
2.
3. In MainPage.xaml drag and drop a TextBox and a Button as shown below.