WSStreamedHttpBinding 範例示範如何建立系結,其設計目的是在使用 HTTP 傳輸時支援串流案例。
備註
此範例的安裝程式和建置指示位於本主題結尾。
建立和設定新標準系結的步驟如下。
建立新的標準系結
Windows Communication Foundation (WCF) 中的標準系結,例如 basicHttpBinding 和 netTcpBinding 會針對特定需求設定基礎傳輸和通道堆棧。 在此範例中,設定
WSStreamedHttpBinding通道堆疊以支援串流。 根據預設,WS-Security 和可靠的傳訊不會新增至通道堆疊,因為串流不支援這兩個功能。 新的系結會在衍生自WSStreamedHttpBinding的類別Binding中實作。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是一個 StandardBindingCollectionElement<TStandardBinding,TBindingConfiguration>,它將WSStreamedHttpBinding暴露給 WCF 組態系統。 大部分的實作會委派給WSStreamedHttpBindingConfigurationElement,其衍生自 StandardBindingElement。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>
要設定、建置和執行範例,請執行以下步驟:
請使用下列命令安裝 ASP.NET 4.0。
%windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable請確定您已執行 Windows Communication Foundation 範例One-Time 安裝程式中列出的步驟。
若要建置解決方案,請遵循 建置 Windows Communication Foundation 範例中的指示。
若要在跨計算機組態中執行範例,請遵循 執行 Windows Communication Foundation 範例中的指示。
當用戶端視窗顯示時,輸入 「Sample.txt」。 您應該會在目錄中找到「Sample.txt副本」。
WSStreamedHttpBinding 範例服務
所使用的 WSStreamedHttpBinding 範例服務位於服務子目錄中。 該實作OperationContract會先使用MemoryStream從傳入數據流擷取所有數據,然後返回MemoryStream。 範例服務是由 Internet Information Services (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 位址時,會顯示安全性警示,例如 https://localhost/servicemodelsamples/service.svc。 若要讓 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");