이 샘플에서는 HTTP 전송이 사용될 때 스트리밍 시나리오를 지원하도록 디자인된 바인딩을 만드는 방법을 보여 줍니다.
참고
이 샘플의 설치 절차 및 빌드 지침은 이 항목의 끝부분에 나와 있습니다.
새 표준 바인딩을 만들고 구성하기 위한 단계는 다음과 같습니다.
새 표준 바인딩 만들기
basicHttpBinding 및 netTcpBinding과 같은 WCF(Windows Communication Foundation)의 표준 바인딩은 특정 요구 사항에 맞게 기본 전송 및 채널 스택을 구성합니다. 이 샘플에서WSStreamedHttpBinding은 스트리밍을 지원하도록 채널 스택을 구성합니다. 기본적으로 WS-Security 및 신뢰할 수 있는 메시징은 스트리밍에서 지원되지 않으므로 이러한 기능은 둘 다 채널 스택에 추가되지 않습니다. 새 바인딩은 Binding에서 파생되는WSStreamedHttpBinding클래스에서 구현됩니다.WSStreamedHttpBinding은 HttpTransportBindingElement, HttpsTransportBindingElement, TransactionFlowBindingElement 및 TextMessageEncodingBindingElement 바인딩 요소를 포함합니다. 다음 샘플 코드와 같이 결과 바인딩 스택을 구성하기 위해 클래스에서CreateBindingElements()메서드를 제공합니다.public override BindingElementCollection CreateBindingElements() { // return collection of BindingElements BindingElementCollection bindingElements = new BindingElementCollection(); // the order of binding elements within the collection is important: layered channels are applied in the order included, followed by // the message encoder, and finally the transport at the end if (flowTransactions) { bindingElements.Add(transactionFlow); } bindingElements.Add(textEncoding); // add transport (http or https) bindingElements.Add(transport); return bindingElements.Clone(); }구성 지원 추가
구성을 통해 전송을 노출하기 위해 샘플에서는 두 개의 추가 클래스인WSStreamedHttpBindingConfigurationElement및WSStreamedHttpBindingSection을 구현합니다.WSStreamedHttpBindingSection클래스는 WCF 구성 시스템에WSStreamedHttpBinding을 노출하는 StandardBindingCollectionElement입니다. 대량의 구현은 StandardBindingElement에서 파생되는WSStreamedHttpBindingConfigurationElement에 위임됩니다.WSStreamedHttpBindingConfigurationElement클래스에는WSStreamedHttpBinding의 속성에 해당하는 속성과 각 구성 요소를 바인딩에 매핑하기 위한 함수가 있습니다.
서비스의 구성 파일에 다음 섹션을 추가하여 구성 시스템에 처리기를 등록합니다.<configuration> <system.serviceModel> <extensions> <bindingExtensions> <add name="wsStreamedHttpBinding" type="Microsoft.ServiceModel.Samples.WSStreamedHttpBindingCollectionElement, WSStreamedHttpBinding, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" /> </bindingExtensions> </extensions> </system.serviceModel> </configuration>그런 다음 serviceModel 구성 섹션에서 처리기를 참조할 수 있습니다.
<configuration> <system.serviceModel> <client> <endpoint address="https://localhost/servicemodelsamples/service.svc" bindingConfiguration="Binding" binding="wsStreamedHttpBinding" contract="Microsoft.ServiceModel.Samples.IStreamedEchoService"/> </client> </system.serviceModel> </configuration>
샘플을 설치, 빌드 및 실행하려면
Windows Communication Foundation 샘플의 일회 설치 절차에 나열된 단계를 수행했는지 확인합니다.
IIS(인터넷 정보 서비스) 서버 인증서 설치 지침을 수행했는지 확인합니다.
솔루션을 빌드하려면 Windows Communication Foundation 샘플 빌드의 지침을 따릅니다.
다중 컴퓨터 구성에서 샘플을 실행하려면 Windows Communication Foundation 샘플 실행의 지침을 따릅니다.
클라이언트 창이 표시되면 "Sample.txt"를 입력합니다. 디렉터리에서 "Copy of Sample.txt"를 볼 수 있습니다.
WSStreamedHttpBinding 샘플 서비스
WSStreamedHttpBinding을 사용하는 샘플 서비스는 서비스 하위 디렉터리에 있습니다. OperationContract 구현에서는 MemoryStream을 반환하기 전에 MemoryStream을 사용하여 들어오는 스트림에서 모든 데이터를 검색합니다. 샘플 서비스는 IIS(인터넷 정보 서비스)에 의해 호스팅됩니다.
[ServiceContract]
public interface IStreamedEchoService
{
[OperationContract]
Stream Echo(Stream data);
}
public class StreamedEchoService : IStreamedEchoService
{
public Stream Echo(Stream data)
{
MemoryStream dataStorage = new MemoryStream();
byte[] byteArray = new byte[8192];
int bytesRead = data.Read(byteArray, 0, 8192);
while (bytesRead > 0)
{
dataStorage.Write(byteArray, 0, bytesRead);
bytesRead = data.Read(byteArray, 0, 8192);
}
data.Close();
dataStorage.Seek(0, SeekOrigin.Begin);
return dataStorage;
}
}
WSStreamedHttpBinding 샘플 클라이언트
WSStreamedHttpBinding을 사용하여 상호 작용하는 데 사용되는 클라이언트는 클라이언트 하위 디렉터리에 있습니다. 이 샘플에 사용된 인증서는 Makecert.exe를 사용하여 만든 테스트 인증서이므로 브라우저에서 https://localhost/servicemodelsamples/service.svc와 같은 HTTPS 주소에 액세스하려 하면 보안 경고가 나타납니다. WCF 클라이언트에서 제공되는 테스트 인증서로 작업을 수행할 수 있도록 클라이언트에 추가 코드를 더하여 보안 경고가 표시되지 않도록 합니다. 코드 및 함께 사용되는 클래스는 프로덕션 인증서를 사용할 때에는 필요가 없습니다.
// WARNING: This code is only required for test certificates such as those created by makecert. It is
// not recommended for production code.
PermissiveCertificatePolicy.Enact("CN=ServiceModelSamples-HTTPS-Server");
Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.