Tuesday, January 20, 2015

How to host wcf service in IIS 7 or 7.5

This article will give you brief description about required steps to host your wcf service in IIS and test it using console application.

For creating and hosting WCF service in IIS follow below steps.

  1. Create WCF service

    Create a new service using Create new WCF service library and test using WCFTestClient
  2. Add Service Host

    Add service host for hosting Product Service by right clicking on Project from solution explorer. Name the service host as ProductServiceHost.svc
    WCF Service host for Product Service
  3. ServiceHost tag

    Add below service host tag to ProductServiceHost.svc. Give fully qualified name of service to ServiceHost attribute.
                <%@ ServiceHost Service="NorthwindServices.ProductService" %>
                

  4. Create Web Site in IIS

    Open IIS manager by clicking Windows start -> Run -> enter inetmgr -> click ok If IIS is not installed on your machine click here to install.
    Go to IIS manager and right click on sites and select Add Web site.
    Add new web site to IIS

    Enter details as shown in below picture
    • Enter site name as NorthwindServices
    • Change the application pool to ASP.net V4.0
    • Select the physical path of folder containing ProductServiceHost.svc
    • Enter port number on you wish to host service.
    Add new web site to IIS
  5. Publish Services

    Go to your service application and right click on service project and select publish.
    Publish WCF service to IIS
    Enter target location as http://localhost:7741/
  6. Test the WSDL

    Open your browser and enter http://localhost:7741/ProductServiceHost.svc. You will see the link for WSDL.
    WCF service to IIS
  7. Add Client Application

    Now add console application to solution. Name it as NorthwindApp. WCF Client Application
  8. Add Service Reference

    Add service reference to NorthwindApp by right click on NorthwindApp project and click on Add Service Reference. Below window should appear. Enter the address of service endpoint which you have added in service app.config. WCF Client Application

    Enter ProductServiceRef for Namespace and click on OK. Service reference is added to your client application.

    WCF Client Application

    Check Client application's App.config, it should have service endpoint and client details.
  9. Client Implementation

    Service reference is now available for Northwind. We can call service operations. Add the below code to NorthwindApp -> Program.cs file.
    class Program
    {
        static void Main(string[] args)
        {
            ShowOperations();
        }
    
        private static  void ShowOperations()
        {
            ConsoleKeyInfo cki;
    
            do
            {
                Console.WriteLine(); 
                Console.WriteLine("Enter Product ID or press Esc to quit : ");
                cki = Console.ReadKey();
                if (!char.IsNumber(cki.KeyChar))
                {
                    Console.WriteLine("  Invalid number");
                }
                else
                {
                    Int32 number;
                    if (Int32.TryParse(cki.KeyChar.ToString(), out number))
                    {
                        Console.WriteLine(); 
                        GetProductName(number);
                        GetProductQty(number);
                        GetCategoryName(number);  
                    }
                    else
                    {
                        Console.WriteLine("Unable to parse input");
                    }
                }
            } while (cki.Key != ConsoleKey.Escape);
    
            Console.Read(); 
        }
    
        private static void GetProductName(int ProductID)
        {   
            ProductsClient client = new ProductsClient();
            string ProductName = client.GetProductName(ProductID);
            Console.WriteLine(string.Format("Product name {0} for Product ID {1}",
                    ProductName, ProductID));
        }
    
        private static void GetProductQty(int ProductID)
        {   
            ProductsClient client = new ProductsClient();
            int ProductQty = client.GetProductQty(ProductID);
            Console.WriteLine(string.Format("Product qty {0} for Product ID {1}",
                    ProductQty, ProductID));
        }
    
        private static void GetCategoryName(int ProductID)
        {   
            ProductsClient client = new ProductsClient();
            string CategoryName = client.GetCategoryName(ProductID);
            Console.WriteLine(string.Format("Category name {0} for Product ID {1}",
                        CategoryName, ProductID));
        }
    }
                

See more details about Hosting WCF service in IIS with SSL and Transport Security

Download source code with Service Library, Service Host and client application.

No comments:

Post a Comment

Image noise comparison methods

 1. using reference image technique     - peak_signal_noise_ratio (PSNR)     - SSI 2. non-reference image technique     - BRISQUE python pac...