WCF to ASMX binding much slower returning large data than standard web reference
I am binding to an existing ASMX web service using a WCF service reference. I have a method that returns a DataSet object that contains a string of 500k length.
As a standard "old-school" web reference, calling this method takes around 2-3 seconds to complete. As a WCF service开发者_C百科 reference this is taking 20+ seconds to complete. This is having an impact on our systems now :(
I've tried altering the bindings to max out all the maxReceivedMessageSize and maxBytesPerRead etc, but it hasn't made any difference.
Why is the WCF reference so much slower and what can I do to fix this?
It would seem that there are a few issues here.
First, WCF services, being stateful unless otherwise configured, have to be activated on every new connection. This activation is slowed by SSL due to additional trips back and forth for authentication. As one of the questions/suggestions led to above, try to make the initial connection and then attempt to execute the method 10 times or so and measure the time it takes for subsequent calls, it should be much faster after the initial call. In fact, according to MS, WCF is more efficient in handling calls than the "old school" ASMX, but is still subject to an activation time.
Next, WCF has some crazy serialization action happening in the background and does not play well with a DataSet object, because of the insane amounts of overhead and metadata that the DataSet object has. If a larger dataset MUST be used, you should change the serialization of the stream to MTOM or Binary (but that only works if your clients are also WCF/.NET based). The other option would be to not use DataSets, which is probably the best option when it comes to WCF. Here is a link to an interesting article about the speed of serializing datasets.
An additional article on why not to use DataSets.
ref:
http://msdn.microsoft.com/en-us/library/bb310550.aspx
http://blogs.microsoft.co.il/blogs/oshvartz/archive/2011/07/23/wcf-performance-using-datasets-part-2.aspx
http://www.hanselman.com/blog/ReturningDataSetsFromWebServicesIsTheSpawnOfSatanAndRepresentsAllThatIsTrulyEvilInTheWorld.aspx
You do need to look at a number of aspects, not least that you are comparing like for like in terms of data quantities. It may be that there are other factors that have caused the slow down.
My first question would be why have you changed it to WCF? Was there a specific reason for this - I know it is the right way to go but is there a specific reason that meant this needed to be changed now? Were there any changes to the interface that might have had wide ranging effects.
The WCF is liable to be slightly slower, because of the object processing, but it should not have anything like this difference. Which would all point to something else in the environment being the real issue.
What type of binding is being used? There is the BasicHttpBinding and WSHttpBinding, and to simulate an asmx, BasicHttpBinding is recommended. In general, WCF should be at least comparable to their asmx counterpart. If that doesn't help, try tweaking the concurrency settings and instantiation modes for the WCF.
精彩评论