“WCF Tips and Tricks ” Christian Weyer

[MS Techdays 2009] Summary “WCF Tips and Tricks ” Christian Weyer

WCF
WCF is not just about web services (only consumption of web services) and not always about SOA (cool SOA framework). It is about Communication!

Consuming
Tips:

  • Follow the ABC-rule: Address, Bind and Contract.
  • For non-interop we don’t need proxy classes. Shared contracts work fine in WCF-to-WCF scenarios. Share contracts not types.
  • For ChannelFactories always try to close the channel.
  • Avoid using statements in proxies. These can throw network exceptions at the end of the using block. So use an explicit close with explicit exception handling.
  • Caching proxy or ChannelFactories avoid high overhead for setting-up and tearing-down in every call.

Examples:

WCF1

Hosting
Tips:

  • Use IIS always for robust high scalable hosting. Self-hosting in Windows Services is light-weighted and allows for more control.
  • Custom initializations can be realized through custom ServiceHost and ServiceHostFactory classes.

Examples:

WCF2

Binding
Tips:

  • Try to work session-less. By default WsHttpBinding is used which means heavy security and is session-based. Sessions also have the advantage that the security hand-shake is only done once. Wrong binding options can affect performance.
  • SOAP formatter is preferred over binary formatter. The argument most often used: ‘Because it is human readable.’Weyer’s? reaction: ‘Yeah right!’ In WCF-to-WCF communication the binary formatter is optimal for speed. Only in interop situations choose for angular brackets i.e. XML

Examples:

WCF3

Performance
Tips:

  • WCF is very conservative and is written to avoid DNS attacks. This means although default quota’s seem to be unbounded, WCF enforces quotas!

WCF4

To achieve real unbounded quota? you?l have to set these values to MaxValue. So never use the defaults but use some custom framework to change the values by default to MaxValue or to any other suited value.

Examples:

WCF5

Metadata
Tips:

  • Do not rely on the ?wsdl?because it is dynamically generated. Create a static flat WSDL file. By flat we mean avoid includes of other files.
  • Use IIS host headers to reflect names into WSDL instead of localhost. .Net 3.5 SP 1 supports multiple bindings without requiring multiple IIS-site base addresses.

Examples:

WCF6

Large Data
Tips:

  • Large data means avoiding SOAP messages.
  • Technical solutions are chunking and streaming:
    • Chunking: chopping the data payload into chunks, sent the chunks over the wire and glue back the chunks together. Chunking Channels are available as SDK and work transparently with different transports and bindings.
    • Streaming: requires contracts to follow the stream and message shape. Can’t be used in combination with MSMQ.
  • Alternative is the MTOM framework for encoding binary data. This is an alternative to SOAP in the case interop is required. So don’t be alarmed if you don’t have XML documents in an interop context!

Examples:

WCF7

Performance
Tips:

  • Cache, cache and cache! The problem is that to enable the scale out of your application to a farmed environment we require distributed caches which are only available in specialized frameworks.
  • Client site HTTP communication is limited by default to two concurrent connections but this is configurable through System.Net.

Examples:
WCF8

Tracing
Tips:

  • Tracing can save your day but don? overuse it in production! Tracing is organized through configuration.

Examples:

WCF9

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.