Vorgehensweise: Erstellen eines grundlegenden Atom-Feeds

Windows Communication Foundation (WCF) ermöglicht Ihnen die Erstellung eines Diensts, der einen Syndication-Feed verfügbar macht. In diesem Thema wird erläutert, wie ein Syndication-Dienst erstellt wird, der einen Atom-Syndication-Feed verfügbar macht.

So erstellen Sie einen grundlegenden Syndication-Dienst

  1. Definieren Sie einen Dienstvertrag mit einer Schnittstelle, die mit dem WebGetAttribute-Attribut gekennzeichnet ist. Jeder Vorgang, der als Syndication-Feed verfügbar gemacht wird, sollte ein Atom10FeedFormatter-Objekt zurückgeben.

    [ServiceContract]
    public interface IBlog
    {
        [OperationContract]
        [WebGet]
        Atom10FeedFormatter GetBlog();
    }
    
    Bb412177.note(de-de,VS.100).gifHinweis:
    Alle Dienstvorgänge, auf die das WebGetAttribute-Attribut angewendet wird, werden HTTP GET-Anforderungen zugeordnet. Wenn Sie den Vorgang einer anderen HTTP-Methode zuordnen möchten, verwenden Sie stattdessen WebInvokeAttribute. Weitere Informationen finden Sie unter Vorgehensweise: Erstellen eines grundlegenden WCF-Web-HTTP-Diensts.

  2. Implementieren Sie den Dienstvertrag.

    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 illistrates how to expose a feed using ATOM with WCF");
    
            SyndicationItem item1 = new SyndicationItem(
                "Item One",
                "This is the content for item one",
                new Uri("https://localhost/Content/One"),
                "ItemOneID",
                DateTime.Now);
    
            SyndicationItem item2 = new SyndicationItem(
                "Item Two",
                "This is the content for item two",
                new Uri("https://localhost/Content/Two"),
                "ItemTwoID",
                DateTime.Now);
    
            SyndicationItem item3 = new SyndicationItem(
                "Item Three",
                "This is the content for item three",
                new Uri("https://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. Erstellen Sie ein SyndicationFeed-Objekt, und fügen Sie einen Autor, eine Kategorie und eine Beschreibung hinzu.

    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 illistrates how to expose a feed using ATOM with WCF");
    
  4. Erstellen Sie mehrere SyndicationItem-Objekte.

    SyndicationItem item1 = new SyndicationItem(
        "Item One",
        "This is the content for item one",
        new Uri("https://localhost/Content/One"),
        "ItemOneID",
        DateTime.Now);
    
    SyndicationItem item2 = new SyndicationItem(
        "Item Two",
        "This is the content for item two",
        new Uri("https://localhost/Content/Two"),
        "ItemTwoID",
        DateTime.Now);
    
    SyndicationItem item3 = new SyndicationItem(
        "Item Three",
        "This is the content for item three",
        new Uri("https://localhost/Content/three"),
        "ItemThreeID",
        DateTime.Now);
    
  5. Fügen Sie die SyndicationItem-Objekte dem Feed hinzu.

    List<SyndicationItem> items = new List<SyndicationItem>();
    
    items.Add(item1);
    items.Add(item2);
    items.Add(item3);
    
    feed.Items = items;
    
  6. Geben Sie den Feed zurück.

    return new Atom10FeedFormatter(feed);
    

So hosten Sie den Dienst

  1. Erstellen Sie ein WebServiceHost-Objekt.

    Uri baseAddress = new Uri("https://localhost:8000/BlogService/");
    WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), baseAddress);
    
  2. Öffnen Sie den Diensthost, laden Sie den Feed vom Dienst, zeigen Sie den Feed an, und warten Sie darauf, dass der Benutzer die Eingabetaste drückt.

    svcHost.Open();
    Console.WriteLine("Service is running");
    
    XmlReader reader = XmlReader.Create("https://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();
    

So rufen Sie GetBlog() mit HTTP GET auf

  1. Öffnen Sie den Internet Explorer, geben Sie die folgende URL ein, und drücken Sie die EINGABETASTE: https://localhost:8000/BlogService/GetBlog.

    Die URL enthält die Basisadresse des Diensts (https://localhost:8000/BlogService), die relative Adresse des Endpunkts und den aufzurufenden Dienstvorgang.

So rufen Sie GetBlog() aus dem Code auf

  1. Erstellen Sie einen XmlReader mit der Basisadresse und der Methode, die Sie aufrufen.

    XmlReader reader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog");
    
  2. Rufen Sie die statische Load-Methode auf, und übergeben Sie dabei den gerade erstellten XmlReader.

    SyndicationFeed feed = SyndicationFeed.Load(reader);
    

    Dies ruft einen Dienstvorgang auf und füllt einen neuen SyndicationFeed mit dem vom Dienstvorgang zurückgegebenen Formatierungsprogramm auf.

  3. Greifen Sie auf das Feedobjekt zu.

    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);
    }
    

Beispiel

Nachfolgend ist die vollständige Codeauflistung für dieses Beispiel angegeben.

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 illistrates how to expose a feed using ATOM with WCF");

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

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

            SyndicationItem item3 = new SyndicationItem(
                "Item Three",
                "This is the content for item three",
                new Uri("https://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("https://localhost:8000/BlogService/");
            WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), baseAddress);
            try
            {
                svcHost.Open();
                Console.WriteLine("Service is running");

                XmlReader reader = XmlReader.Create("https://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();
            }
        }
    }
}

Kompilieren des Codes

Verweisen Sie beim Kompilieren des obigen Codes auf System.ServiceModel.dll und System.ServiceModel.Web.dll.

Siehe auch

Verweis

WebHttpBinding
WebGetAttribute