Wednesday, November 24, 2010

Create, Host(Self Hosting, IIS hosting) and Consume WCF servcie

Download SourceCode

What is WCF ?

Windows Communication foundation API allows to build distributed architecture in such manner that once created service code can be utilize by the in-house application which using tcp/ip to communicate, web application using http protocol to communicate to web services, application using msmq. WCF Services also works with the application developed other than .net framework.

Create WCF Service

Follow the below listed step to create Service

Step 1:
Create service from the Visual Studio select the go to c# or vb than select project type WCF Service library or WCf service website project template.

the first one create the *.dll and app.config file for after the compilation which you can than use in your host application and expose the service to clients.

the second one create the project with the proper web.config file and *.svc file which is get hosted on IIS via virtual directly and service get explose to client via HTTP protocol. so by that service can not be expose to tcp client. The benefit of this option is no need to create host application you can directly host it on iis via virtual directory.

For this discussion first I am creating WCF service library and than I am going to discuss about iis deployment also.

Step 2:
Create contract by specifing proper attributes. This contract tells the client what are the method get expose by the service and custom type expose by the service. You can decorate your contract by the list of attribute in System.ServiceModel namespace.

you can create you own files or after selecting proper project type its create two files for you Iservcie.cs and Service.cs. If you are going to create your own files its recommanded that you can create you interface and implement it in your class.

[ServiceContract] - defines the interface participate in provided service.

[OperationContract] - defines the method going to be expose by the service.

[DataMember] - defines the property expose via service.

[DataContract] - defines the custom data type expose via service.

After you done with the creation of the web service you code should look as below

Library app.config file
This file contains infomation about the metadata exchange defines endpoint of metadata. its also contains information about the endpoints where client talk with service.

Endpoints are ABC means Address, Binding and Contract. as you see in above config file service expose for tcp communication by following line

Binding details :

IService.Cs file


Hosting WCF Service
  • Self Host
  • IIS hosting
  • Windows service
  • WAS
Demo of Self hosting and IIS hosting.

Self hosting
 Step 1:
  Create new project >> Class Library >> name this project as Host

  Step 2:
  Add referance to the host application
  Right click on Referance >> Add Referance

Step 3:
After adding referance to the host application to expose webservice for client to consume
as you see below line of the code create the object of the host and which is of type Service1 Which we created in the WCF Service library project

as you can see we used using block over so that it depose host object when we close application.

Step 4:

to start the service to hadle request coming form the client of ther service  you need to call open method

and to stop receiving request you need to call close method


IIS hosting
  1. To host same WCF library in your application create WCF Web application project using WCF web application template as we discuss in start.
  2. Delete the files created in the IService.cs and Service.cs file from App_Code folder
  3. Include WCF library *.dll file which we created. 
  4. Open Service.svc file and modify the single line in it 

  5. Change the web service WEB.Config file to to expose service to client. i.e you need to create Same element as listed above in in the app.config file of service library.   
Consuming WCF Service

 Step 1:
 Create new project >> Class Library >> name this project as Client as we created Host application
     If you want to consume webservice hosted in you web application start directly for the 2 step listed below.

 Step 2:
 Add Service Referance to the client by Right click on project >> Add Service Referance

which atomatically create the proxy files to consume web service.
Make use of ServiceModel Metadata Utility Tool (Svcutil.exe)  the is command-line tool to generate proxy code from metadata.
   svcutil /t:code http://<service_url> /out:<file_name>.cs /config:<file_name>.config

 Step 3:
  To send request to service form the client

So as you see in above code you need to create object of Service1Client and than you can easily call the method expose by the Service. If you want to see the auto-generated code press F12 on the Service1Client it show the proxy code to you.

Summary :

As you follow the process listed process step by step its quite easy to create, consume and host WCF Service.


  1. Thank you! Got lot of concepts cleared after reading this article

  2. How to consume this WCF service using jquery

  3. Sathish - there is already article on my blog regarding consuming the WCF via Jquery you can check it....

  4. Thanks vry much for your article. Do you have an article for cross domain webservice

  5. Hola! I've been reading your web site for a long time now and finally got the bravery to go ahead and give you a shout out from Porter Texas! Just wanted to tell you keep up the good work!
    Also see my website - view

  6. How to consume this WCF service using jquery

  7. Hello, i think that i saw you visited my website thus i got here to
    go back the favor?.I'm trying to in finding issues to improve my web site!I guess its good enough to make use of a few of your ideas!!

    My blog post institution Johannesburg
    Also see my web site > make up artist Johannesburg

  8. Hello there, You have done a great job.

    I will definitely digg it and personally recommend to my friends.
    I'm confident they'll be benefited from this site.

    Also visit my web blog ... cellulite Urban

  9. I every time spent my half an hour to read this weblog's content every day along with a cup of coffee.

    Here is my blog post - more info

  10. It's amazing to visit this site and reading the views of all mates concerning this paragraph, while I am also eager of getting experience.

    Feel free to visit my webpage :: vexxhost feedback

  11. Wow, this piece of writing is pleasant, my sister is analyzing
    these kinds of things, thus I am going to let know her.

    Feel free to visit my web page :: visit link

  12. Very good post. I certainly love this site. Stick with it!

    Check out my web page :: more info

  13. Hi! I understand this is somewhat off-topic however I needed to ask.
    Does building a well-established blog like yours take a large amount of work?
    I'm completely new to operating a blog but I do write in my diary everyday. I'd like to
    start a blog so I can share my personal experience and feelings online.

    Please let me know if you have any kind of ideas or tips for
    brand new aspiring bloggers. Thankyou!

    my web-site; site

  14. Hi, of course this article is in fact pleasant and I have learned lot
    of things from it on the topic of blogging. thanks.

    Here is my web-site - recruitment agencies Gauteng

  15. I'm gone to tell my little brother, that he should also pay a visit this blog on regular basis to get updated from newest reports.

    Also visit my blog post; hip to waist ratio

  16. Just want to say your article is as surprising. The
    clarity for your put up is just nice and i can assume
    you are an expert in this subject. Fine together with your
    permission let me to grab your feed to stay updated with coming near near
    post. Thank you 1,000,000 and please carry on the rewarding work.

    Look at my website Different Jewellery

  17. For latest news you have to pay a visit internet
    and on internet I found this website as a best site for most up-to-date updates.

    My web blog ... windows xp registry cleaner

  18. Hi to all,-my Samsung ML 1410 now functions fine (Home windows 8 Pro 64 little bit).

    Thanks a lot for that excellent Study.

    My weblog: xerox phaser 8560 solid ink

  19. Hey there to all,-my Samsung ML 1410 now functions fine
    (Home windows 8 Pro 64 little bit). Many thanks a great deal for that great Research.

    My webpage - xerox phaser 8560mfp

  20. If you are going for best contents like I do, just visit this site all the time as
    it gives feature contents, thanks

    My blog post :: best diet pills

  21. Excellent, what a website it is! This web site presents helpful
    data to us, keep it up.

    my web site ... semenax

  22. I think that is one of the such a lot significant information for me.
    And i am glad studying your article. However want to observation on some general issues, The website style is ideal, the articles is in point of fact nice :
    D. Excellent activity, cheers

    Also visit my webpage :: vigrx for men

  23. fantastic submit, very informative. I'm wondering why the other specialists of this sector don't understand this.
    You should continue your writing. I am sure, you have a great readers' base already!

    My page - virility ex

  24. Do you have a spam problem on this site; I also am a blogger,
    and I was wondering your situation; we have created some nice procedures and
    we are looking to swap strategies with other folks, be sure to shoot me an email if interested.

    My site: human growth hormone

  25. Hi there! I could have sworn I've visited this site before but after looking at some of the posts I realized it's new to me.
    Nonetheless, I'm definitely delighted I came across it and I'll be book-marking it and
    checking back regularly!

    my homepage ... hoodia balance

  26. I'll right away take hold of your rss feed as I can not to find your e-mail subscription link or e-newsletter service. Do you've any?
    Please let me realize so that I may subscribe.


    Here is my web page :: thyromine

  27. I'm not sure why but this web site is loading very slow for me. Is anyone else having this issue or is it a problem on my end? I'll
    check back later on and see if the problem still exists.

    Feel free to surf to my web page :: male enhacment

  28. sir, please send the video to my email, its fantastic.

    Stop by my site ...

  29. Good post. I definitely love this site. Stick with it!

    Also visit my web page: buy genf20 plus

  30. hp laserjet p1505 printer drivers download.

    my web blog

  31. I was actually shocked to go through that this partnered with a Lexmark, but I
    Googled the E210 and it looks virtually identical
    to the Samsung ML-1210 ... So I suspect there are
    some inter-manufacturer resemblances that I was not aware

    Have a look at my webpage - xerox phaser 8560 maintenance kit

  32. Awesome issues here. I am very glad to see your article.
    Thank you so much and I'm taking a look forward to contact you. Will you please drop me a e-mail?

    Take a look at my web site breast actives testimonials

  33. Greetings! Very helpful advice in this particular post!
    It is the little changes which will make the biggest changes.

    Many thanks for sharing!

    My page; buy zetaclear

  34. say thanks to's wonderful idea, you are better than samsung's
    designer !!!

    my website xerox phaser 8560 maintenance kit

  35. Very nice article. I definitely appreciate this site.
    Continue the good work!

    Feel free to visit my webpage ... provillus cheap

  36. Hello to every body, it's my first visit of this web site; this website contains awesome and truly good stuff in favor of readers.

    my web site;

  37. Howdy this is kind of of off topic but I was wondering
    if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I'm starting a blog soon but have no coding skills so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!

    Also visit my blog post :: virility ex instructions

  38. I am really inspired with your writing skills as smartly as
    with the format in your weblog. Is this a paid subject matter or did you
    customize it your self? Anyway keep up the excellent quality writing, it is rare to look a great blog like this one nowadays.

    My web blog; buy genf20 plus

  39. Hey would you mind stating which blog platform you're working with? I'm planning to start my own
    blog soon but I'm having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I'm looking for something unique.

    P.S Apologies for being off-topic but I had to ask!

    Feel free to surf to my website - smoke deter

  40. You actually make it appear really easy together with your presentation but I in finding this matter to be really something which I believe I might
    never understand. It sort of feels too complex and very huge for me.
    I am having a look forward for your next put up,
    I'll attempt to get the hang of it!

    Look into my webpage :: venaro natural pearl

  41. Wow, fantastic blog layout! How long have you been blogging
    for? you make blogging look easy. The overall look of your site
    is great, as well as the content!

    Also visit my web-site :: breast actives

  42. I do accept as true with all the ideas you have presented on your post.
    They're really convincing and can definitely work. Still, the posts are too quick for novices. May just you please lengthen them a little from subsequent time? Thank you for the post.

    Here is my web-site - energizery

  43. Considering they've a boost of vitality from the fat burners, they'll utilize that during a
    workout session by doubling-up their exercises.

    This has been seen as to be a great late night snack because it will give you a good
    mix of proteins, carbs, in addition to healthy dietary fats that will help keep your blood glucose levels stable overnight.

    Unfortunately, for a few people, genetics plays the largest role
    in determining how easy it can be to lose weight
    for their bodies.

    Feel free to surf to my website: phen375 uk

  44. I'm really enjoying the design and layout of your site. It's a very easy on the eyes which makes it much more enjoyable for me to
    come here and visit more often. Did you hire out a developer to create your theme?
    Outstanding work!

    Feel free to visit my web blog :: Http://

  45. Wow, marvelous blog format! How lengthy have you ever been running a blog for?

    you made running a blog glance easy. The total glance of your
    web site is wonderful, let alone the content!

    my site - path of exile items

  46. Hello there, I discovered your site via Google whilst looking for a comparable matter, your web site got here up, it appears to be like
    good. I've bookmarked it in my google bookmarks.
    Hello there, simply become alert to your blog via Google, and located that it's really informative.
    I am going to be careful for brussels. I'll appreciate for those who continue this in future. A lot of other people might be benefited from your writing. Cheers!

    Also visit my weblog :: virility ex

  47. Great To Read Your Post!!! There are actually quite a few different types of Cheap Hosting services available today that will allow you to have your own domain name and host a website on the Internet. The type of web hosting that you need, including the amount of space and the specific functionality, will determine the amount of money that you’ll need to spend in order to be able to host your website.

  48. Best web hosting companies. Reviews, rates, statistics of top hosting companies.
    Find best hosting company at

  49. too many bots adding random responses. Its a shame

  50. what about self hosting wcf service with winodws service call from cross domain?????