Windows Communication Foundation 4.5 的新功能What's New in Windows Communication Foundation 4.5

本主題討論 Windows Communication Foundation (WCF) 4.5 版的新功能。This topic discusses features new to Windows Communication Foundation (WCF) version 4.5.

WCF 簡化功能WCF Simplification Features

為了使 WCF 4.5 應用程式更容易開發和維護,我們已投入了大量的努力。Much work has been done to make WCF 4.5 applications easier to develop and maintain. 如需詳細資訊,請參閱 WCF 簡化功能For more information, see WCF Simplification Features.

以工作為基礎的非同步支援Task-based Async Support

根據預設,[加入服務參考] 可產生傳回工作的非同步服務作業方法。By default, Add Service Reference generates Task-returning async service operation methods. 也可使用同步和非同步方法。This is done for both synchronous and asynchronous methods. 使用這種全新且以工作為基礎的非同步程式設計模型,可讓您以非同步方式呼叫服務作業。This allows you to call the service operations asynchronously using the new Task based async programming model. 當您呼叫所產生的 Proxy 方法時,WCF 會建構 Task 物件以代表非同步作業,並將該 Task 傳回給您。When you call the generated proxy method, WCF constructs a Task object to represent the asynchronous operation and returns that Task to you. 此工作會在作業完成時完成。The Task completes when the operation completes. 在執行非同步作業時,您可以將它實作為以工作為基礎的非同步作業。When implementing an async operation you can implement it as a task-based async operation. 如需詳細資訊,請參閱 同步和非同步作業For more information see, Synchronous and Asynchronous Operations.

簡化產生的組態檔Simplified Generated Configuration Files

當您在 Visual Studio 中加入服務參考,或使用 SvcUtil.exe 工具時,用戶端組態檔就會產生。When you add a service reference in Visual Studio or use the SvcUtil.exe tool, a client configuration file is generated. 在舊版 WCF 中,這些組態檔會包含所有繫結屬性的值,即使值為預設值,也是如此。In previous versions of WCF these configuration files contained the value of every binding property even if its value is the default value. 在 WCF 4.5 中產生的組態檔只會包含那些設定成非預設值的繫結屬性。In WCF 4.5 the generated configuration files contain only those binding properties that are set to a non-default value.

如需詳細資訊,請參閱 WCF 簡化功能For more information, see WCF Simplification Features.

合約優先開發Contract-First Development

WCF 現可支援合約優先開發。WCF now has support for contract-first development. svcutil.exe 具有/serviceContract 參數,可讓您從 WSDL 檔案產生服務和資料合約。The svcutil.exe has a /serviceContract switch which allows you to generate service and data contracts from a WSDL document.

從可攜式子集專案加入服務參考Add Service Reference from a Portable Subset Project

可移植子集專案可讓 .NET 元件程式設計人員維護單一來源樹狀結構和組建系統,同時仍支援多個 .NET 平臺 (桌面、Silverlight、Windows Phone 和 Xbox) 。Portable subset projects enable .NET assembly programmers to maintain a single source tree and build system while still supporting multiple .NET platforms (desktop, Silverlight, Windows Phone, and Xbox). 便攜子集專案只參考可在任何 .NET 平臺上使用之元件的 .NET 可移植程式庫。Portable subset projects only reference .NET portable libraries that are assemblies that can be used on any .NET platform. 開發人員的體驗與在任何其他 WCF 用戶端應用程式內加入服務參考相同。The developer experience is the same as adding a service reference within any other WCF client application. 如需詳細資訊,請參閱 便攜子集專案中的加入服務參考For more information, see Add Service Reference in a Portable Subset Project.

ASP.NET 相容模式預設值已變更ASP.NET Compatibility Mode Default Changed

WCF 會在寫入 WCF 服務時提供 ASP.NET 相容模式,將 ASP.NET HTTP 管線功能的完整存取權限授與開發人員。WCF provides ASP.NET compatibility mode to grant developers full access to the features in the ASP.NET HTTP pipeline when writing WCF services. 若要使用此模式,您必須 aspNetCompatibilityEnabled 在 web.config 的區段中,將屬性設定為 true <serviceHostingEnvironment> 。此外,此 appDomain 中的任何服務都必須將 RequirementsMode 其上的屬性 AspNetCompatibilityRequirementsAttribute 設定 AllowedRequired 或。To use this mode, you must set the aspNetCompatibilityEnabled attribute to true in the <serviceHostingEnvironment> section of web.config. Additionally, any service in this appDomain needs to have the RequirementsMode property on its AspNetCompatibilityRequirementsAttribute set to Allowed or Required. 依預設 AspNetCompatibilityRequirementsAttribute ,現在會設定為 AllowedBy default AspNetCompatibilityRequirementsAttribute is now set to Allowed. 如需詳細資訊,請參閱 WCF 服務和 ASP.NETFor more information, see WCF Services and ASP.NET.

新的傳輸預設值New Transport Default Values

為了簡化組態,我們已變更一些傳輸屬性的預設值。In order to simplify configuration a number of transport property default values have changed. 如需詳細資訊,請參閱 WCF 簡化功能For more information, see WCF Simplification Features.

XmlDictionaryReaderQuotasXmlDictionaryReaderQuotas

XmlDictionaryReaderQuotas 包含 XML 字典讀取器的可設定配額值,這些值會限制編碼器在建立訊息時使用的記憶體量。XmlDictionaryReaderQuotas contains configurable quota values for XML dictionary readers which limit the amount of memory utilized by an encoder while creating a message. 雖然這些配額是可設定的,但變更為預設值可降低開發人員必須明確設定這些值的機率。While these quotas are configurable, the default values have changed to lessen the possibility that a developer will have to set them explicitly. 如需詳細資訊,請參閱 WCF 簡化功能For more information, see WCF Simplification Features.

WCF 組態驗證WCF Configuration Validation

Visual Studio 現在有一部分建置流程會針對專案中定義的屬性來驗證 WCF 組態檔。As part of the build process within Visual Studio, WCF configuration files are now validated for attributes defined within the project. 如果驗證失敗,將會在 Visual Studio 中顯示驗證錯誤或警告的清單。A list of validation errors or warnings is displayed in Visual Studio if the validation fails.

XML 編輯器工具提示XML Editor Tooltips

為了協助新的和現有的 WCF 服務開發人員設定其服務,現在每個屬於服務組態檔之一部分的組態項目及其屬性,Visual Studio XML 編輯器都有提供工具提示。In order to help new and existing WCF service developers to configure their services, the Visual Studio XML editor now provides tooltips for every configuration element and its properties that is part of the service configuration file.

資料流的改進Streaming Improvements

加入對真正非同步資料流處理的支援,如果接收端不是正在讀取或是讀取速度緩慢,傳送端現在不會封鎖執行緒,延展性因此提升。Added support for true asynchronous streaming where the send side now does not block threads if the receive side is not reading or slow in reading thereby increasing scalability. 移除對用戶端發送資料流訊息至 IIS 裝載之 WCF 服務時的訊息緩衝限制。Removed the limitation of message buffering when a client sends a streamed message to an IIS hosted WCF service. 如需詳細資訊,請參閱 WCF 簡化功能For more information, see WCF Simplification Features.

簡化透過 HTTPS 向 IIS 公開端點Simplifying Exposing an Endpoint Over HTTPS with IIS

已加入 HTTPS 通訊協定對應以簡化透過 HTTPS 公開端點的程序。An HTTPS protocol mapping has been added to simplify exposing an endpoint over HTTPS. 若要啟用 HTTPS 端點,請確定您的網站已設定 HTTPS 繫結和 SSL 憑證,然後只需為裝載服務的虛擬目錄啟用 HTTPS 即可。To enable an HTTPS endpoint, ensure your website has an HTTPS binding and SSL certificate configured, and then simply enable HTTPS for the virtual directory that hosts the service. 如果服務的中繼資料已啟用,也會透過 HTTPS 加以公開。If metadata is enabled for the service, it will be exposed over HTTPS as well.

產生單一 WSDL 文件Generating a Single WSDL Document

某些協力廠商 WSDL 處理堆疊無法處理透過 xsd:import 相依於其他文件的 WSDL 文件。Some third-party WSDL processing stacks are not able to process WSDL documents that have dependencies on other documents through a xsd:import. WCF 現在允許您指定以單一文件來傳回所有 WSDL 資訊。WCF now allows you to specify that all WSDL information be returned in a single document. 要求從服務要求中繼資料時,將 "? singleWSDL" 附加至 URI。To request a single WSDL document append "?singleWSDL" to the URI when requesting metadata from the service.

WebSocket 支援WebSocket Support

WebSockets 是透過連接埠 80 和 443 提供真正雙向通訊的技術,其效能特性與 TCP 類似。WebSockets is a technology that provides true bidirectional communication over ports 80 and 443 with performance characteristics similar to TCP. 為了支援透過 WebSocket 傳輸進行通訊,我們已加入兩個新的繫結。Two new bindings have been added to support communication over a WebSocket transport. NetHttpBindingNetHttpsBindingNetHttpBinding and NetHttpsBinding. 如需詳細資訊,請參閱: 系統提供的系結。For more information see: System-Provided Bindings.

新的傳輸預設值New Transport Default Values

下列資料表說明已變更的設定以及可找到其他資訊的位置。The following table describes the settings that have changed and where to find additional information.

屬性Property 開啟On 新的預設值New Default 如需詳細資訊,請參閱For more information see
channelInitializationTimeoutchannelInitializationTimeout NetTcpBinding 30 秒30 seconds ChannelInitializationTimeout
listenBackloglistenBacklog NetTcpBinding 12 * 處理器的數量12 * number of processors ListenBacklog
maxPendingAcceptsmaxPendingAccepts ConnectionOrientedTransportBindingElementConnectionOrientedTransportBindingElement

SMSvcHost.exeSMSvcHost.exe
2 * 用於傳輸的處理器數量2 * number of processors for transport

SMSvcHost.exe 4 個 * 處理器數目4 * number of processors for SMSvcHost.exe
MaxPendingAccepts 設定 Net.TCP Port Sharing ServiceMaxPendingAccepts Configuring the Net.TCP Port Sharing Service
maxPendingConnectionsmaxPendingConnections ConnectionOrientedTransportBindingElementConnectionOrientedTransportBindingElement 12 * 處理器的數量12 * number of processors MaxPendingConnections
receiveTimeoutreceiveTimeout SMSvcHost.exeSMSvcHost.exe 30 秒30 seconds 設定 Net.TCP Port Sharing ServiceConfiguring the Net.TCP Port Sharing Service

XML 編輯器工具提示XML Editor Tooltips

為了協助新的和現有的 WCF 服務開發人員設定其服務,現在每個屬於服務組態檔之一部分的組態項目及其屬性,Visual Studio XML 編輯器都有提供工具提示。In order to help new and existing WCF service developers to configure their services, the Visual Studio XML editor now provides tooltips for every configuration element and its properties that is part of the service configuration file.

在程式碼中設定 WCF 服務Configuring WCF Services in Code

Windows Communication Foundation (WCF) 可讓開發人員使用設定檔或程式碼來設定服務。Windows Communication Foundation (WCF) allows developers to configure services using configuration files or code. 當服務需要在部署後進行設定時,組態檔非常有用。Configuration files are useful when a service needs to be configured after being deployed. 使用組態檔時,IT 專業人員只需更新組態檔,不必重新編譯。When using configuration files, an IT professional only needs to update the configuration file, no recompilation is required. 但是組態檔可能會很複雜而難以維護。Configuration files, however, can be complex and difficult to maintain. 由於不支援組態檔偵錯,而且組態項目是以名稱來參考,這使得製作組態檔容易出錯且難度增加。There is no support for debugging configuration files and configuration elements are referenced by names which makes authoring configuration files error-prone and difficult. WCF 也可讓您在程式碼中設定服務。WCF also allows you to configure services in code. 在舊版 WCF (4.0 和舊版中) 在自我裝載的案例中,以程式碼來設定服務很容易,類別可讓 ServiceHost 您在呼叫 ServiceHost 之前設定端點和行為。開啟。In earlier versions of WCF (4.0 and earlier) configuring services in code was easy in self-hosted scenarios, the ServiceHost class allowed you to configure endpoints and behaviors prior to calling ServiceHost.Open. 但是在 Web 裝載案例中,您就無法存取 ServiceHost 類別。In web hosted scenarios, however, you don’t have access to the ServiceHost class. 為了設定 Web 裝載服務,您需要建立會建立 System.ServiceModel.ServiceHostFactoryServiceHostFactory,並執行任何所需的設定。To configure a web hosted service you were required to create a System.ServiceModel.ServiceHostFactory that created the ServiceHostFactory and performed any needed configuration. 從 .NET Framework 4.5 開始,WCF 提供更簡單的方法,讓您在程式碼中同時設定自我裝載和 web 託管服務。Starting with .NET Framework 4.5, WCF provides an easier way to configure both self-hosted and web hosted services in code. 如需詳細資訊,請參閱 在程式碼中設定 WCF 服務For more information, see Configuring WCF Services in Code.

ChannelFactory 快取ChannelFactory Caching

WCF 用戶端應用程式會使用 ChannelFactory<TChannel> 類別來建立與 WCF 服務的通訊通道。WCF client applications use the ChannelFactory<TChannel> class to create a communication channel with a WCF service. 建立 ChannelFactory<TChannel> 執行個體會產生額外負荷,因為這涉及到下列作業:Creating ChannelFactory<TChannel> instances incurs some overhead because it involves the following operations:

  1. 建構 ContractDescription 樹狀Constructing the ContractDescription tree

  2. 反映所有必要的 CLR 型別Reflecting all of the required CLR types

  3. 建構通道堆疊Constructing the channel stack

  4. 處置資源Disposing of resources

為了盡量減少這種負荷,WCF 可以在您使用 WCF 用戶端 Proxy 時快取通道處理站。To help minimize this overhead, WCF can cache channel factories when you are using a WCF client proxy. 如需詳細資訊,請參閱通道處理站 快取。For more information, see Channel Factory and Caching.

壓縮和二進位編碼器Compression and the Binary Encoder

從 WCF 4.5 開始,WCF 二進位編碼器加入壓縮的支援。Beginning with WCF 4.5 the WCF binary encoder adds support for compression. 壓縮型別是透過 CompressionFormat 屬性來設定。The type of compression is configured with the CompressionFormat property. 用戶端和服務都必須設定 CompressionFormat 屬性。Both the client and the service must configure the CompressionFormat property. 壓縮適用於 HTTP、HTTPS 和 TCP 通訊協定。Compression will work for HTTP, HTTPS, and TCP protocols. 如果用戶端指定要使用壓縮,但服務不支援這項功能,則會擲回通訊協定例外狀況以表示通訊協定不相符。If a client specifies to use compression but the service does not support it a protocol exception is thrown indicating a protocol mismatch. 如需詳細資訊,請參閱 選擇訊息編碼器For more information, see Choosing a Message Encoder.

UDPUDP

已新增 UDP 傳輸的支援,可讓開發人員撰寫使用「引發並忘記」訊息的服務。Support has been added for a UDP transport which allows developers to write services that use "fire and forget" messaging. 用戶端傳送訊息給服務,而不期待服務發出任何回應。A client sends a message to a service and expects no response from the service.

多重驗證支援Multiple Authentication Support

已加入支援,如同 IIS 的支援一樣,在使用 HTTP 傳輸與傳輸安全性時,於單一 WCF 端點上支援多個驗證模式。Support has been added to support multiple authentication modes, as supported by IIS, on a single WCF endpoint when using the HTTP transport and transport security. IIS 允許您在虛擬目錄上啟用多個驗證模式,這項功能可讓單一 WCF 端點支援裝載 WCF 服務之虛擬目錄所啟用的多個驗證模式。IIS allows you to enable multiple authentication modes on a virtual directory, this feature allows a single WCF endpoint to support the multiple authentication modes enabled for the virtual directory where the WCF service is hosted.

IDN 支援IDN Support

已加入支援,並允許具有國際化網域名稱的 WCF 服務。Support has been added to allow for WCF services with Internationalized Domain Names. 如需詳細資訊,請參閱 WCF 和國際化功能變數名稱For more information see WCF and Internationalized Domain Names.

HttpClientHttpClient

已加入名稱為 HttpClient 的新類別,使得處理 HTTP 要求大為方便。A new class called HttpClient has been added to make working with HTTP requests much easier. 如需詳細資訊,請參閱 讓應用程式以社交和連線至 HTTP 服務Http 用戶端範例For more info, see Making apps social and connected with HTTP services and the HTTP Client Sample.

組態 IntellisenseConfiguration Intellisense

在組態檔中,對於專案內定義之自訂屬性的屬性值現可支援 Intellisense 功能,以協助您快速準確地處理組態檔。Attribute values in configuration files for custom attributes defined in the project now support intellisense to facilitate working with configurations quickly and accurately.

組態工具提示Configuration tooltips

WCF 元素和屬性現在會在 XML 編輯器中有工具提示,以更輕鬆且精確地識別元素或屬性的用途。WCF elements and attributes now have tooltips in the XML editor, to more easily and accurately identify the purpose of the element or attribute.

貼上資料做為類別Paste Data as Classes

在 WCF 專案中,定義於 XML 的資料型別 (例如服務中所公開的項目) 可以直接貼到程式碼頁面上。In a WCF project, data types defined in XML (such as are exposed in a service) can be pasted directly into a code page. XML 型別會以 CLR 型別貼上。The XML type will be pasted as a CLR type. 如需詳細資訊,請參閱 從 XML 產生資料類型類別See Generating Data Type Classes from XML for more details.

WebServiceHost 和預設端點WebServiceHost and default endpoints

在 Visual Studio 2010 中,不論您是否明確指定端點,WebServiceHost 都會自動建立預設端點。In Visual Studio 2010, WebServiceHost automatically created a default endpoint whether you explicitly specified an endpoint or not. 在 Visual Studio 2012 和更新版本中,如果未明確新增任何端點,WebServiceHost 只會建立預設端點。In Visual Studio 2012 and later, WebServiceHost only creates a default endpoint if no endpoints are explicitly added. 如果您的用戶端預期預設端點,您可以明確地新增端點並將用戶端指向該端點。If your client is expecting the default endpoint you can explicitly add an endpoint and point the client to it. 或者,您可以將下列設定加入您應用程式的組態檔,告知 WCF 還原成之前的行為Alternatively, you can tell WCF to revert back to the previous behavior by adding the following setting to you application’s configuration file

<appSettings>
    <add key="wcf:webservicehost:enableautomaticendpointscompatability" value="true"/>
  </appSettings>

IHttpCookieContainerManagerIHttpCookieContainerManager

這個由 IChannelFactory<TChannel> 公開的介面,可讓您在用戶端上更輕鬆地使用 Cookie。This interface, exposed by IChannelFactory<TChannel>, makes working with cookies on the client side much easier. 當繫結上的 AllowCookies 設定為 true 時,您可使用下列程式碼存取 Cookie:When AllowCookies is set to true on the binding, you can access cookies by using the following code:

IHttpCookieContainerManager cookieManager = factory.GetProperty<IHttpCookieContainerManager>();
System.Net.CookieContainer container = cookieManager.CookieContainer;

之後,您即可從 CookieContainer 擷取或設定 Cookie。You can then retrieve or set the cookies from the CookieContainer. 當 AllowCookies 設為 false 時,您可使用 OperationContext 手動擷取 Cookie,並使用另一個 OperationContext 或訊息偵測器,在其他要求中傳送此 Cookie。When AllowCookies is set to false, you can manually retrieve the cookies using OperationContext and send it in other requests using another OperationContext or message inspector. IHttpCookieContainerManager 介面可讓您驗證使用者與服務,並使用該服務傳回的驗證 Cookie 來驗證其他服務。The IHttpCookieContainerManager interface allows you to authenticate a user with a service and use the authentication cookie returned by that service to authenticate with other services.