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;
        }
    }
}

No comments:

Post a Comment