Saturday, July 13, 2013

Consuming WCF Service with Android - Part 1

In this part of the tutorial we'll be creating a simple WCF Service using Visual Studio 2012 and hosting it on the local IIS. We'll be using the Library System Database. You can download SQL script files from the post.

Here is the list of some good tutorials on WCF for beginners:

  1. A Beginner's Tutorial for Understanding Windows Communication Foundation (WCF)

Creating a simple WCF service

Here is the code for IBookCatalog.cs. The code defines the OperationContract and DataContract

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace BooksCatalog
{
    [ServiceContract]
    public interface IBookCatalog
    {
        [OperationContract]
        Book GetBookDetailsByIsbn(string Isbn);

        // TODO: Add your service operations here
    }

    [DataContract]
    public class Book
    {
        [DataMember]
        public string Isbn { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string Author { get; set; }
        
        [DataMember]
        public string Genre { get; set; }

        [DataMember]
        public int Copies { get; set; }
    }
}
And here is the code for BookCatalog.svc.cs
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.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;

namespace BooksCatalog
{
    public class BookCatalog : IBookCatalog
    {
        private SqlDataReader Reader;

        public Book GetBookDetailsByIsbn(string Isbn)
        {
            Book book = new Book();
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LibrarySystem"].ConnectionString);
            SqlCommand cmd = new SqlCommand("SP_SearchByISBN", conn);

            try
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@ISBN", SqlDbType.NChar).Value = Isbn;
                cmd.Parameters.Add("@nCopies", SqlDbType.Int).Direction = ParameterDirection.Output;
              
                conn.Open();
                Reader = cmd.ExecuteReader();
                if (Reader.Read())
                {
                    book.Isbn = Reader["ISBN"].ToString();
                    book.Title = Reader["Title"].ToString();
                    book.Author = Reader["Author"].ToString();
                    book.Genre = Reader["Genre"].ToString();
                    book.Copies = int.Parse(Reader["Copies"].ToString());
                }
            }
            catch (SqlException ex)
            {
            }
            finally
            {
                if (Reader != null)
                    Reader.Close();

                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return book;
        }
    }
}

Library System Database


Here is the database diagram that I would be referring in most of my code.
And here are the SQL Script files for the Library System:

  1. Library System (No Data) 
  2. Library System (With Data)
Most of the time I would be referring to this database in my projects, and the files would be updated as required.

If you have any comments or requests you are welcome.

Starting with Android



I have been using Android for about 2 years, and being a software developer I always wanted to develop apps for mobile devices, specifically for the android platform. Now it's the right time to do that. I have recently taken a job that requires me to develop apps for android devices. This app is would be Finance ERP app that would be consuming WCF services.

I would be learning a lot on this job and during this course of learning, I would be sharing my experiences with you all. As I learn more concepts, I would post them as time permits.

Thanks all and happy coding