Blog from Saravanan Arumugam

Let us talk about Technologies

Category Archives: Computers and Internet

Default end point of WCF service


Default properties of WCF service

Recently I tried to create a WCF service manually without using WCF service project template in Visual Studio 2010. I found a strange nature in the service which was different from my usual understanding.

I followed the following steps.

1. Created a web project.
2. Added two class files, one for Service Contract Interface and another for the service definition.
3. Created a service file (.svc) with the definitions pointing to the contract and namespace I have created.

The only thing I left alone was the configuration. I didn’t add any end point to expose my service out.

To my understanding, from the recommendation of any book or article I read, a service must expose an end point either through configuration or through the code.

I wanted to see the behavior of the service without specifying an endpoint explicitly to it.

I ran the service and found that it’s hosted usually as any web application hosted from file system.

On selecting the .svc link, I could get the WSDL link.

I then created a client application to consume the service and to see if its also behaving properly. I added a service reference and gave the address of the WSDL.

It worked flawlessly.

 

From this exercise I figured out the following.

1. An endpoint is not a mandatory thing for a WCF service.

2. When we create a consumer that adds service reference, the consumer project/application adds an endpoint in the configuration.

3. The end point in the consumer points to an address such as http://localhost:12345/TaskManagerService.svc.

4. The consumer calls the service through basicHTTPBinding. Hence basicHTTPBinding looks to be the default binding that Visual studio arrange for the developers.

5. The address of the WSDL port number and the address stays constant how many ever times we restart the applications. This behavior is due to the Developer Server arrangement by the Visual Studio.

Advertisements

Visual Studio Development Server


When we work on a File-system based web site, the application is run on the localhost with a port number. E.g. http://localhost:12345/TaskManagerService.aspx.

This port number is a randomly chosen one when we run the web application.

Even when we restart the web application (restart the visual studio), this port number stays as is for every run. It usually doesn’t change. This is because of the following behavior of the Visual Studio.

 

The port number used to host the web application is stored in the project file (.csproj or .vbproj) under the tag, <DevelopmentServerPort>.

When a web application is initially created, this Development Server Port is initialized to zero. <DevelopmentServerPort>0</DevelopmentServerPort>

But when the application is run for the first time, an available port is randomly chosen and assigned to this web application. That’s how we get the default website address starting with http://localhost:12345.

The chosen port number is stored in the project file for any future runs. <DevelopmentServerPort>12345</DevelopmentServerPort>. During every run of the web application, Visual studio looks for a port number in the project file and sees if the port is free for use. If the port is available, the same port number is used for hosting the application.

 

Changing the default Development Server Port Number

For changing the port number follow these steps.

1. Open the .csproj or .vbproj file with a regular text editor (e.g. Notepad).

2. Browse for the tag <DevelopmentServerPort> and change the value in it.

or

1. In Visual Studio click on the Project Menu and Select <ProjectName> Properties…

2. Select the Web tab.

3. Under the Servers group, we would find the Use Visual Studio Development Server option.

4. Under this option we can select the Specific Port option and specify the port number of our choice.

image

Note: Though we have the option to chose the port number, there is no guarantee that the number of our choice would be available. So on running the application, it may likely be changed by the visual studio.

Read out loud Feature in Acrobat Reader


Using e-books is becoming more popular now a days. Almost all the technical books are available in the form of pdf. Even though conventional paperbacks are preferred by most users,  e-books are cheaper and easy to use. Introduction of sophisticated mobile devices increase the ease of use of e-books.

In the acrobat reader, traditional tool for reading e-books, we have the facility of having the computer read the e-book content for us (Accessibility feature). We can just follow what it speaks out and understand. Personally, this facilitated me reading the e-books quickly and made the reading fun.

I would like to narrate how to use the reading feature of Acrobat in this article.

Enable Read out loud feature

To enable the read out loud feature, in the acrobat reader, select the View menu > Read out loud > Activate Read out loud.

Alternatively try using the hotkeys: Control + Shift + Y.

Reading the e-book

Once the read out loud is turned on, we can,

  • Read to the End of the Document by pressing Control + Shift + B.
  • Read this page only by pressing Control + Shift + V.
  • Read a selected paragraph by clicking on the paragraph.
  • Reading can be paused or resumed by pressing Control + Shift + C .
  • Reading can be stopped by pressing Control + Shift + E.

Note: All the above can be done through the menu options: View > Read out loud

Configuring voices for Read out loud feature

Internally Acrobat reader uses the operating system’s internal feature called Text-To-Speech (TTS) engine to read the documents.

Text-To-Speech capability refers to the ability to play back text in a spoken voice.

For configuring the Read out loud feature in Acrobat reader follow these steps.

Step 1: Use Edit Menu > Preferences. (In Mac, we can find the preference in Acrobat Reader Application Menu)

Step 2: In the Category list, chose Reading.

Step 3: Under the Readout loud group box, Use default voice check box would be checked by default. Unchecking this would enable the selection of different voices.

Step 4: Select the voice of your choice in the drop down and click OK.

Each voice listed in the preference Drop down is a speech engine from operating system.

Configuring Speech Engines

Windows XP and Windows Vista offer a default Text-To-Speech engine, Microsoft Sam. Windows 7 comes with Microsoft Anna.

By default Text-To-Speech engine is turned on in Windows and it can be configured through “Speech properties” in Control panel. (In Windows 7, use Control Panel > Speech Recognition > Select Text-To-Speech on the left pane.)

Though Microsoft comes up with only one speech engine, it allows us to install additional 3rd party speech engines.

Note: Refer to How to configure and use Text-to-Speech in Windows XP and in Windows Vista for more detail on configuring Text-To-Speech engine.

Mac OS provides many such speech engines by default.

Refer to Apple’s Commitment to Accessibility for more detail.

Windows XP Icon Generation


Its always been a mystery how Microsoft Icons are made. Whenever we try with out traditional icon creation tools, we usually end up in minimal color bitmap kind of Icons or we have to google download some ready made icons from internet.

I recently came across an article from Microsoft (Though it was an old article, I saw it just some days ago) that was talking about Windows XP style icon creation.

Creating Windows XP Icons

This article primarily talks about using Photoshop as an image processing tool for creation of icons. It walks us through the theories of icon creation concept, decision of icon size, color etc.

I found it so useful for creating icons of my own. So I thought of publishing it for others to refer.

Rating Engine Implementation using Abstract Factory Pattern




I recently attended a code challenge contest in our company and won the contest. 

As a prize I was given the “WCF 4 Unleashed” book worth $40. 

 

The challenge in the contest was to implement a Rating engine with the Abstract Factory Pattern.  

I would like to present the Abstract Factory Implementation for the Rating Engine, that Ii wrote to win the prize over here. (This is supposed to rate the health care products – Medical/Dental) 

 

The underlying idea of Abstract factory pattern is that the implementations would be on the generic family of classes (Product, Rating Algorithm). 

The real classes being used would not be disclosed to the client. There would be a set of Factory classes to generate the specific concrete classes (which will be substituted on the places of Generic class declaration), based on the program scenario.

[http://www.dofactory.com/Patterns/PatternAbstract.aspx]

 

Following is the class diagram of the EightFingerGenie.ProductRating.RatingEngine namespace.

[Note: EightFingerGenie is the Organization name and ProductRating is the application that uses the RatingEngine]

 

image

 

Brief explanation on the diagram

Here the product factory is an abstract class which is capable of creating a product (Either Medical/Dental Product) when CreateProduct() method is called.

 

ProductFactory class is also responsible for deciding the rating algorithm to be followed for rating the products.

 

Product is the base class to represent either a medical or dental product.

 

It holds most of the definitions of the Product.

Medical and Dental product classes implement the Product Specific implementations. (In my implementation there are no such specific implementations)

 

Implementation of Abstract Factory Pattern

Before going in for explanation of different classes, let’s have a look at the implementation/use of Abstract Factory pattern assuming all the supporting classes (Shown in the diagram above) are available.

 

namespace EightFingerGenie.ProductRating
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine(“Processing quote with both medical and dental products…”);
            Quote comboQuote = GetComboQuote();
            comboQuote.RateThisQuote();

            Console.ReadLine();
        }

        private static Quote GetComboQuote()
        {
            Quote comboQuote = new Quote();

            MedicalProductFactory medicalFactory = new MedicalProductFactory();
            DentalProductFactory dentalFactory = new DentalProductFactory();

            Product[] products = {
                medicalFactory.CreateProduct(“Medical Product 1″),
                dentalFactory.CreateProduct(“Dental Product 2″),
                medicalFactory.CreateProduct(“Medical Product 3″),
                dentalFactory.CreateProduct(“Dental Product 4″)
            };

            //Rating Algorithm is made visible upto the consumer level in order to provide the
            //consumer a flexibility to chose the algorithm.
            Array.ForEach<Product>(products, delegate(Product p)
            {
                if (p.ProductType == AvailableProductType.Medical)
                    p.RatingAlgorithm = medicalFactory.GetRatingAlgorithm();
                else
                    p.RatingAlgorithm = dentalFactory.GetRatingAlgorithm();
            }
                    );

            comboQuote.Products.AddRange(products);

            return comboQuote;
        }

    }
}

 

In the code above, a Quote object can be imagined like a container to multiple Products (Quote object’s definition can be found under this paragraph).

At a high level, we can notice that in the implementation, the code is written to use Product class alone to contain both Medical and Dental products.

The code will use RatingAlgorithm class to represent any kind of RatingAlgorithm.

 

But the type of product and the type of algorithm to rate the product are all provided by the ProductFactory Classes.

 

Quote Class

The quote class holds a list of products, and some quote specific properties.

RateThisQuote() method rates every products present in the Quote class.

 

namespace EightFingerGenie.ProductRating
{
    public class Quote
    {
        #region Constructors

        public Quote()
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the list of products in the Quote
        /// </summary>
        public List<Product> Products
        {
            get { return products; }
        } private List<Product> products = new List<Product>();

        /// <summary>
        /// Gets the presence of Medical product in the Quote
        /// </summary>
        public bool HasMedical
        {
            get
            {
                if (this.products != null)
                    return this.products.Exists(delegate(Product p) { return p.ProductType == AvailableProductType.Medical; });
                else
                    return false;
            }
        }

        /// <summary>
        /// Gets the presence of Dental product in the Quote
        /// </summary>
        public bool HasDental
        {
            get
            {
                if (this.products != null)
                    return this.products.Exists(delegate(Product p) { return p.ProductType == AvailableProductType.Dental; });
                else
                    return false;
            }
        }

        #endregion

        #region Public Methods
        /// <summary>
        /// Rates all the products associated in the quote
        /// </summary>
        public void RateThisQuote()
        {
            if (this.products != null)
            {
                foreach (Product product in this.products)
                    Console.WriteLine(“Product: {0}\n\tResult: {1}\n”, product.ProductName, product.RateProduct());
            }
        }

        #endregion
    }
}

 

Product Class

This class represents the base product which is expected to be emitted by the ProductFactory.

This holds some Product Specific properties and RateProdut() method.

RateProduct() method uses the appropriate rating algorithm to rate the product, based on the category of the product (Medical / Dental).

using System;
using System.Collections.Generic;
using System.Text;
using EightFingerGenie.ProductRating.RatingEngine.Common;
using EightFingerGenie.ProductRating.RatingEngine.RatingAlgorithms;

namespace EightFingerGenie.ProductRating.RatingEngine.Products
{
    public abstract class Product
    {
        #region Constructors

        /// <summary>
        /// Creates an instance of a product
        /// </summary>
        /// <param name=”productName”>Name of the product</param>
        /// <remarks>
        /// Product instance cannot be constructed directly.
        /// This class can be instantiated only by its derived classes.
        /// Use the product factory classes to create a Product.
        /// </remarks>
        /// <exception cref=”ArgumentException”></exception>
        protected Product(string productName)
        {
            this.productName = productName ?? string.Empty;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets name of the product
        /// </summary>
        public string ProductName
        {
            get { return productName; }
            set { productName = value; }
        } private string productName;

        /// <summary>
        /// Gets the type of the product
        /// </summary>
        public AvailableProductType ProductType
        {
            get { return productType; }

            //Only the derived classes should be able to set the property.
            protected set { productType = value; }
        } private AvailableProductType productType;

        public RatingAlgorithm RatingAlgorithm
        {
            get { return ratingAlgorithm; }
            set { ratingAlgorithm = value; }
        } private RatingAlgorithm ratingAlgorithm;

        #endregion

        #region Public Methods

        /// <summary>
        /// Rates the current product
        /// </summary>
        /// <returns>Returns the result in the text form</returns>
        public string RateProduct()
        {
            return this.ratingAlgorithm.Rate();
        }

        #endregion
    }
}

 

MedicalProdcut and DentalProduct Classes 

With the general product specific properties specified in the base Product (abstract) class, MedicalProduct and DentalProduct class define the category specific properties (In the example, I have not implemented any special category specific property except the type of the Product). In the constructor of these classes, product type is set.

 

using System;
using System.Collections.Generic;
using System.Text;
using EightFingerGenie.ProductRating.RatingEngine.Common;

namespace EightFingerGenie.ProductRating.RatingEngine.Products
{
    public class MedicalProduct : Product
    {
        #region Constructors

        public MedicalProduct(string productName)
            : base(productName)
        {
            this.ProductType = AvailableProductType.Medical;
        }

        #endregion
    }
}

 

namespace EightFingerGenie.ProductRating.RatingEngine.Products
{
    public class DentalProduct : Product
    {
        #region Constructors

        public DentalProduct(string productName)
            : base(productName)
        {
            this.ProductType = AvailableProductType.Dental;
        }

        #endregion
    }
}

 

ProductFactory Class

This is an abstract class and it exposes two abstract methods.

CreateProduct() method is used for creation of a specific type of product. For example, MedicalProductFactory.CreateProduct() would automatically create a MedicalProduct.

 

namespace EightFingerGenie.ProductRating.RatingEngine.Products.Factories
{
    public abstract class ProductFactory
    {
        /// <summary>
        /// Creates a product
        /// </summary>
        /// <param name=”productName”>Name of the product.</param>
        /// <returns>Returns a product instance.</returns>
        public abstract Product CreateProduct(string productName);

        /// <summary>
        /// Produces a rating algorithm
        /// </summary>
        /// <returns>Returns a RatingAlgorithm instance</returns>
        public abstract RatingAlgorithm GetRatingAlgorithm();
    }
}

 

MedicalProductFactory, DentalProductFactory Classes

These are the classes that substitute the concrete family of classes on the generic implementation. MedicalProduct/DentalProduct is overlaid on the places of Product and MedicalRatingAlgorithm/DentalRatingAlgorithm is overlaid on the places of RatingAlgorithm.

namespace EightFingerGenie.ProductRating.RatingEngine.Products.Factories
{
    public class MedicalProductFactory : ProductFactory
    {
        public override Product CreateProduct(string productName)
        {
            return new MedicalProduct(productName);
        }

        public override RatingAlgorithm GetRatingAlgorithm()
        {
            return new MedicalRatingAlgorithm();
        }
    }
}

namespace EightFingerGenie.ProductRating.RatingEngine.Products.Factories
{
    public class DentalProductFactory : ProductFactory
    {
        public override Product CreateProduct(string productName)
        {
            return new DentalProduct(productName);
        }

        public override RatingAlgorithm GetRatingAlgorithm()
        {
            return new DentalRatingAlgorithm();
        }
    }
}

 

RatingAlgorithm Class

Finally, the RatingAlgorithm Family. For simplicity I have implemented just a Rate() method in the class.

 

namespace EightFingerGenie.ProductRating.RatingEngine.RatingAlgorithms
{
    public abstract class RatingAlgorithm
    {
        /// <summary>
        /// Rates the associated product
        /// </summary>
        /// <returns>Returns the result in the text form.</returns>
        public abstract string Rate();
    }
}

The derived classes of this class have to implement Rate() method on specific product types.

 

MedicalRatingAlgorithm/DentalRatingAlgorithm Classes

Again for simplicity they just output a string specifying the name of the algorithm.

 

namespace EightFingerGenie.ProductRating.RatingEngine.RatingAlgorithms
{
    public class MedicalRatingAlgorithm : RatingAlgorithm
    {
        public override string Rate()
        {
            return “Rated according to the medical rating algorithm”;
        }
    }
}

namespace EightFingerGenie.ProductRating.RatingEngine.RatingAlgorithms
{
    public class DentalRatingAlgorithm : RatingAlgorithm
    {
        public override string Rate()
        {
            return “Rated according to the dental rating algorithm”;
        }
    }
}

 

Output

The implementation of the abstract factory on Rating Engine outputs the following.

 image