Procédure : créer un flux RSS de base
Windows Communication Foundation (WCF) vous permet de créer un service qui expose un flux de syndication. Cette rubrique explique comment créer un service de syndication qui expose un flux de syndication RSS.
Pour créer un service de syndication de base
Définissez un contrat de service utilisant une interface marquée avec l'attribut WebGetAttribute. Chaque opération exposée comme un flux de syndication doit retourner un objet Rss20FeedFormatter.
[ServiceContract] public interface IBlog { [OperationContract] [WebGet] Rss20FeedFormatter GetBlog(); }
<ServiceContract()> _ Public Interface IBlog <OperationContract()> _ <WebGet> _ Function GetBlog() As Rss20FeedFormatter End Interface
Notes
Toutes les opérations du service qui appliquent l'attribut WebGetAttribute sont mappées aux demandes HTTP GET. Pour mapper votre opération à une méthode HTTP différente, utilisez WebInvokeAttribute à la place. Pour plus d’informations, consultez Guide pratique pour créer un service web HTTP WCF de base.
Implémentez le contrat de service.
public class BlogService : IBlog { public Rss20FeedFormatter GetBlog() { SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI")); feed.Authors.Add(new SyndicationPerson("someone@microsoft.com")); feed.Categories.Add(new SyndicationCategory("How To Sample Code")); feed.Description = new TextSyndicationContent("This is a how to sample that demonstrates how to expose a feed using RSS 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 Rss20FeedFormatter(feed); } }
Public Class BlogService Implements IBlog Public Function GetBlog() As Rss20FeedFormatter Implements IBlog.GetBlog Dim feed As SyndicationFeed = New SyndicationFeed("My Blog Feed", "This is a test feed", New Uri("http://SomeURI")) feed.Authors.Add(New SyndicationPerson("someone@microsoft.com")) feed.Categories.Add(New SyndicationCategory("How To Sample Code")) feed.Description = New TextSyndicationContent("This is a how to sample that demonstrates how to expose a feed using RSS with WCF") Dim item1 As SyndicationItem = New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("http://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As SyndicationItem = New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("http://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As SyndicationItem = New SyndicationItem( _ "Item Three", _ "This is the content for item three", _ New Uri("http://localhost/Content/three"), _ "ItemThreeID", _ DateTime.Now) Dim items As New List(Of SyndicationItem)() items.Add(item1) items.Add(item2) items.Add(item3) feed.Items = items Return New Rss20FeedFormatter(feed) End Function End Class
Créez un objet SyndicationFeed et ajoutez un auteur, une catégorie et une description.
SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI")); feed.Authors.Add(new SyndicationPerson("someone@microsoft.com")); feed.Categories.Add(new SyndicationCategory("How To Sample Code")); feed.Description = new TextSyndicationContent("This is a how to sample that demonstrates how to expose a feed using RSS with WCF");
Dim feed As SyndicationFeed = New SyndicationFeed("My Blog Feed", "This is a test feed", New Uri("http://SomeURI")) feed.Authors.Add(New SyndicationPerson("someone@microsoft.com")) feed.Categories.Add(New SyndicationCategory("How To Sample Code")) feed.Description = New TextSyndicationContent("This is a how to sample that demonstrates how to expose a feed using RSS with WCF")
Créez plusieurs objets SyndicationItem.
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);
Dim item1 As SyndicationItem = New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("http://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As SyndicationItem = New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("http://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As SyndicationItem = New SyndicationItem( _ "Item Three", _ "This is the content for item three", _ New Uri("http://localhost/Content/three"), _ "ItemThreeID", _ DateTime.Now)
Ajoutez le flux SyndicationItem.
List<SyndicationItem> items = new List<SyndicationItem>(); items.Add(item1); items.Add(item2); items.Add(item3); feed.Items = items;
Dim items As New List(Of SyndicationItem)() items.Add(item1) items.Add(item2) items.Add(item3) feed.Items = items
Retournez le flux.
return new Rss20FeedFormatter(feed);
Return New Rss20FeedFormatter(feed)
Pour héberger un service
Créez un objet WebServiceHost.
Uri baseAddress = new Uri("http://localhost:8000/BlogService"); WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), baseAddress);
Dim baseAddress As New Uri("http://localhost:8000/BlogService") Dim svcHost As New WebServiceHost(GetType(BlogService), baseAddress)
Ouvrez l'hôte de service et attendez jusqu'à ce que l'utilisateur appuie sur ENTRÉE.
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("Summary: {0}", ((TextSyndicationContent)item.Summary).Text); } Console.WriteLine("Press <enter> to quit..."); Console.ReadLine(); svcHost.Close();
svcHost.Open() Console.WriteLine("Service is running") Dim reader As XmlReader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog") Dim feed As SyndicationFeed = SyndicationFeed.Load(reader) Console.WriteLine(feed.Title.Text) Console.WriteLine("Items:") For Each item As SyndicationItem In feed.Items Console.WriteLine("Title: {0}", item.Title.Text) Console.WriteLine("Summary: {0}", item.Summary.Text) Next Console.WriteLine("Press <enter> to quit...") Console.ReadLine() svcHost.Close()
Pour appeler GetBlog() avec un HTTP GET
- Dans un navigateur web, accédez à l’URL suivante :
http://localhost:8000/BlogService/GetBlog
. L'URL contient l’adresse de base du service (http://localhost:8000/BlogService
), l’adresse relative du point de terminaison et l’opération de service à appeler.
Pour appeler GetBlog() à partir d'un code
Créez un XmlReader avec l'adresse de base et la méthode que vous appelez.
Uri serviceAddress = new Uri("http://localhost:8000/BlogService/GetBlog");
Dim serviceAddress As New Uri("http://localhost:8000/BlogService/GetBlog")
Appelez la méthode Load(XmlReader) statique, en passant dans le XmlReader que vous venez de créer.
XmlReader reader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog"); SyndicationFeed feed = SyndicationFeed.Load(reader);
Dim reader As XmlReader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog") Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)
Cela appelle l'opération de service et remplit un nouvel objet SyndicationFeed avec le module de formatage retourné à partir de l'opération de service.
Accédez à l'objet de flux.
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); }
Console.WriteLine(feed.Title.Text) Console.WriteLine("Items:") For Each item As SyndicationItem In feed.Items Console.WriteLine("Title: {0}", item.Title.Text) Console.WriteLine("Summary: {0}", item.Summary.Text) Next
Exemple
Les éléments suivants représentent l'intégralité du code pour cet exemple.
using System;
using System.Xml;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Collections.ObjectModel;
using System.Collections.Generic;
namespace Service
{
[ServiceContract]
public interface IBlog
{
[OperationContract]
[WebGet]
Rss20FeedFormatter GetBlog();
}
public class BlogService : IBlog
{
public Rss20FeedFormatter GetBlog()
{
SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"));
feed.Authors.Add(new SyndicationPerson("someone@microsoft.com"));
feed.Categories.Add(new SyndicationCategory("How To Sample Code"));
feed.Description = new TextSyndicationContent("This is a how to sample that demonstrates how to expose a feed using RSS 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 Rss20FeedFormatter(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("Summary: {0}", ((TextSyndicationContent)item.Summary).Text);
}
Console.WriteLine("Press <enter> to quit...");
Console.ReadLine();
svcHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
svcHost.Abort();
}
}
}
}
Imports System.Xml
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.ServiceModel.Syndication
Imports System.ServiceModel.Web
Imports System.Collections.ObjectModel
Imports System.Collections.Generic
<ServiceContract()> _
Public Interface IBlog
<OperationContract()> _
<WebGet> _
Function GetBlog() As Rss20FeedFormatter
End Interface
Public Class BlogService
Implements IBlog
Public Function GetBlog() As Rss20FeedFormatter Implements IBlog.GetBlog
Dim feed As SyndicationFeed = New SyndicationFeed("My Blog Feed", "This is a test feed", New Uri("http://SomeURI"))
feed.Authors.Add(New SyndicationPerson("someone@microsoft.com"))
feed.Categories.Add(New SyndicationCategory("How To Sample Code"))
feed.Description = New TextSyndicationContent("This is a how to sample that demonstrates how to expose a feed using RSS with WCF")
Dim item1 As SyndicationItem = New SyndicationItem( _
"Item One", _
"This is the content for item one", _
New Uri("http://localhost/Content/One"), _
"ItemOneID", _
DateTime.Now)
Dim item2 As SyndicationItem = New SyndicationItem( _
"Item Two", _
"This is the content for item two", _
New Uri("http://localhost/Content/Two"), _
"ItemTwoID", _
DateTime.Now)
Dim item3 As SyndicationItem = New SyndicationItem( _
"Item Three", _
"This is the content for item three", _
New Uri("http://localhost/Content/three"), _
"ItemThreeID", _
DateTime.Now)
Dim items As New List(Of SyndicationItem)()
items.Add(item1)
items.Add(item2)
items.Add(item3)
feed.Items = items
Return New Rss20FeedFormatter(feed)
End Function
End Class
Module Program
Sub Main()
Dim baseAddress As New Uri("http://localhost:8000/BlogService")
Dim svcHost As New WebServiceHost(GetType(BlogService), baseAddress)
Try
svcHost.Open()
Console.WriteLine("Service is running")
Dim reader As XmlReader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog")
Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)
Console.WriteLine(feed.Title.Text)
Console.WriteLine("Items:")
For Each item As SyndicationItem In feed.Items
Console.WriteLine("Title: {0}", item.Title.Text)
Console.WriteLine("Summary: {0}", item.Summary.Text)
Next
Console.WriteLine("Press <enter> to quit...")
Console.ReadLine()
svcHost.Close()
Catch ce As CommunicationException
Console.WriteLine("An exception occurred: {0}", ce.Message)
svcHost.Abort()
End Try
End Sub
End Module
Compilation du code
Lors de la compilation du code précédent, référencez System.ServiceModel.dll et System.ServiceModel.Web.dll.