• No results found

Sample client code to consume the service

Note: If Metadata Exchange Endpoint is enabled in service configuration , you will get below error. Make sure it is commented.

Security settings for this service require 'Anonymous' Authentication but it is not enabled for the IIS application that hosts this service

If Client and Service configuration is not properly configured with “Windows Authentication”, you will get below error. Make sure both are using same config settings.

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'.

What's new in WCF 4.5

This article explains about the new features introduced in WCF 4.5. Microsoft has brought new feautes in WCF 4.5 to reduce the developers job and maintain the code

All new features of WCF 4.5 is explained in different parts  Task-based Async Support, Config Validation  Contract-First Development, Simplified config  XmlDictionaryReaderQuotas, Configuration tooltips  Streaming Improvements, WebSocket, Single WSDL  HTTPS with IIS, Configuring WCF Services in Code  Compress the Binary Encoder, ChannelFactory Caching  UDP Endpoint, Multiple Authentication

What's new in WCF 4.5 - Part 1 WCF Configuration Validation

 In old version of WCF, configuration files are not validated while building the project. But in WCF 4.5 while compiling the project, validation errors will be displayed as warning message in visual studio.

Task-based Async Support

 In WCF4.5, by default Task based async service operations methods are generated while Adding Service Reference. This is done for both synchronous and asynchronous method. This allows the client application to call the service operation using Task based programming model.

 In WCF 4.5, if you select the “Generate asynchronous operations” while adding service reference. Old form of async operations methods are created in the proxy class

Fig 1: Add service Reference from WCF 3.5

Fig 2: Add service Reference from WCF 4.5

Fig 4: Service Client proxy generated using WCF 4.5

What's new in WCF 4.5 - Part 2

Simplified Generated Configuration Files

 In old version WCF, configuration files generated at the client side will have all default setting and it looks complex. But in WCF 4.5, when you generate the configuration file using SvcUtil.exe tool or Service Reference only non-default value will be available in the configuration file.

Fig 1: Configuration file generated in WCF 4.0

Fig 2: Configuration file generated in WCF 4.5

Contract-First Development

 In WCF4.5, Using "SvcUtil.exe" you can generate only the contract files, in old version this feature is not available. If you create the proxy using "SvcUtil.exe", system will generate the contract, service client operation and data contract in single "service.cs" file. If you want to generate only contract you can use this simple command "/serviceContract".

Fig 3:Contract file creation using SvcUtil.exe

What's new in WCF 4.5 - Part 3 Default - ASP.NET Compatibility Mode

In general WCF service hosting AppDomain can run in two different mode

Mixed Transports Mode(Default): In this mode does not participate in ASP.NET HTTP pipeline and it behaves consistently independent of hosting environment and transport.

ASP.Net Compatibility Mode: In this mode, service will participate in ASP.Net HTTP pipeline and it is similar to old web service (.asmx). ASP.NET features such as File Authorization, UrlAuthorization, and HTTP Session State are applicable to services running in this mode

In WCF 4.5 default aspNetCompatibilityEnabled attribute to true in web.config Fig 1: WCF 4.5 Configuration file

Fig 2: WCF 4.0 Configuration file

Configuration tooltips

XmlDictionaryReaderQuotas

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 need to set them explicitly

MaxArrayLength Int32.MaxValue MaxBytesPerRead Int32.MaxValue

MaxDepth 128 nodes deep

MaxNameTableCharCount Int32.MaxValue MaxStringContentLength Int32.MaxValue What's new in WCF 4.5 - Part 4

Streaming Improvements

WCF 4.5 support for true asynchronous streaming. Let see what is mean by true asynchronous streaming  In previous version, When WCF service is sending streamed messages to multiple clients. Service will be

block its thread to transfer to next client till the slow client is received the message.

 But in case of WCF4.5, service does not block one thread per client anymore and will free up the thread to service another client

How to enable asynchronous streaming?

 In previous versions of WCF when receiving a message for an IIS-hosted service that used streaming message transfer, ASP.NET would buffer the entire message before sending it to WCF. This would cause large memory consumption.

 This buffering has been removed in .NET 4.5 and now IIS-hosted WCF services can start processing the incoming stream before the entire message has been received, thereby enabling true streaming.

Generating a Single WSDL Document

In WCF 4.0 and lower version, generated wsdl file does not contain all metadata information in single file. WSDL file will refer the other document using ‘import’ statement as shown below. So when third party try to consume the service using WSDL has to explicitly add these references.

In WCF 4.5, service provide two option to create the wsdl, we can create wsdl information in single file

WebSocket Support

WebSocket is a web technology providing dbi directional communications channels over a single TCP connection. WebSocket is designed to be implemented in web browsers and web servers, but it can be used by any client or server application. The WebSocket Protocol is an independent TCP-based protocol.

Two new bindings are added in WCF4.5 to support communication over WebSocket. NetHttpBinding NetHttpsBinding

Simple to expose HTTPS with IIS

WCF 4.0

WCF 4.0 has one of new feature called default endpoint, when ever new service is created default endpoint created with ‘basicHttpBinding’. But if you need to expose the service in ‘HTTPS’ binding then you need to Explicitly create the endpoint and provide all information.

WCF 4.5

In WCF 4.5, default endpoint can be created with HTTPS binding, by simply specifying the SSL and enabling ‘https’ in IIS setting

How to create HTTPS with default endpoint?

Step 1:Create the WCF service and browse the WSDL, you will find only basicHttpBinding as shown below Fig 1 : default 'basicHttpBinding'