Vorgehensweise: Erstellen eines grundlegenden RS-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, der einen RSS Syndication-Feed verfügbar macht, erstellt wird.
So erstellen Sie einen grundlegenden Syndication-Dienst
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 Rss20FeedFormatter-Objekt zurückgeben.
<ServiceContract()> _ Public Interface IBlog <OperationContract()> _ <WebGet> _ Function GetBlog() As Rss20FeedFormatter End Interface
[ServiceContract] public interface IBlog { [OperationContract] [WebGet] Rss20FeedFormatter GetBlog(); }
Hinweis: Alle Dienstvorgänge, die das WebGetAttribute-Attribut übernehmen, 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. Implementieren Sie den Dienstvertrag.
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("https://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As SyndicationItem = New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("https://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As SyndicationItem = New SyndicationItem( _ "Item Three", _ "This is the content for item three", _ New Uri("https://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
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("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 Rss20FeedFormatter(feed); } }
Erstellen Sie ein SyndicationFeed-Objekt, und fügen Sie einen Autor, eine Kategorie und eine Beschreibung hinzu.
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")
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");
Erstellen Sie mehrere SyndicationItem-Objekte.
Dim item1 As SyndicationItem = New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("https://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As SyndicationItem = New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("https://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As SyndicationItem = New SyndicationItem( _ "Item Three", _ "This is the content for item three", _ New Uri("https://localhost/Content/three"), _ "ItemThreeID", _ DateTime.Now)
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);
Fügen Sie dem Feed das SyndicationItem hinzu
Dim items As New List(Of SyndicationItem)() items.Add(item1) items.Add(item2) items.Add(item3) feed.Items = items
List<SyndicationItem> items = new List<SyndicationItem>(); items.Add(item1); items.Add(item2); items.Add(item3); feed.Items = items;
Geben Sie den Feed zurück.
Return New Rss20FeedFormatter(feed)
return new Rss20FeedFormatter(feed);
So hosten Sie einen Dienst
Erstellen Sie ein WebServiceHost-Objekt.
Dim baseAddress As New Uri("https://localhost:8000/BlogService") Dim svcHost As New WebServiceHost(GetType(BlogService), baseAddress)
Uri baseAddress = new Uri("https://localhost:8000/BlogService"); WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), baseAddress);
Öffnen Sie den Diensthost, und warten Sie, bis der Benutzer die EINGABETASTE drückt.
svcHost.Open() Console.WriteLine("Service is running") Dim reader As XmlReader = XmlReader.Create("https://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()
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("Summary: {0}", ((TextSyndicationContent)item.Summary).Text); } Console.WriteLine("Press <enter> to quit..."); Console.ReadLine(); svcHost.Close();
So rufen Sie GetBlog() mit HTTP GET auf
- Ö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
Erstellen Sie einen XmlReader mit der Basisadresse und der Methode, die Sie aufrufen.
Dim serviceAddress As New Uri("https://localhost:8000/BlogService/GetBlog")
Uri serviceAddress = new Uri("https://localhost:8000/BlogService/GetBlog");
Rufen Sie die statische Load-Methode auf, und übergeben Sie dabei den gerade erstellten XmlReader.
Dim reader As XmlReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog") Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)
XmlReader reader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog"); SyndicationFeed feed = SyndicationFeed.Load(reader);
Dies ruft einen Dienstvorgang auf und füllt einen neuen SyndicationFeed mit dem vom Dienstvorgang zurückgegebenen Formatierungsprogramm auf.
Greifen Sie auf das Feedobjekt zu.
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(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.
Imports System
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("https://localhost/Content/One"), _
"ItemOneID", _
DateTime.Now)
Dim item2 As SyndicationItem = New SyndicationItem( _
"Item Two", _
"This is the content for item two", _
New Uri("https://localhost/Content/Two"), _
"ItemTwoID", _
DateTime.Now)
Dim item3 As SyndicationItem = New SyndicationItem( _
"Item Three", _
"This is the content for item three", _
New Uri("https://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("https://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("https://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
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("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 Rss20FeedFormatter(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("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();
}
}
}
}
Kompilieren des Codes
Verweisen Sie beim Kompilieren des obigen Codes auf System.ServiceModel.dll und System.ServiceModel.Web.dll.