WCF(Windows Communication Foundation)에서는 배포 피드를 노출하는 서비스를 만들 수 있습니다. 이 항목에서는 Atom 1.0 및 RSS 2.0을 사용하여 배포 피드를 노출하는 배포 서비스를 만드는 방법을 설명합니다. 이 서비스는 배포 형식 중 하나를 반환할 수 있는 하나의 끝점을 노출합니다. 편의를 위해 이 샘플에서 사용되는 서비스는 자체 호스팅됩니다. 프로덕션 환경에서 이 형식의 서비스는 IIS 또는 WAS에서 호스팅됩니다. 다양한 WCF 옵션에 대한 자세한 내용은 호스팅을 참조하십시오.
기본 배포 서비스를 만들려면
WebGetAttribute 특성으로 표시된 인터페이스를 사용하여 서비스 계약을 정의합니다. 배포 피드로 노출된 각 작업은 SyndicationFeedFormatter 개체를 반환합니다. WebGetAttribute의 매개 변수를 확인하십시오.
UriTemplate
은 이 서비스 작업을 호출하는 데 사용되는 URL을 지정합니다. 이 매개 변수의 문자열에는 중괄호 안에 있는 리터럴 및 변수({format})가 포함됩니다. 이 변수는 서비스 작업의 format 매개 변수에 해당합니다. 자세한 내용은 다음 항목을 참조하십시오. UriTemplate.BodyStyle
은 이 서비스 작업이 보내고 받는 메시지가 작성되는 방법에 영향을 줍니다. Bare는 이 서비스 작업에서 보내거나 받은 데이터가 인프라 정의 XML 요소로 래핑되지 않도록 지정합니다. 자세한 내용은 다음 항목을 참조하십시오. WebMessageBodyStyle.<ServiceContract()> _ <ServiceKnownType(GetType(Atom10FeedFormatter))> _ <ServiceKnownType(GetType(Rss20FeedFormatter))> _ Public Interface IBlog <OperationContract()> _ <WebGet(UriTemplate:="GetBlog?format={format}")> _ Function GetBlog(ByVal format As String) As SyndicationFeedFormatter End Interface
[ServiceContract] [ServiceKnownType(typeof(Atom10FeedFormatter))] [ServiceKnownType(typeof(Rss20FeedFormatter))] public interface IBlog { [OperationContract] [WebGet(UriTemplate = "GetBlog?format={format}")] SyndicationFeedFormatter GetBlog(string format); }
참고:
ServiceKnownTypeAttribute를 사용하여 이 인터페이스의 서비스 작업에서 반환하는 형식을 지정합니다. 서비스 계약을 구현합니다.
Public Class BlogService implements IBlog Public Function GetBlog(ByVal format As String) As SyndicationFeedFormatter Implements IBlog.GetBlog Dim feed As 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 sample that demonstrates how to expose a feed through RSS and Atom with WCF") Dim item1 As New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("https://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("https://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As 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 If (format = "rss") Then Return New Rss20FeedFormatter(feed) Else Return New Atom10FeedFormatter(feed) End If End Function End Class
public class BlogService : IBlog { public SyndicationFeedFormatter GetBlog(string format) { 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 sample that demonstrates how to expose a feed through RSS and 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; if (format == "rss") return new Rss20FeedFormatter(feed); else if (format == "atom") return new Atom10FeedFormatter(feed); else return null; } }
SyndicationFeed 개체를 만들고 만든 이, 범주 및 설명을 추가합니다.
Dim feed As 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 sample that demonstrates how to expose a feed through RSS and Atom 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 sample that demonstrates how to expose a feed through RSS and Atom with WCF");
여러 SyndicationItem 개체를 만듭니다.
Dim item1 As New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("https://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("https://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As 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);
SyndicationItem 개체를 피드에 추가합니다.
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;
형식 매개 변수를 사용하여 요청된 형식을 반환합니다.
If (format = "rss") Then Return New Rss20FeedFormatter(feed) Else Return New Atom10FeedFormatter(feed) End If
if (format == "rss") return new Rss20FeedFormatter(feed); else if (format == "atom") return new Atom10FeedFormatter(feed); else return null;
서비스를 호스팅하려면
WebServiceHost 개체를 만듭니다. WebServiceHost 클래스는 코드 또는 구성에 끝점이 지정되어 있지 않으면 서비스 기본 주소에 끝점을 자동으로 추가합니다. 이 샘플에서는 끝점이 지정되어 있지 않으므로 기본 끝점이 노출됩니다.
Dim address As New Uri("https://localhost:8000/BlogService/") Dim svcHost As New WebServiceHost(GetType(BlogService), address)
Uri address = new Uri("https://localhost:8000/BlogService/"); WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), address);
서비스 호스트를 열고 서비스에서 피드를 로드하여 피드를 표시한 다음 사용자가 Enter 키를 누를 때까지 기다립니다.
svcHost.Open() Console.WriteLine("Service is running") Console.WriteLine("Loading feed in Atom 1.0 format.") Dim atomReader As XmlReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=atom") Dim atomFeed As SyndicationFeed = SyndicationFeed.Load(atomReader) Console.WriteLine(atomFeed.Title.Text) Console.WriteLine("Items:") For Each item As SyndicationItem In atomFeed.Items Console.WriteLine("Title: {0}", item.Title.Text) Console.WriteLine("Content: {0}", CType(item.Content, TextSyndicationContent).Text) Next Console.WriteLine("Loading feed in RSS 2.0 format.") Dim rssReader As XmlReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=rss") Dim rssFeed As SyndicationFeed = SyndicationFeed.Load(rssReader) Console.WriteLine(rssFeed.Title.Text) Console.WriteLine("Items:") For Each item As SyndicationItem In rssFeed.Items Console.WriteLine("Title: {0}", item.Title.Text) Console.WriteLine("Content: {0}", CType(item.Content, TextSyndicationContent).Text) Next Console.WriteLine("Press <ENTER> to quit...") Console.ReadLine() svcHost.Close()
svcHost.Open(); Console.WriteLine("Service is running"); Console.WriteLine("Loading feed in Atom 1.0 format."); XmlReader atomReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=atom"); SyndicationFeed atomFeed = SyndicationFeed.Load(atomReader); Console.WriteLine(atomFeed.Title.Text); Console.WriteLine("Items:"); foreach (SyndicationItem item in atomFeed.Items) { Console.WriteLine("Title: {0}", item.Title.Text); Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Content).Text); } Console.WriteLine("Loading feed in RSS 2.0 format."); XmlReader rssReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=rss"); SyndicationFeed rssFeed = SyndicationFeed.Load(rssReader); Console.WriteLine(rssFeed.Title.Text); Console.WriteLine("Items:"); foreach (SyndicationItem item in rssFeed.Items) { Console.WriteLine("Title: {0}", item.Title.Text); // Notice we are using item.Summary here instead of item.Content. This is because // of the differences between Atom 1.0 and RSS 2.0 specs. Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Summary).Text); } Console.WriteLine("Press <ENTER> to quit..."); Console.ReadLine(); svcHost.Close();
HTTP GET을 사용하여 GetBlog를 호출하려면
Internet Explorer를 열고 https://localhost:8000/BlogService/GetBlog를 입력한 다음 Enter를 누릅니다.
이 URL에는 서비스의 기본 주소(https://localhost:8000/BlogService), 끝점의 상대 주소 및 호출할 서비스 작업이 포함됩니다.
코드에서 GetBlog()를 호출하려면
기본 주소 및 호출할 메서드를 사용하여 XmlReader를 만듭니다.
Dim atomReader As XmlReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=atom")
XmlReader reader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=atom");
지금 만든 XmlReader를 전달하는 정적 Load 메서드를 호출합니다.
Dim feed As SyndicationFeed = SyndicationFeed.Load(atomReader)
SyndicationFeed feed = SyndicationFeed.Load(reader);
이렇게 하면 서비스 작업이 호출되고 서비스 작업에서 반환된 포맷터로 새 SyndicationFeed가 채워집니다.
피드 개체에 액세스합니다.
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("Content: {0}", (CType(item.Content, TextSyndicationContent).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("Content: {0}", ((TextSyndicationContent)item.Content).Text); }
예제
다음은 이 예제에 해당되는 전체 코드 목록입니다.
using System;
using System.Xml;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
namespace Service
{
[ServiceContract]
[ServiceKnownType(typeof(Atom10FeedFormatter))]
[ServiceKnownType(typeof(Rss20FeedFormatter))]
public interface IBlog
{
[OperationContract]
[WebGet(UriTemplate = "GetBlog?format={format}")]
SyndicationFeedFormatter GetBlog(string format);
}
public class BlogService : IBlog
{
public SyndicationFeedFormatter GetBlog(string format)
{
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 sample that demonstrates how to expose a feed through RSS and 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;
if (format == "rss")
return new Rss20FeedFormatter(feed);
else if (format == "atom")
return new Atom10FeedFormatter(feed);
else return null;
}
}
public class Host
{
static void Main(string[] args)
{
Uri address = new Uri("https://localhost:8000/BlogService/");
WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), address);
try
{
svcHost.Open();
Console.WriteLine("Service is running");
Console.WriteLine("Loading feed in Atom 1.0 format.");
XmlReader atomReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=atom");
SyndicationFeed atomFeed = SyndicationFeed.Load(atomReader);
Console.WriteLine(atomFeed.Title.Text);
Console.WriteLine("Items:");
foreach (SyndicationItem item in atomFeed.Items)
{
Console.WriteLine("Title: {0}", item.Title.Text);
Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Content).Text);
}
Console.WriteLine("Loading feed in RSS 2.0 format.");
XmlReader rssReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=rss");
SyndicationFeed rssFeed = SyndicationFeed.Load(rssReader);
Console.WriteLine(rssFeed.Title.Text);
Console.WriteLine("Items:");
foreach (SyndicationItem item in rssFeed.Items)
{
Console.WriteLine("Title: {0}", item.Title.Text);
// Notice we are using item.Summary here instead of item.Content. This is because
// of the differences between Atom 1.0 and RSS 2.0 specs.
Console.WriteLine("Content: {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();
}
}
}
}
코드 컴파일
앞의 코드를 컴파일할 때 System.ServiceModel.dll 및 System.ServiceModel.Web.dll을 참조합니다.