wcf tips tricks a selection from the field n.
Skip this Video
Loading SlideShow in 5 Seconds..
WCF Tips & Tricks [a selection] from the field PowerPoint Presentation
Download Presentation
WCF Tips & Tricks [a selection] from the field

Loading in 2 Seconds...

play fullscreen
1 / 40

WCF Tips & Tricks [a selection] from the field - PowerPoint PPT Presentation

  • Uploaded on

WCF Tips & Tricks [a selection] from the field. Christian Weyer Solution Architect thinktecture christian.weyer@thinktecture.com. and Christian Weyer. Support & consulting for Windows and .NET software developers and architects Developer coaching and mentoring

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

WCF Tips & Tricks [a selection] from the field

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
wcf tips tricks a selection from the field

WCF Tips & Tricks[a selection] from the field

Christian Weyer

Solution Architect



and christian weyer
and Christian Weyer
  • Support & consulting for Windows and .NET software developers and architects
    • Developer coaching and mentoring
    • Architecture consulting and prototyping
    • Architecture and code reviews
    • Applicationoptimization, troubleshooting, debugging
  • Focus on distributed applications, service orientation, workflows, cloud computing, interoperability, security, end-to-end solutions
    • Windows Server, WCF, WF, MSMQ, Azure Services, Windows Azure
  • http://www.thinktecture.com
  • christian.weyer@thinktecture.com
for starters super duper tip
For starters: Super-duper TIP
  • WCF is not just Web Services !
  • WCF is not always about SOA !
  • WCF does not stand for
    • Web Services Consumption Foundation
    • Windows Cool-SOA Foundation
  • It is the Windows CommunicationFoundation!
agenda dealing with
Agenda – Dealing with...
  • Consuming
  • Hosting
  • Bindings
  • Quotas & throttles
  • WSDL
  • Performance & throughput
  • Large data
  • Tracing

Problems Solutions/Tips 


not covered some
Not covered, some...
  • Contract modelling
  • REST
  • Security
  • Asynchronous processing
  • Fault handling
  • Deep extensibility
  • WF integration
  • NATs & firewalls
  • ... and surely more ...
consuming problems
Consuming – Problems
  • Do I always need to create a proxy class from WSDL/MEX?
  • How can I make consuming services more robust?
  • Is there a way to improve performance when calling services?
consuming solutions i
Consuming – Solutions I
  • For non-interop no need to use svcutil.exe or ‘Add Service Reference’
    • shared contracts approach works good in WCF-to-WCF scenarios
    • ChannelFactory<T> and DuplexChannelFactory<T> are powerful means
    • use custom interface extending service contract & IClientChannel
  • Avoid using statement when dealing with proxies (ICommunicationObject-derived objects)
    • can still throw at end of using block, e.g. for network errors
    • explicit exception handling; can be dealt withe.g. in extension method
consuming solutions ii
Consuming – Solutions II
  • Try to cache proxy or ChannelFactory in high-throughput applications
    • creating them can mean significant overhead
    • ASP.NET client applications should not create ChannelFactory on each page call
consuming samples
Consuming – Samples

Client side

interface IMyContractChannel : IMyContract, System.ServiceModel.IClientChannel {}

ChannelFactory<IMyContractChannel> cf = new ChannelFactory<IMyContractChannel>(binding, address);

IMyContractChannel client = cf.CreateChannel();





… client.Close();


catch (CommunicationException e)


… client.Abort();


catch (TimeoutException e)


… client.Abort();


catch (Exception e)


… client.Abort();



Client side

Client side

IMyContractChannelchannel =



Client side

hosting problems
Hosting - Problems
  • Which host to use? IIS/WAS or a self-host?
  • How do I inject logic in IIS/WAS hosting?
hosting solutions
Hosting – Solutions
  • Use IIS/WAS for robust, highly scalable services
    • beware of the process & AppDomain lifecycle features
    • when using non-HTTP (TCP, Named Pipes, MSMQ) with WAS hosting AppDomain recycling still comes into your way
  • Use self-hosting in Windows Service to have full control and light-weight hosting environment
  • Custom ServiceHost & ServiceHostFactory implementations to provide custom initialization code
    • hook up factory in .svc file for IIS/WAS
hosting samples
Hosting – Samples

class MyServiceHost : System.ServiceModel.ServiceHost


public MyServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses)




protected override void ApplyConfiguration()





Custom ServiceHost

class MyServiceHostFactory : System.ServiceModel.Activation.ServiceHostFactory


protected override ServiceHostCreateServiceHost( Type serviceType, Uri[] baseAddresses)


return new MyServiceHost(serviceType, baseAddresses);



Custom ServiceHostFactory

<%@ ServiceHost Language="C#" Debug="true" Service="MediaService" Factory="MyServiceHostFactory" %>

.svc file

bindings problems
Bindings - Problems
  • My WCF service is slow, what is happening?
  • I want to use HTTP but not necessarily angle brackets (aka ‚XML‘)
  • How can I choose from the best communication options?
bindings solutions i
Bindings – Solutions I
  • Beware of using the wrong bindings
    • e.g. Visual Studio WCF wizards use WsHttpBinding(heavy with message security & session-based)
    • only use features you really need
  • Think about the real need for session-bound channels/bindings
    • sessions change the game of fault and error handling
    • use sessions when you need session semantics in your service
  • But: sessions can give performance improvements
    • e.g. security token hand-shake happens only once with SecureConversation
bindings solutions ii
Bindings – Solutions II
  • Custom bindings will save your day
    • e.g. binary over HTTP often a good trade-off for WCF-to-WCF communication scenarios
    • build custom binding in config or code
  • Create user-defined binding for easier re-usage
    • bake common custom binding setups into re-usable code and config implementations
bindings samples
Bindings - Samples



<add name="netHttpBinding"

type="NetHttpBindingCollectionElement, Thinktecture.ServiceModel, Version=…" />





<binding name="unsecureNetHttp" securityMode="None" />



<binding name="binaryHttp">

<binaryMessageEncoding />

<httpTransport />




public class NetHttpBinding :











User-defined binding

quotas throttles problems
Quotas & Throttles - Problems
  • Beyond Hello World, all my services and consumers fail with strange exceptions
  • My services do not perform the way they are supposed to
  • How can I teach WCF to be less ‘conservative’ in closed Intranet environments?
quotas throttles solutions
Quotas & Throttles - Solutions
  • Bindings
    • adjust buffers, connection limits, timeouts
  • Behaviors
    • configure throttling service behavior
  • Serializers
    • check maximum items in object graph value
  • Custom ChannelFactoryand ServiceHost can automate all this
    • e.g. through profiles


not true!


quotas throttles samples
Quotas & Throttles - Samples



<binding name="rawStreamingWeb" transferMode="StreamedResponse">





<binding name="httpStreaming" sendTimeout="Infinite">

<binaryMessageEncoding />

<httpTransporttransferMode="Streamed" />






<behavior name="MyServiceBehavior">

<serviceThrottlingmaxConcurrentCalls="1500" maxConcurrentInstances="1500" maxConcurrentSessions="1500" />






Consuming code

wsdl problems
WSDL - Problems
  • Some non-WCF consumers cannot understand the WSDL WCF produces
  • My WSDL contains the wrong host name
wsdl solutions
WSDL – Solutions
  • Use custom extension to flatten WSDL into one file
    • need to use same namespace values forServiceContract, ServiceBehavior, BindingNamespace
    • eliminates wsdl:import and xsd:import
  • Register hostheaders in IIS toreflectnamesinto WSDL
    • for HTTP and HTTPS
  • Specify different URIs forlisteningandexposing in WSDL

<endpointaddress="https://www.tt.com/TheUriIWantInWSDL" listenUri="http://localhost/TheActualUriServiceListensToOnThisBox" ...>

  • Considerexposing a static WSDL whichdocumentsyourpublishedinterfaceversion
wsdl samples
WSDL - Samples

<%@ ServiceHost Language= "C#" Service="ProductCatalog" Factory="Thinktecture.ServiceModel.Activation.FlatWsdlServiceHostFactory" %>

.svc file

applicationHost.config (IIS7)


<binding protocol="http”bindingInformation="*:80:www.thinktecture.com" />

<binding protocol="https" bindingInformation="*:443:www.thinktecture.com" />


large data problems
Large Data - Problems
  • My service eats a lot of memory and chokes the CPU when sending/receiving large data
  • Bigger messages are making my communication really slow
  • I have arbitrary, non-structured data to transfer
large data solutions i
Large Data – Solutions I
  • WCF supports MTOM for encoding binary data
    • MTOM especially useful for interop
  • Chunking channels available as SDK & community samples
    • enables sending chunks of data instead of one single piece
    • transparently works with different transports as a binding element
large data solutions ii
Large Data – Solutions II
  • Consider using streaming for transfering abitrary data
    • requires certain contract shape
      • Stream
      • Message
      • Stream as single body in MessageContract
    • works over any transport besides MSMQ
    • works with transport and mixed-mode security
    • still watch out for quotas
    • powerful with web programming model
large data samples
Large Data - Samples


public interface IVideoPlayer



[WebGet(UriTemplate = "videos/{videoID}")]

[WebContentType(MimeType = "video/x-ms-wmv")]

Stream Play(string videoID);


Service contract


WebServiceHostwebHost = new WebServiceHost(


WebHttpBinding binding = new WebHttpBinding();

binding.TransferMode = TransferMode.Streamed;






performance throughput problems
Performance/Throughput - Problems
  • Somehow my entire WCF-based application is ‚slow‘
  • Hosting my WCF service in IIS seems not to perform well under high load
  • I cannot seem to get a high throughput when clients talk to my service via HTTP
  • All that data is being transferred again and again, it makes my system slow
performance throughput solutions
Performance/Throughput – Solutions
  • Configuring throttling can heal a lot (look there!)
  • .NET 3.5 SP1 provides asynchronous HTTP module & handler for hosting WCF in IIS for better behavior
  • Client-side HTTP communication is limited to 2 concurrent connections to a server
    • configurable through System.Net
  • Cache, cache, cache!
    • try to use caching intensively (but wisely) to save unnecessary round-trips
performance throughput samples
Performance/Throughput - Samples



<add address="*" maxconnection="20"/>




public List<Episode> ListEpisodes()

{IDataCache cache = DataCacheFactory.CreateInstance();

List<Episode> episodes = cache.Get<List<Episode>>(CacheConstants.AllEpisodes);

if (episodes == null)


varepisodeList = mediaLogic.ListAllEpisodes();

episodes = EpisodeDataMapper.MapAllEpisodes(episodeList);

cache.Add(CacheConstants.AllEpisodes, episodes);


return episodes;


public interface IDataCache


void Add(string key, object cacheItem);

TCacheItem Get<TCacheItem>(string key);

void Remove(string key);


E.g. service facade

Caching lib

special case tracing
Special Case: Tracing
  • Use it! It can save your… 
  • If things go wrong and you have no clue why: trace!
  • But do not overuse it when in production
    • wrong usage can mean severe overhead
  • Configured via config file
    • can be manipulated via code, but only through WMI
  • Did I already say tracing can save your …?
tracing sample
Tracing - Sample



<sourcename="System.ServiceModel" switchValue="Warning" propagateActivity="true">


<add type="System.Diagnostics.DefaultTraceListener" name="Default" />

<addname="ServiceModelTraceListener" />






type="System.Diagnostics.XmlWriterTraceListener, …"

name="ServiceModelTraceListener" traceOutputOptions="Timestamp" />


<traceautoflush="true" />


PS script

$ms = get-wmiobject -class "AppDomainInfo" -namespace "root\servicemodel" -computername "." | where {$_.Name -eq "MyWCFHost.exe"}

$ms.TraceLevel = "Warning, ActivityTracing"






  • Avoiding problems with the using statement
    • http://msdn.microsoft.com/en-us/library/aa355056.aspx
  • Custom encoders
    • http://msdn.microsoft.com/en-us/library/ms751486.aspx
    • http://blogs.msdn.com/drnick/archive/2006/05/16/598420.aspx
  • Tracing
    • http://msdn2.microsoft.com/en-us/library/ms732023.aspx
    • http://msdn2.microsoft.com/en-us/library/aa751795.aspx
    • http://msdn2.microsoft.com/en-us/library/ms733025.aspx
    • http://msdn2.microsoft.com/en-us/library/aa751917.aspx
  • Setting up IIS SSL hostheaders
    • http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/596b9108-b1a7-494d-885d-f8941b07554c.mspx
    • http://blogs.iis.net/thomad/archive/2008/01/25/ssl-certificates-on-sites-with-host-headers.aspx
  • baseAddressPrefixFilter
    • http://msdn.microsoft.com/en-us/library/bb924492.aspx
  • Chunkingchannel
    • http://code.msdn.microsoft.com/WCFResources/Release/ProjectReleases.aspx?ReleaseId=1546
  • Asynchronous WCF HTTP Module/Handler for IIS7 for Better Server Scalability
    • http://blogs.msdn.com/wenlong/archive/2008/08/13/orcas-sp1-improvement-asynchronous-wcf-http-module-handler-for-iis7-for-better-server-scalability.aspx
  • WCF bindings & more
    • http://www.noemax.com
  • We have code solutions for some WCF problems, for free of course
    • Thinktecture.ServiceModel
  •  Email Christian Weyer
    • christian.weyer@thinktecture.com
  • Weblog Christian Weyer
    • http://blogs.thinktecture.com/cweyer
  • thinktecture
    • http://www.thinktecture.com



In-depth support and consulting for software architects and developers




y our msdn resources check out these websites blogs more
Your MSDN resourcescheck out these websites, blogs & more!

PresentationsTechDays: www.techdays.chMSDN Events: http://www.microsoft.com/switzerland/msdn/de/presentationfinder.mspxMSDN Webcasts: http://www.microsoft.com/switzerland/msdn/de/finder/default.mspx

MSDN EventsMSDN Events: http://www.microsoft.com/switzerland/msdn/de/events/default.mspxSave the date: Tech•Ed 2009 Europe, 9-13 November 2009, Berlin

MSDN Flash (our by weekly newsletter)Subscribe: http://www.microsoft.com/switzerland/msdn/de/flash.mspx

MSDN Team BlogRSS: http://blogs.msdn.com/swiss_dpe_team/Default.aspx

Developer User Groups & CommunitiesMobile Devices: http://www.pocketpc.ch/Microsoft Solutions User Group Switzerland: www.msugs.ch.NET Managed User Group of Switzerland: www.dotmugs.chFoxPro User Group Switzerland: www.fugs.ch

y our technet resources check out these websites blogs more
Your TechNet resourcescheck out these websites, blogs & more!

PresentationsTechDays: www.techdays.ch

TechNet EventsTechNet Events: http://technet.microsoft.com/de-ch/bb291010.aspx Save the date: Tech•Ed 2009 Europe, 9-13 November 2009, Berlin

TechNet Flash (our by weekly newsletter)Subscribe: http://technet.microsoft.com/de-ch/bb898852.aspx

Schweizer IT Professional und TechNet BlogRSS: http://blogs.technet.com/chitpro-de/

IT Professional User Groups & CommunitiesSwissITPro User Group: www.swissitpro.chNT Anwendergruppe Schweiz: www.nt-ag.chPASS (Professional Association for SQL Server): www.sqlpass.ch

save the date for tech days next year

Save the date for tech·days nextyear!

7. – 8. April 2010Congress Center Basel


Premium Sponsoring Partners

Classic Sponsoring Partners

Media Partner