SIGN UP MEMBER LOGIN:    
ARTICLE

Consume a Custom WCF Service in a Console Application

Posted by Vijai Anand Articles | WCF with C# January 10, 2012
In this article you will see how to consume the custom WCF service in a Console Application.
Reader Level:

In this article you will see how to consume the custom WCF service in a Console Application.

SQL database details

WcfCnsl1.jpg

Employee_Details table has the following data:

WcfCnsl2.jpg

I have created a custom WCF service and hosted it in IIS http://localhost:9002/EmployeesService/EmployeesService.svc which is used to fetch the data from the above SQL database table for the specified id.

Syntax

public Employee_Detail getEmployeebyId(int empId)


Return Value

Type: EmployeesWCFService.Employee_Detail

You will get the following result for the specified id 102:

WcfCnsl3.jpg


Example

Create a Console application


Steps Involved

  1. Open Visual Studio 2010 by going to Start | All Programs | Microsoft Visual Studio 2010 | Right click on Microsoft Visual Studio 2010 and click on Run as Administrator.
  2. Go to File tab, click on New and then click on Project.
  3. In the New Project dialog box, expand the Visual C# node, and then select the Windows node.
  4. In the Templates pane, select Console Application.
  5. Enter the Name and then click OK.
  6. In the Solution Explorer, right click on the solution and then click on Properties.
  7. Select the Application tab, check whether ".Net Framework 3.5" is selected for Target Framework.
  8. Select the Build tab; check whether "Any CPU" is selected for Platform Target.
  9. In the Solution Explorer, right click on the References folder and click on Add Service Reference.
  10. The Add Service Reference wizard will pop up.
  11. In the Address section, enter the WCF service URL and then click on Go button.

    WcfCnsl4.jpg
     
  12. Enter the proper name for the Namespace.
  13. Click on Ok.
  14. Service Reference will be added successfully.
  15. Replace Program.cs with the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ConsumeCustomWCF.EmployeesServiceReference;
    using System.Net;

    namespace ConsumeCustomWCF
    {
        class Program
        {
            static void Main(string[] args)
            {
                EmployeesServiceClient serviceClient = new EmployeesServiceClient();
                EmployeesServiceReference.Employee_Detail emp = serviceClient.getEmployeebyId(102);
                Console.WriteLine(emp.EmpId);
                Console.WriteLine(emp.EmployeeName);
                Console.WriteLine(emp.EmployeeDesignation);
                Console.WriteLine(emp.Dept);
                Console.WriteLine(emp.WorkLocation);
                Console.ReadLine();
            }
        }
    }

     

  16. Build the solution.

  17. Hit F5.

  18. Output:

    WcfCnsl5.jpg

Summary

Thus in this article you have seen how to consume a custom WCF service in a Console application.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks everyone:-)

Posted by Vijai Anand Jan 11, 2012

great work.

Posted by Akshay Teotia Jan 11, 2012

Hi Vijay it's a great effort to accomplish such task keep it up thanks for sharing.....

Posted by Amit Maheshwari Jan 11, 2012

Nice article.

Posted by Abhi Kumar Jan 10, 2012

keep posting

Posted by Sonakshi Singh Jan 10, 2012
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor