Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using WCF from WPF very slow on first use

I have been struggling for a few days with an issue with our WPF applications and I wonder if someone has come across this before and can help? The problem seems to boil down to the client generating "on-the-fly" a serializer to handle the types in that web method call. When that method is called for the first time (the web service itself has been running already), it may take e.g. 8 seconds, subsequent calls may take e.g. 20ms. The CPU on the client WPF process is v. high during this delay.

When using the XmlSerializer, there is a way of pre-generating these serializer assemblies, using svcutil. When (as we are) using the normal WCF DataContractSerializer, this option does not seem to be present.

What I would like is to be able to pre-generate this assembly for all types in all my data contracts (a lot) or, alternatively, to replace this process with a custom one that I can code and passes the data in binary (we own both ends of this webservice/client and they are both .NET 4). I have already used BinaryForamtter and GZip compression and while this speeds up the transfer of data, it always gets restored to XML to be de-serialized by the framework, hence this problem remains.

Any ideas?

like image 704
Simon Evans Avatar asked Oct 11 '12 09:10

Simon Evans


1 Answers

You can use a binary library such as protobuf-net, which is quite fast, even if there is an initial startup cost because code has to be generated for each type, it's still way better than DataContractSerializer or BinaryFormatter. You should gain a few seconds and have an overall smoother experience. It can be easily integrated with WCF. Keep in mind that WCF will still inspect your various contracts to generate the correct WSDL and various metadata.

There are other things that can slow down WCF startup, such as determining the default web proxy. Be sure that useDefaultWebProxy is false in your binding configuration if you don't have any use for it.

Still, you will find that WCF startup is generally slow no matter what you do to optimize it. Personally, tired of fighting slowness in a similar scenario (I controlled both ends, and the client was a WPF application), I simply ditched WCF and went for ServiceStack + protobuf-net. The first call went from 2-3 seconds to ~100ms, and all subsequent HTTP calls are really instantaneous. The overall user experience has greatly improved. Note that I'm in no way affiliated with ServiceStack, this is just my experience.

like image 103
Julien Lebosquain Avatar answered Sep 21 '22 08:09

Julien Lebosquain