Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Create and Consume wcf service in visual studio 2010 C#.net

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 316
    Comment on it

    Steps to Create a WCF service in Visual Studio 2010

    1.To create a new WCF service application project, go to File->New->Project. Then select WCF under Visual C# installed templates and then select wcf service application.

    2.In the solution explorer you will get two files in web service project

    i)Service1.svc

    ii)IService1.cs

    3.Add following code in IService.cs code file under ServiceContract attribute

        [OperationContract]
        string WelcomeMessage(string name);
    

    4.Add following code in Service1.cs code file in the class Service1 which is implementing IService1

            public string WelcomeMessage(string name)
        {
            return string.Format("Welcome {0}", name);
        }
    

    5.Now execute the wcf service by hitting F5 button

    To test the service you need to create a client and use it to call the service

    Steps to Consume a WCF service in Visual Studio 2010

    1.You will need to run svcutil.exe utility to generate the code files that will be later used in client consumer application.

    2.The svcutil.exe utility is installed with the windows SDK's and can be found at the location "C:\Program Files\Microsoft SDK's\Windows\vX.0A\bin", where X can be any latest installed SDK number.

    3.Now create a directory/folder to store the proxy files generated by svcutil utility. here i am creating a new folder named "WcfFiles" in c:\

    4.Open Command Prompt, goto C:\WcfFiles location and run the following command

    C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\svcutil.exe 'web service URL'

    The successful command execution will gives two files in the WcfFiles folder

    i)Service1.cs

    ii)Output.config

    5.Now create a Client or Consumer application, we create a new Console Application.

    6.Right click on the solution explorer and click on 'Add Service reference'.

    1. Add Service reference window will pop up, now we need to enter the url of our wcf service we have just created and click on go.

    2. Click on Ok to finish the add service reference wizard.

    9.Now copy the file Service1.cs from the location C:\WcfFiles and paste into your project.

    10.Add following code to the Program.cs file

                  namespace MyFirstWebService
                    {
                       Class Program
                          {
                             static void Main(string[] args)
                                {
                                   ServiceClient service=new ServiceClient();
                                   Console.WriteLine(service.WelcomeMessage("Rohit Grover"));
                                   Console.ReadLine();
                                }
                          }
                     }
    

    11.Save all files and hit F5 Button to execute the client application.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: