DiagnosticsFeed 샘플은 WCF(Windows Communication Foundation)를 사용하여 배포할 RSS/Atom 피드를 만드는 방법을 보여 줍니다. 개체 모델의 기본 사항과 WCF(Windows Communication Foundation) 서비스에서 설정하는 방법을 보여 주는 기본 "Hello World" 프로그램입니다.
WCF는 특수 데이터 형식을 반환하는 서비스 작업으로 배포 피드를 모델화합니다 SyndicationFeedFormatter. 인스턴스는 SyndicationFeedFormatter RSS 2.0 및 Atom 1.0 형식으로 피드를 직렬화할 수 있습니다. 다음 샘플 코드는 사용된 계약을 보여줍니다.
[ServiceContract(Namespace = "")]
interface IDiagnosticsService
{
[OperationContract]
//The [WebGet] attribute controls how WCF dispatches
//HTTP requests to service operations based on a URI suffix
//(the part of the request URI after the endpoint address)
//using the HTTP GET method. The UriTemplate specifies a relative
//path of 'feed', and specifies that the format is
//supplied using a query string.
[WebGet(UriTemplate="feed?format={format}")]
[ServiceKnownType(typeof(Atom10FeedFormatter))]
[ServiceKnownType(typeof(Rss20FeedFormatter))]
SyndicationFeedFormatter GetProcesses(string format);
}
GetProcesses 작업은 WebGetAttribute 속성을 사용하여 WCF가 HTTP GET 요청을 서비스 작업으로 디스패치하는 방법을 제어하고, 전송된 메시지의 형식을 지정합니다.
다른 WCF 서비스와 마찬가지로 배포 피드는 관리되는 모든 애플리케이션에서 자체 호스팅될 수 있습니다. 배포 서비스에는 특정 바인딩() WebHttpBinding과 특정 엔드포인트 동작()이 WebHttpBehavior올바르게 작동해야 합니다. 새 WebServiceHost 클래스는 특정 구성 없이 이러한 엔드포인트를 만들기 위한 편리한 API를 제공합니다.
WebServiceHost host = new WebServiceHost(typeof(ProcessService), new Uri("http://localhost:8000/diagnostics"));
//The WebServiceHost will automatically provide a default endpoint at the base address
//using the proper binding (the WebHttpBinding) and endpoint behavior (the WebHttpBehavior)
또는 IIS 호스팅 .svc 파일 내에서 동일한 기능을 제공하는 데 사용할 WebServiceHostFactory 수 있습니다(이 기술은 이 샘플 코드에서 설명되지 않음).
<% @ServiceHost Language="C#|VB" Debug="true" Service="ProcessService" %>
이 서비스는 표준 HTTP GET을 사용하여 요청을 수신하므로 RSS 또는 ATOM 인식 클라이언트를 사용하여 서비스에 액세스할 수 있습니다. 예를 들어, RSS 인식 브라우저에서 http://localhost:8000/diagnostics/feed/?format=atom 또는 http://localhost:8000/diagnostics/feed/?format=rss로 이동하여 이 서비스의 출력을 볼 수 있습니다.
WCF 배포 개체 모델이 Atom 및 RSS에 매핑되는 방법을 사용하여 신디케이트된 데이터를 읽고 명령적 코드를 사용하여 처리할 수도 있습니다.
XmlReader reader = XmlReader.Create( "http://localhost:8000/diagnostics/feed/?format=rss",
new XmlReaderSettings()
{
//MaxCharactersInDocument can be used to control the maximum amount of data
//read from the reader and helps prevent OutOfMemoryException
MaxCharactersInDocument = 1024 * 64
} );
SyndicationFeed feed = SyndicationFeed.Load(reader);
foreach (SyndicationItem i in feed.Items)
{
XmlSyndicationContent content = i.Content as XmlSyndicationContent;
ProcessData pd = content.ReadContent<ProcessData>();
Console.WriteLine(i.Title.Text);
Console.WriteLine(pd.ToString());
}
샘플 설정, 빌드 및 실행
Windows Communication Foundation 샘플에 대한One-Time 설치 절차의 설정 지침에 설명된 대로 컴퓨터에서 HTTP 및 HTTPS에 대한 올바른 주소 등록 권한이 있는지 확인합니다.
솔루션을 빌드합니다.
콘솔 애플리케이션을 실행합니다.
콘솔 애플리케이션이 실행되는 동안 RSS 인식 브라우저를 사용하여
http://localhost:8000/diagnostics/feed/?format=atom또는http://localhost:8000/diagnostics/feed/?format=rss으로 이동합니다.