Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
Windows Communication Foundation (WCF), dağıtım akışını kullanıma sunan bir hizmet oluşturmanıza olanak tanır. Bu konuda, hem Atom 1.0 hem de RSS 2.0 kullanarak bir dağıtım akışını kullanıma sunan bir dağıtım hizmetinin nasıl oluşturulacağı açıklanır. Bu hizmet, her iki sendikasyon formatını döndürebilen bir uç nokta sunar. Kolaylık olması için bu örnekte kullanılan hizmet kendi kendine barındırılır. Üretim ortamında bu tür bir hizmet IIS veya WAS altında barındırılabilir. Farklı WCF barındırma seçenekleri hakkında daha fazla bilgi için bkz. barındırma.
Temel bir dağıtım hizmeti oluşturmak için
WebGetAttribute özniteliğiyle işaretlenmiş bir arabirim kullanarak bir hizmet sözleşmesi tanımlayın. Dağıtım akışı olarak kullanıma sunulan her işlem bir SyndicationFeedFormatter nesnesi döndürür. WebGetAttributeparametrelerini not edin.
UriTemplate
bu hizmet işlemini çağırmak için kullanılan URL'yi belirtir. Bu parametrenin dizesi sabit değerler ve küme ayraçları içinde bir değişken ({formatında}) içerir. Bu değişken, hizmet işlemininformat
parametresine karşılık gelir. Daha fazla bilgi için bkz. UriTemplate.BodyStyle
, bu hizmet işleminin gönderdiği ve aldığı iletilerin nasıl yazıldığını etkiler. Bare, bu hizmet işlemine gönderilen ve bu hizmetten gönderilen verilerin altyapı tanımlı XML öğeleri tarafından sarmalanmadığını belirtir. Daha fazla bilgi için bkz. WebMessageBodyStyle.[ServiceContract] [ServiceKnownType(typeof(Atom10FeedFormatter))] [ServiceKnownType(typeof(Rss20FeedFormatter))] public interface IBlog { [OperationContract] [WebGet(UriTemplate = "GetBlog?format={format}")] SyndicationFeedFormatter GetBlog(string format); }
<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
Uyarı
Bu arabirimdeki hizmet işlemleri tarafından döndürülen türleri belirtmek için ServiceKnownTypeAttribute kullanın.
Hizmet sözleşmesini uygulayın.
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("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; if (format == "rss") return new Rss20FeedFormatter(feed); else if (format == "atom") return new Atom10FeedFormatter(feed); else return null; } }
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("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 If (format = "rss") Then Return New Rss20FeedFormatter(feed) Else Return New Atom10FeedFormatter(feed) End If End Function End Class
bir SyndicationFeed nesnesi oluşturun ve bir yazar, kategori ve açıklama ekleyin.
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");
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")
Birkaç SyndicationItem nesnesi oluşturun.
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 nesnelerini akışa ekleyin.
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
İstenen biçimi döndürmek için format parametresini kullanın.
if (format == "rss") return new Rss20FeedFormatter(feed); else if (format == "atom") return new Atom10FeedFormatter(feed); else return null;
If (format = "rss") Then Return New Rss20FeedFormatter(feed) Else Return New Atom10FeedFormatter(feed) End If
Hizmeti barındırmak için
bir WebServiceHost nesnesi oluşturun. WebServiceHost sınıfı, kod veya yapılandırmada belirtilmemişse hizmetin temel adresine otomatik olarak bir uç nokta ekler. Bu örnekte hiçbir uç nokta belirtilmediğinden varsayılan uç nokta kullanıma sunulur.
Uri address = new Uri("http://localhost:8000/BlogService/"); WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), address);
Dim address As New Uri("http://localhost:8000/BlogService/") Dim svcHost As New WebServiceHost(GetType(BlogService), address)
Hizmet ana bilgisayarını açın, hizmetten akışı yükleyin, akışı görüntüleyin ve kullanıcının ENTER tuşuna basmasını bekleyin.
svcHost.Open(); Console.WriteLine("Service is running"); Console.WriteLine("Loading feed in Atom 1.0 format."); XmlReader atomReader = XmlReader.Create("http://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: {item.Title.Text}"); Console.WriteLine($"Content: {((TextSyndicationContent)item.Content).Text}"); } Console.WriteLine("Loading feed in RSS 2.0 format."); XmlReader rssReader = XmlReader.Create("http://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: {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: {((TextSyndicationContent)item.Summary).Text}"); } 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.") Dim atomReader As XmlReader = XmlReader.Create("http://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("http://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()
GETBlog'u HTTP GET ile çağırmak için
Bir tarayıcı açın, aşağıdaki URL'yi girin ve Enter:
http://localhost:8000/BlogService/GetBlog
tuşlarına basın.URL, hizmetin temel adresini (
http://localhost:8000/BlogService
), uç noktanın göreli adresini ve çağrılacak hizmet işlemini içerir.
Koddan GetBlog() çağırmak için
Temel adres ve çağırdığınız yöntemle bir XmlReader oluşturun.
XmlReader reader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog?format=atom");
Dim atomReader As XmlReader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog?format=atom")
Statik Load(XmlReader) metodunu çağırarak, yeni oluşturduğunuz XmlReader'i geçirin.
SyndicationFeed feed = SyndicationFeed.Load(reader);
Dim feed As SyndicationFeed = SyndicationFeed.Load(atomReader)
Bu, hizmet işlemini çağırır ve yeni bir SyndicationFeed'ı hizmet işlemi tarafından döndürülen biçimlendirici ile doldurur.
Akış nesnesine erişin.
Console.WriteLine(feed.Title.Text); Console.WriteLine("Items:"); foreach (SyndicationItem item in feed.Items) { Console.WriteLine($"Title: {item.Title.Text}"); Console.WriteLine($"Content: {((TextSyndicationContent)item.Content).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("Content: {0}", (CType(item.Content, TextSyndicationContent).Text)) Next
Örnek
Aşağıda, bu örneğin tam kod listesi verilmiştir.
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("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;
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("http://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("http://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: {item.Title.Text}");
Console.WriteLine($"Content: {((TextSyndicationContent)item.Content).Text}");
}
Console.WriteLine("Loading feed in RSS 2.0 format.");
XmlReader rssReader = XmlReader.Create("http://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: {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: {((TextSyndicationContent)item.Summary).Text}");
}
Console.WriteLine("Press <ENTER> to quit...");
Console.ReadLine();
svcHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine($"An exception occurred: {ce.Message}");
svcHost.Abort();
}
}
}
}
Kodu Derleme
Önceki kodu derleme aşamasında, System.ServiceModel.dll ve System.ServiceModel.Web.dllreferans alın.