Docstoc

WCF - Syndication Services

Document Sample
WCF - Syndication Services Powered By Docstoc
					WCF – Syndication Services
Demo Script
Prepared by: [Demo/Script Owner(s)]                          Version: 1.1                       2007-11-14



                                                              Really Simple Syndication (RSS) is an XML document format used by Web sites to publish
                                                              frequently updated content, such as news headlines and blog posts. The distribution is called an
                                                              RSS feed. In most cases, the RSS feed is retrieved directly from a Web server via HTTP. The RSS
                                                              specification includes conventions that dictate how often feeds should be checked for updates.




Key Messages:
    1.   Simple http service development
    2.   JSON messaging capability
    3.   Simple syndication


Key Technologies:
The following technologies are utilized within this demo:

 Technology / Product                                       Version
     1.    Visual Studio 2008                               RTM

     2.    C# 3.0                                           (included with Visual Studio 2008)




Setup and Configuration
Before starting the demo, be sure to have all of the resources and code for this demo script installed. By default these files will be extracted to
C:\VS2008TrainingKit\Demos\14 - WCF - Syndication Services. After uncompressing the demo files, be sure to set up the environment as follows.



                    You need administrative privileges on the machine you will run this Demo Script to be enabled to
                    complete the exercises




Ensure installed components
    1.    Open the Control Panel and select Programs and Features.
    2.    Select Turn Windows features on or off.




                                                                                                                                                      2
3.   Check the Windows Communication Foundation HTTP Activation option inside the Microsoft .NET Framework 3.0 node and click the OK button.




                                                                                                                                               3
Open the provided solution
    1.   Open Visual Studio with administrative privileges
    2.   Open the provided solution file named PictureSyndication.sln

Create a Virtual Directory
    1.   Select the WebHost project. Then click the Project menu and select WebHost Properties.




    2.   Click the Web tab and then the Create Virtual Directory button.


Opening Statement
The power of Windows Communication Foundation is that you let the WCF layer to take care of the messaging plumber and you can focus on the functionally
of your application and host the service anywhere


Step-by-step Walkthrough
Estimated time for setting up and configuring the demo: 10 minutes.
Estimated time to complete the demo: 25 minutes.




                                                                                                                                                          4
Running a WCF project
Action                                           Script                                           Screenshot

1.   In the Browser, go to                          This demo is available on the web for you
     “www.cloudsamples.net/pictureservices”          to go there and play with it
     site
                                                    It is a service called picture services
                                                    The idea is that we take pictures from my
                                                     local machine and turn them into
                                                     syndication and expose that data in a REST
                                                     way

2.   In the top tool bar, go to Source Code ->      You can browse the source code
     Browse




3.   Show the code                                  If we browse, we are going to obtain this
                                                    I can see the source here




                                                                                                               5
4.   Switch to the previous browser window       I can even run it online, if you scroll to the
                                                  bottom and go here
5.   Scroll Down
6.   Click View Service Pictures Online




7.   Switch to Visual Studio                     First thing to look at in WCF application is
                                                  the service contract
8.   Open the IPictureService.cs inside the
     PictureSyndication project




9.   Show the GetPicture method                  We have the WebGet attribute set, and we
                                                  are defining the URI´s in a separate file
                                                 Notice that the return type of this method
                                                  is “stream”
                                                 We are getting a picture id and returning a
                                                  stream




                                                                                                   6
10. Press F5                                      I am going to press F5 to run the service
                                                  We see an html page




11. Show the WcfSvcHost pop-up that appears       The service is has been hosted by a new
                                                   hosting tool in the .net framework 3.5




12. Show the URL in the address bar from the      Is not this URL a bit different for what you
    browser                                        would expect to see for a service in a
                                                   browser?
                                                  It is html!, and is a WCF demo!

13. Switch back to VS                             We return a “stream”
14. Show the GetDocumentation method




                                                                                                  7
15. Open PictureService.cs inside                I get a MemoryStream
    PictureSyndication project to go to
                                                 I go ahead and create the text
    PictureService implementation
16. Show the GetDocumentation method             Set the stream to zero
                                                 Then I set my content-type
                                                 The I send back the stream
                                                 So you can send html through WCF
                                                 You can even render an ASP.NET page
                                                 So free your mind if you assume that the
                                                  WCF service output has to do just soap or
                                                  ws-*, it can do anything!

17. Go back to the GetDocumentation method       Let`s look at this more closely
    inside the IPictureServices.cs file
18. Right click on
    ServiceUris.GetDocumentation and select
    “Go To Definition”




19. Show the following code                      What would the URI be?

 public const string
 GetDocumentation = "help";




                                                                                              8
20. Show the browser URI                         So the URI is going to consist of navigating
                                                  to the base address and then slash help



21. Click the “Pictures” link in “Available      So, what about if I want to get the pictures
    Feeds” category                               from my local machine




22. Show the browser URI                         And here, we see a different URI




23. Make a the comments that are in the          The kind of cool thing here is that we are
    following cell                                retrieving pictures from windows desktop
                                                  search
                                                 We have got a service that is going out and
                                                  connecting to something local, like
                                                  windows desktop search, using a COM
                                                  object. We are interoperating with that
                                                  COM object
                                                 So we take that data and we serve it out
                                                  using a REST API. So COM is far from REST,
                                                  it is tightly bound to a file system and we



                                                                                                 9
                                                  are just taking that information an exposing
                                                  it over the web, that Is the idea.
                                                 Extend this to a document repository, etc
                                                 The power here is that you let the WCF
                                                  layer to take care of the messaging and you
                                                  can focus on the functionality of your
                                                  application and host this thing anywhere

24. Show the “Solution Explorer”                 I can take that assembly and reference it
                                                  from somewhere else.
25. Show the references from the WebHost
    project                                      So here I have built a web application that
                                                  references the picture services
26. Notice the PictureSyndication reference




27. Double click the Feed.svc file in the        I have added a .svc file
    WebHost project
                                                 I indicate where my service is going to be in
                                                  this .svc file




28. Show the Factory attribute                   What does it do?




                                                                                                  1
                                                                                                  0
29. Open the Web.config file                   How much service model stuff is in my
                                                web.config file?
30. Scroll through it
                                               Where is my service model configuration?
                                               It is not there, It is in the
                                                WebServiceHostFactory
                                               It has all the behaviors, use the right
                                                binding, etc

31. Set the WinHost project as a start-up      Another interesting thing is that I have a
    project                                     simple window host here
32. Press F5                                   We press F5




33. Go back to the browser                     So the hosting runtime in WCF is
                                                disconnected from the ASP.NET
34. Go to “http://localhost:8000/help”
    address                                    it does not have a dependency




                                                                                             1
                                                                                             1
JSON Services
Action                                         Script                                           Screenshot

35. In Solution Explorer, go to AJAX-Service      I will show you a really quick demo
    web application project




36. Open the AJAXService.cs file                  I have created this service




37. Show the DoWork method                        This method return information about the
                                                   picture provided by the “PictureServices”
                                                   service
                                                  We will try to consume it from the browser
                                                   using Javascript




                                                                                                             1
                                                                                                             2
38. Open the Markup view of Default.aspx          What I want to do is retrieve information
                                                   about this service from the SVC endpoint




39. Show the Service Reference inside the         How am I going to do that?
    Script Manager control
                                                  I have added the service reference down
                                                   here in the script manager
                                                  It says: I want to use this service in this
                                                   path
                                                  This is a relative path pointing to my svc file
                                                   that is in my directory




40. Go to <Script> tag inside the <head> tag      Now, what we can do is “var proxy = new
                                                   www.cloudsamples.net.PictureServices.AJA
41. Write “var proxy = new
                                                   XService();”
    www.cloudsamples.net.PictureServices.AJ
    AXService();” to show intelli-sense           Notice the JScript intelli-sense




                                                                                                     1
                                                                                                     3
42. In the next line, write “proxy.” (plus dot)      Now we have the proxy and look at the
    To show intelli-sense                             methods with intelli-sense
43. Then, delete the “proxy.” You have just
    coded. Just left the variable declaration.




44. Right click on the AJAX-Service project and      I am going to the web site .svc
    select “Set As StartUp project”
                                                     My AJAX service .SVC file, the normal
45. Right click on the AJAXService.svc file and       service endpoint
    select “Set As Start Page”
46. Press F5




47. Add “/js” to the URL in the browser              What Visual Studio is actually doing behind
    address bar                                       the scenes is navigating to a URL that has
                                                      got a “/js” at the end of it.
48. Save the file with a txt extension
                                                     WCF is waiting for that incoming request
                                                      that returns…




                                                                                                    1
                                                                                                    4
49. Open the file                               …the javascript proxy
50. Show the code                               So, the WCF has just created the proxy for
                                                 you and define the methods in this type
                                                So Visual Studio will catch this thing, and
                                                 then you can use it for intelli-sense.




51. END                                         So, all this is built-in, you do not have to do
                                                 a thing for it.


Syndication Services
Action                                       Script                                                Screenshot

52. In Solution Explorer, open                  So, I new-up syndication feed
    PictureService.cs file (in
    PictureSyndication project)                 I can add some extensions into it

53. Go to the “GetPictureListAsRss” method      I add syndication items to the feed, so I
                                                 take my feed and set its Items property to
                                                 an IEnumerable type
                                                Add some more stuff of extensions
                                                And then I new-up a formatter
                                                I can new-up a Rss feed formatter, an
                                                 Atom feed formatter
                                                It does not matter. Then I return it.
                                                That is all I do.


                                                                                                                1
                                                                                                                5
                                                This is all you have to do, new-up a feed,
                                                 add some items to it,
                                                And return it.

54. Open the App.config file (in                Now consuming a feed is a little more
    PictureSyndication project)                  interesting




55. Change the Picture default provider to      We have a FLICKR provider for picture
    “Flickr”                                     services


56. Comment the <add name=”wsd” attribute       So instead of retrieving pictures from my
                                                 local machine I can retrieve pictures from
57. Uncomment the <add name=”Flickr”
                                                 my FLICKR feed
    attribute
                                                So, what we are going to do in this example
                                                 is use the syndication API to go out,
                                                 connect to FLICKR and return to me the
                                                 pictures as if they were local




                                                                                               1
                                                                                               6
58. If the “WCF Service Host”, is not running,       So here is my help
    right click on PictureSyndication project
    and select Debug -> Start new instance




59. Click in the “Pictures” link in the browser      I am going to click in Pictures
    window




                                                                                        1
                                                                                        7
60. Show the web page                             This pictures are coming from FLICKR
                                                  But it looks like they are coming from local
                                                  So , all we have done is go out and
                                                   consume that feed and then redirected
                                                   from our local web site




61. In Solution Explorer, open the                I will show you the code that does this
    FlickrPictureProvider.cs file (in
    PictureProvider project and in Providers
    folder)




                                                                                                  1
                                                                                                  8
 62. Go to “GetPictures” method                        Basically all we do is call
                                                        SyndicationFeed.Load
 63. Show the method content
                                                       We pass it the URI and we get the feed




 64. END


Summary
You have seen how to host a WCF service, the new capabilities of the factory inside the SVC file that makes appropriate configuration of the service. You have
also seen how to consume the service from Javascript and how WCF creates that proxy for you, saving you to write a lot of code. Then we have seen some of
the syndication API that WCF provides to us.




                                                                                                                                                                 1
                                                                                                                                                                 9

				
DOCUMENT INFO