Wednesday, November 24, 2010

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

Shout it kick it on DotNetKicks.com
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 : http://www.c-sharpcorner.com/UploadFile/pjthesedays/bindwcf05032009012251AM/bindwcf.aspx


IService.Cs file



Service.cs


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
      


Host.cs



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.
or
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.

58 comments:

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

    ReplyDelete
  2. How to consume this WCF service using jquery

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

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

    ReplyDelete
  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

    ReplyDelete
  6. How to consume this WCF service using jquery

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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


    Check out my web page :: more info

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

    my web site ... semenax

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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.

    Thanks.

    Here is my web page :: thyromine

    ReplyDelete
  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

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

    Stop by my site ... http://ateljeskogslyckan.blogspot.com/2012/04/blakulla-ja-men-da-var-det-dags-igen.html

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


    Also visit my web page: buy genf20 plus

    ReplyDelete
  30. hp laserjet p1505 printer drivers download.

    my web blog http://galilee.ezpt.kr/

    ReplyDelete
  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
    of.

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

    ReplyDelete
  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

    ReplyDelete
  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

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

    my website xerox phaser 8560 maintenance kit

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

    Feel free to visit my webpage ... provillus cheap

    ReplyDelete
  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; snorezip.herbalcurenow.com

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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://Forum.Vigsys.net/

    ReplyDelete
  45. Hi there, its good post about media print, we all understand media is a great source of
    information.

    Here is my webpage; e-cigs

    ReplyDelete
  46. Fuh ... done it. Put up Samsung Universal motorist and windows
    8 considered it much better motorist, though it never operated.
    It can be deleted trough the Control panel -) Programs and something.
    After that just comply with Aaron write-up. Fun ... Many thanks !
    !! ).

    Also visit my site; http://s152875474.onlinehome.us/david/?p=179&cpage=195

    ReplyDelete
  47. It worked for my Samsung ML-1210. Thanks. I was about to select
    a brand-new printer because of Windows 8.

    my page xerox phaser 8560mfp driver (http://www.tsclearning.catholic.edu.au)

    ReplyDelete
  48. I have the trouble solved on printing the labels.
    Somehow, I acquired a leading frame on page one that was
    non-standard (smaller sized) than the other web pages. This was throwing every label off.
    I could possibly not find a method of erasing the first page just, so I
    reduced and pasted each address into a brand-new label file.
    The result was fantastic and the top margin on all the web pages
    were fine.

    Feel free to surf to my web blog - xerox phaser 8560 error codes
    - http://www.concordia-Saoleo.com.br -

    ReplyDelete
  49. 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

    ReplyDelete
  50. 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

    ReplyDelete
  51. Hi there, I desire to subscribe for this weblog to take newest updates,
    thus where can i do it please assist.

    Also visit my webpage :: xerox 8560 drivers

    ReplyDelete
  52. Really informative article. Nevertheless, the cost color and multifunction colour
    laser printers need to have been attended to. They're still more costly compared to inkjet printers although worth the extra expenses.

    my blog post - xerox phaser 8560 [sofiamiller.blogspot.co.uk]

    ReplyDelete
  53. Very good website you have here but I was curious if you knew of any user
    discussion forums that cover the same topics talked about
    here? I'd really love to be a part of community where I can get responses from other knowledgeable people that share the same interest. If you have any suggestions, please let me know. Cheers!

    my blog: victorinox Canada

    ReplyDelete
  54. Wonderful blog! I found it while browsing on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I've been trying for a while but I never seem to get there! Thank you

    Look into my web blog :: Briggs & riley

    ReplyDelete
  55. Hello! I know this is kinda off topic but I was wondering
    which blog platform are you using for this site?
    I'm getting fed up of Wordpress because I've had problems with
    hackers and I'm looking at alternatives for another platform. I would be fantastic if you could point me in the direction of a good platform.

    Here is my blog ... xerox 8560 phaser

    ReplyDelete
  56. Definitely consider that which you stated. Your favorite justification seemed to be at the net the
    simplest factor to take into accout of. I say to you, I certainly get
    annoyed whilst other people consider worries that they plainly don't know about. You controlled to hit the nail upon the highest and outlined out the whole thing without having side effect , other people could take a signal. Will likely be back to get more. Thank you

    My webpage ... xerox 8560 yellow

    ReplyDelete
  57. 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.



    ReplyDelete
  58. Best web hosting companies. Reviews, rates, statistics of top hosting companies.
    Find best hosting company at HostingCompaniesz.com

    ReplyDelete