Tag Archives: tips and tricks

“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

“Data Access Hacks and Shortcuts” Stephen Forte

[MS Techdays 2009] Summary “Data Access Hacks and Shortcuts” Stephen Forte

Not very interesting so I haven’t a lot of notes. For details you should have a look at the conference slides.

Pass .NET collections to Stored Procedures
SQL Sever 2008 allows table valued variables. This in combination with Ado .Net 3.51 allows you to pass DataSets as variables to Stored Procedure. Moreover C# 3.0 can convert any collection, implementing IEnumerable, into a DataSet. So custom collections can be passed to stored procedures as variables.

Spying on your DB Server
LINQ will convert IQueryably to TSQL queries. You can spy on these queries by using following tools:

Over-normalization tackled with Views
If your DBA has gone overboard and created an over-normalized database it is better to create views then to o have queries with lots of unions between tables. These views will deconstructs complex data and optimize lookups.

What about updates? You still can access your tables in the normal way.

Binding REST data to Silverlight
Use LINQ to ADO.NET Data Services on the Silverlight client leverages your investments in LINQ but requires you to be 100% asynchronous.

Reporting DB design Online transactions have different needs when accessing a DB compared to reporting. The first are optimized for inserts. The second focus on fast selects. Why are we trying to do this on the same DB?

It is better to create a reporting DB next to the traditional OLTP DB. Move aggregated data to your data warehouse that is denormalized for fast access to optimally support reporting.

How to keep both in sync? You can create ETL code to move form your OLTP DB to your data warehouse but it should be clear there always will be latency between what’s online and what’s available in the data warehouse. Allowing no latency will remove all advantages because every update is synchronized immediately and affecting the reporting part.