如何:创建基本 Atom 源
使用 Windows Communication Foundation (WCF) 可以创建公开联合源的服务。 本主题讨论如何创建公开 Atom 联合源的联合服务。
创建基本联合服务
使用通过 WebGetAttribute 属性标记的接口定义服务协定。 作为联合源公开的每个操作应返回 Atom10FeedFormatter 对象。
[ServiceContract] public interface IBlog { [OperationContract] [WebGet] Atom10FeedFormatter GetBlog(); }
<ServiceContract()> _ Public Interface IBlog <OperationContract()> _ <WebGet> _ Function GetBlog() As Atom10FeedFormatter End Interface
注意
应用 WebGetAttribute 的所有服务操作将映射到 HTTP GET 请求。 若要将操作映射到不同的 HTTP 方法,请改用 WebInvokeAttribute。 有关详细信息,请参阅如何创建基本 WCF Web HTTP 服务。
实现服务协定。
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 BlogService Implements IBlog Public Function GetBlog() As Atom10FeedFormatter Implements IBlog.GetBlog Dim feed As 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 imports ATOM with WCF") Dim item1 As New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("http://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("http://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As 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 Atom10FeedFormatter(feed) End Function End Class
创建 SyndicationFeed 对象,并添加作者、类别和说明。
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");
Dim feed As 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 imports ATOM with WCF")
创建若干 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 New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("http://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("http://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As New SyndicationItem( _ "Item Three", _ "This is the content for item three", _ New Uri("http://localhost/Content/three"), _ "ItemThreeID", _ DateTime.Now)
向源中添加 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
返回源。
return new Atom10FeedFormatter(feed);
Return New Atom10FeedFormatter(feed)
承载服务
创建 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)
打开服务主机,从服务加载源,显示源,然后等待用户按 Enter。
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();
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("Content: {0}", item.Title.Text) Next Console.WriteLine("Press <ENTER> to quit...") Console.ReadLine() svcHost.Close()
使用 HTTP GET 调用 GetBlog()
在 Web 浏览器中,浏览到以下 URL:
http://localhost:8000/BlogService/GetBlog
URL 包含服务的基址 (
http://localhost:8000/BlogService
)、终结点的相对地址,以及要调用的服务操作。
从代码中调用 GetBlog()
使用基址和调用的方法创建 XmlReader。
XmlReader reader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog");
Dim serviceAddress As New Uri("http://localhost:8000/BlogService/GetBlog")
调用静态 Load(XmlReader) 方法,同时传入刚刚创建的 XmlReader。
SyndicationFeed feed = SyndicationFeed.Load(reader);
Dim feed As SyndicationFeed = SyndicationFeed.Load(serviceAddress)
这将调用服务操作,并用从服务操作返回的格式化程序填充新的 SyndicationFeed。
访问源对象。
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
示例
下面列出了此示例的完整代码。
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();
}
}
}
}
Imports System.Xml
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.ServiceModel.Syndication
Imports System.ServiceModel.Channels
Imports System.ServiceModel.Web
<ServiceContract()> _
Public Interface IBlog
<OperationContract()> _
<WebGet> _
Function GetBlog() As Atom10FeedFormatter
End Interface
Public Class BlogService
Implements IBlog
Public Function GetBlog() As Atom10FeedFormatter Implements IBlog.GetBlog
Dim feed As 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 imports ATOM with WCF")
Dim item1 As New SyndicationItem( _
"Item One", _
"This is the content for item one", _
New Uri("http://localhost/Content/One"), _
"ItemOneID", _
DateTime.Now)
Dim item2 As New SyndicationItem( _
"Item Two", _
"This is the content for item two", _
New Uri("http://localhost/Content/Two"), _
"ItemTwoID", _
DateTime.Now)
Dim item3 As 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 Atom10FeedFormatter(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.AddServiceEndpoint(GetType(IBlog), New WebHttpBinding(), "")
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("Content: {0}", item.Title.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
编译代码
编译前面的代码时,请引用 System.ServiceModel.dll 和 System.ServiceModel.Web.dll。