Läs på engelska

Dela via


Gör så här: Skapa en grundläggande atomfeed

Med Windows Communication Foundation (WCF) kan du skapa en tjänst som exponerar ett syndikeringsflöde. I det här avsnittet beskrivs hur du skapar en syndikeringstjänst som exponerar ett atomsyndikeringsflöde.

Skapa en grundläggande syndikeringstjänst

  1. Definiera ett tjänstkontrakt med hjälp av ett gränssnitt som markerats med attributet WebGetAttribute . Varje åtgärd som exponeras som en syndikeringsfeed ska returnera ett Atom10FeedFormatter objekt.

    [ServiceContract]
    public interface IBlog
    {
        [OperationContract]
        [WebGet]
        Atom10FeedFormatter GetBlog();
    }
    

    Anteckning

    Alla tjänståtgärder som gäller WebGetAttribute mappas till HTTP GET-begäranden. Om du vill mappa åtgärden till en annan HTTP-metod använder du i WebInvokeAttribute stället. Mer information finns i How to: Create a Basic WCF Web HTTP Service (Skapa en grundläggande WCF-webb-HTTP-tjänst).

  2. Implementera tjänstkontraktet.

    public class BlogService : IBlog
    {
        public Atom10FeedFormatter GetBlog()
        {
            SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"), "FeedOneID", new DateTimeOffset(DateTime.Now));
            feed.Authors.Add(new SyndicationPerson("someone@microsoft.com"));
            feed.Categories.Add(new SyndicationCategory("How To Sample Code"));
            feed.Description = new TextSyndicationContent("This is a sample that illustrates how to expose a feed using ATOM with WCF");
    
            SyndicationItem item1 = new SyndicationItem(
                "Item One",
                "This is the content for item one",
                new Uri("http://localhost/Content/One"),
                "ItemOneID",
                DateTime.Now);
    
            SyndicationItem item2 = new SyndicationItem(
                "Item Two",
                "This is the content for item two",
                new Uri("http://localhost/Content/Two"),
                "ItemTwoID",
                DateTime.Now);
    
            SyndicationItem item3 = new SyndicationItem(
                "Item Three",
                "This is the content for item three",
                new Uri("http://localhost/Content/three"),
                "ItemThreeID",
                DateTime.Now);
            List<SyndicationItem> items = new List<SyndicationItem>();
    
            items.Add(item1);
            items.Add(item2);
            items.Add(item3);
    
            feed.Items = items;
            return new Atom10FeedFormatter(feed);
        }
    }
    
  3. Skapa ett SyndicationFeed objekt och lägg till en författare, kategori och beskrivning.

    SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"), "FeedOneID", new DateTimeOffset(DateTime.Now));
    feed.Authors.Add(new SyndicationPerson("someone@microsoft.com"));
    feed.Categories.Add(new SyndicationCategory("How To Sample Code"));
    feed.Description = new TextSyndicationContent("This is a sample that illustrates how to expose a feed using ATOM with WCF");
    
  4. Skapa flera SyndicationItem objekt.

    SyndicationItem item1 = new SyndicationItem(
        "Item One",
        "This is the content for item one",
        new Uri("http://localhost/Content/One"),
        "ItemOneID",
        DateTime.Now);
    
    SyndicationItem item2 = new SyndicationItem(
        "Item Two",
        "This is the content for item two",
        new Uri("http://localhost/Content/Two"),
        "ItemTwoID",
        DateTime.Now);
    
    SyndicationItem item3 = new SyndicationItem(
        "Item Three",
        "This is the content for item three",
        new Uri("http://localhost/Content/three"),
        "ItemThreeID",
        DateTime.Now);
    
  5. Lägg till objekten i SyndicationItem feeden.

    List<SyndicationItem> items = new List<SyndicationItem>();
    
    items.Add(item1);
    items.Add(item2);
    items.Add(item3);
    
    feed.Items = items;
    
  6. Returnera feeden.

    return new Atom10FeedFormatter(feed);
    

Värd för tjänsten

  1. Skapa ett WebServiceHost objekt.

    Uri baseAddress = new Uri("http://localhost:8000/BlogService/");
    WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), baseAddress);
    
  2. Öppna tjänstvärden, läs in feeden från tjänsten, visa feeden och vänta tills användaren trycker på RETUR.

    svcHost.Open();
    Console.WriteLine("Service is running");
    
    XmlReader reader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog");
    SyndicationFeed feed = SyndicationFeed.Load(reader);
    Console.WriteLine(feed.Title.Text);
    Console.WriteLine("Items:");
    foreach (SyndicationItem item in feed.Items)
    {
        Console.WriteLine("Title: {0}", item.Title.Text);
        Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Content).Text);
    }
    Console.WriteLine("Press <ENTER> to quit...");
    Console.ReadLine();
    svcHost.Close();
    

Så här anropar du GetBlog() med en HTTP GET

  1. I en webbläsare bläddrar du till följande URL: http://localhost:8000/BlogService/GetBlog

    URL:en innehåller basadressen för tjänsten (http://localhost:8000/BlogService), slutpunktens relativa adress och den tjänståtgärd som ska anropas.

Anropa GetBlog() från kod

  1. Skapa en XmlReader med basadressen och den metod som du anropar.

    XmlReader reader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog");
    
  2. Anropa den statiska Load(XmlReader) metoden och skicka in den XmlReader du nyss skapade.

    SyndicationFeed feed = SyndicationFeed.Load(reader);
    

    Detta anropar tjänståtgärden och fyller i en ny SyndicationFeed med formateren som returneras från tjänståtgärden.

  3. Få åtkomst till feedobjektet.

    Console.WriteLine(feed.Title.Text);
    Console.WriteLine("Items:");
    foreach (SyndicationItem item in feed.Items)
    {
        Console.WriteLine("Title: {0}", item.Title.Text);
        Console.WriteLine("Summary: {0}", ((TextSyndicationContent)item.Summary).Text);
    }
    

Exempel

Följande är den fullständiga kodlistan för det här exemplet.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.Xml;
using System.ServiceModel.Description;
using System.ServiceModel.Syndication;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;

namespace Service
{
    [ServiceContract]
    public interface IBlog
    {
        [OperationContract]
        [WebGet]
        Atom10FeedFormatter GetBlog();
    }

    public class BlogService : IBlog
    {
        public Atom10FeedFormatter GetBlog()
        {
            SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"), "FeedOneID", new DateTimeOffset(DateTime.Now));
            feed.Authors.Add(new SyndicationPerson("someone@microsoft.com"));
            feed.Categories.Add(new SyndicationCategory("How To Sample Code"));
            feed.Description = new TextSyndicationContent("This is a sample that illustrates how to expose a feed using ATOM with WCF");

            SyndicationItem item1 = new SyndicationItem(
                "Item One",
                "This is the content for item one",
                new Uri("http://localhost/Content/One"),
                "ItemOneID",
                DateTime.Now);

            SyndicationItem item2 = new SyndicationItem(
                "Item Two",
                "This is the content for item two",
                new Uri("http://localhost/Content/Two"),
                "ItemTwoID",
                DateTime.Now);

            SyndicationItem item3 = new SyndicationItem(
                "Item Three",
                "This is the content for item three",
                new Uri("http://localhost/Content/three"),
                "ItemThreeID",
                DateTime.Now);
            List<SyndicationItem> items = new List<SyndicationItem>();

            items.Add(item1);
            items.Add(item2);
            items.Add(item3);

            feed.Items = items;
            return new Atom10FeedFormatter(feed);
        }
    }

    public class Host
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8000/BlogService/");
            WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), baseAddress);
            try
            {
                svcHost.Open();
                Console.WriteLine("Service is running");

                XmlReader reader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog");
                SyndicationFeed feed = SyndicationFeed.Load(reader);
                Console.WriteLine(feed.Title.Text);
                Console.WriteLine("Items:");
                foreach (SyndicationItem item in feed.Items)
                {
                    Console.WriteLine("Title: {0}", item.Title.Text);
                    Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Content).Text);
                }
                Console.WriteLine("Press <ENTER> to quit...");
                Console.ReadLine();
                svcHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                svcHost.Abort();
            }
        }
    }
}

Kompilera koden

När du kompilerar föregående kod refererar du till System.ServiceModel.dll och System.ServiceModel.Web.dll.

Se även