Saturday, 13 July 2013

Walkthrough: Getting Started with WCF

You can see lot of wcf uses and definition in the online, here I am not going to say anything about that a simple example how to write wcf service.

Follow the below instruction and images to write a simple wcf service and hosting it and calling in client.

Step .1 Creating WCF Application


  • Open visual studio and create new wcf project as shown

  • In the Iservice.cs interface add the below [Operation Contracts] declaration as below.




  • In the service.cs file add the definition for the [Operation Contracts]. Here I am doing some arithmetic operations like adding, sub ration, multiplying and dividing.


  • Now right click in the app.config and click on the 'Edit WCF Configuration' .


  • A WCF Editor window will appear and default the endpoints will be 'http' there are many end points in wcf, you can see that by Binding row. Here we are going have net.tcp binding to do that expand service tree and then your project tree and then click on the end point 1 and change as below image.

  • Then click on the end point 2 and change the binding row as 'mex tcp binding'

  • Now expand the advance node and make http enable as false from the service behavior meta data like below.

  • Now change the URL from 'Http' to 'TCP' url like below image.

  • Save the window and close now open your app.config it will be like below. note if you change the class names in the application it should changes in your app.config file also


  • Now Run your WCF application you will see the running notification like below.



    • Now you will see a window like below which has all the function you have written. you can test the function like below.


    Step. 2 Hosting The WCF Application in Windows Services

    • Add New Windows Service Project.

    • In windows service click on the Service.cs file and add installer then you will see project installer file with serviceprocessinstaller1 and serviceprocessor. change the property of serviceprocessorinstaller1 Account to 'NetworkService'. refer below images.



    • Now Refer the WCF Project to your window service and system.servicemodel under reference in your application like below image.




    • Now add using system.servicemodel in you service.cs code behind. and command the codes in the program.cs file and create your service.cs like below images. this code will start the wcf service which we refer in the windows service.


    • Now our WCF host is ready.
    Step .3 Create Installer for WCF Host

    • Add new Setup Project like below and refer our wcf host windows service to it..Add Primary output and custom action. follow below image to do that.







    • Now Right click on the Setup Project and click on the 'Install' then follow the install instruction and complete the installation.

    • Now go to service.msc and start the window service normally it will be service.1 if you didn't change the service name and right clikc on it and start now our wcf service starts.
    Step. 4 Creating Client application calling the WCF service

    • Create any client application and here I have created winform application with client window like below.

    • Now add Service Reference using the URL we provided in WCF application. follow the below images.


    • Now call your wcf service function like below code behind here I added on all button click.




    • Test Your application.


    • .Cheers ;-)
    Thus you created a WCF service using net.tcp protocol.



    No comments:

    Post a Comment