How to Connect Windows Phone With WCF Service

These are the follwing steps we need to follow to connect windows  phone with wcf service 1.Creation of service 2.Hosting the service in IIS

3.Connecting from the windows phone

step 1:

>> First we need to create a wcf service.and put it in the D drive.Lets name it as WinPhoneWcfService. For the connectivity to DataBase we need to add an

ADO.Net Entity Data Model.

>>Then in the IService.cs we need to declare a method

[OperationContract]
     List UserDetails();

 
>>Then in the Service.cs we need to define this Method

public List UserDetails()
   {
       List lsdUserInfoDetails =
                               new List();
       string[] allUserList;

       try
       {
           userDataConnection userContext = new
                                              userDataConnection();

           var query = (from Info in userContext.UserInfo
                          select Info).ToList();

           foreach(var userdetails in query)
              {
                  allUserList = new string[8];
                  allUserList[0] = userdetails.UserName.ToString();
                  allUserList[1] = userdetails.UserCity.ToString();
                  allUserList[2] = userdetails.UserState.ToString();
                  allUserList[3] = userdetails.UserGender.ToString();
                  allUserList[4] = userdetails.UserAge.ToString();
                  allUserList[5] = userdetails.UserDescription.ToString();

                  lsdUserInfoDetails.Add(allUserList);   
              }

           
       }
       catch (Exception es)
       {
           
       }

       return lsdUserInfoDetails;
   }

 
Then we need to build the service

step 2:

Then we need to Host it in the IIS

>> First we have to create a virtual directory
lets name it as WinPhoneService

>>Then we need to convert it to application

>>Then we need to enable the Directory Browsing

>>after browsing it we will get the Service refernce

step 3:

Now its time to connect it from the Windows Phone

>>First we need to create new project named it as UserApp

>> Suppose we want to display the user details so we need to add a ListBox

>>In the MainPage.xaml add the following code

   
       
           
               
                   
                       
                           
                       
                   
               
           
           

               
           

           
               
                   
                       
                   
               
               
                   
                       
                           
                           

                           
                           

                           
                           

                           
                           

                           
                           

                           
                           

                       
                   
               
           
       
   
     

   >>In the MainPage.xaml.cs we need to add the following code

       
   public partial class MainPage : PhoneApplicationPage
   {
      List userList = new List();

       // Constructor
       public MainPage()
       {
           InitializeComponent();
            
           
            UserServiceReference.ServiceClient client = new ServiceClient();
            
             //Event handler after webservice completes operation.
            client.UserDetailsCompleted += new EventHandler                                           

                            (serviceClient_UserDetailsCompleted);
            client.UserDetailsAsync();

       }    
   }

   //Completed event of the UserDetails
   public void serviceClient_UserDetailsCompleted(Object sender,UserDetailsCompletedEventArgs e )
       {
            try
           {

               var lsdUser = e.Result.ToList();

               foreach (var userData in lsdUser)
               {
                   
                   User userObj = new User();
                   userObj.UserName =userData[0].ToString();
                   userObj. userCity = userData[1].ToString();
                   userObj. userState=userData[2].ToString();
                   userObj. userGender=userData[3].ToString();
                   userObj. userAge=userData[4].ToString();
                   userObj. userDescription=userData[5].ToString();
                   
                   userList.Add(userObj);

               }

               //Binding Data to the userListBox
               userListBox.ItemsSource = userList;

           }
           catch (Exception ex)
           {
               string message = ex.ToString();
           }

       }







   /// 
   /// User class
   /// 
   public class User
   {

       private string userName;
       private string userID;
       private string userState;
       private string userCity;
       private string userGender;
       private string userAge;


       public string UserID
       {
           get
           {
               return userID;
           }
           set
           {
               userID = value;
           }
       }

       public string UserName
       {
           get
           {
               return userName;
           }
           set
           {
               userName = value;
           }
       }

       public string UserState
       {
           get
           {
               return userState;
           }
           set
           {
               userState = value;
           }
       }

       public string UserCity
       {
           get
           {
               return userCity;
           }
           set
           {
               userCity = value;
           }
       }

       public string UserAge
       {
           get
           {
               return userAge;
           }
           set
           {
               userAge = value;
           }
       }

       public string UserGender
       {
           get
           {
               return userGender;
           }
           set
           {
               userGender = value;
           }
       }

   }
150 150 Burnignorance | Where Minds Meet And Sparks Fly!