Working with WebServices

by Erik Lane 15. June 2006 17:53

Three things I've hit recently:

1.  You can get an unexpected timeout when you can make a lot of calls at once to the same webservice.
My NUnit tests are hitting our test harness webservice.  If I ran two or three everything would be fine.  But if I ran the complete suite of tests I would start getting an exception after about the third or fourth test.  The exception was "The underlying connection was closed: An unexpected error occurred on a receive".  I'll be honest, I'm not real sure how to actually fix this problem and I don't control the hardware/setup of the site that will host the webservice.
 
I got my fix from Denis Pitcher which had me add some code to the references.cs file to set the KeepAlive property to false.  Microsoft now has a KB article on the topic that explains more about the problem but also has a good suggestion that keeps you from having to update the references.cs file each time you update the web reference.  Just create a new class that inherits from the generated proxy class and then override the GetWebRequest method so you can set the KeepAlive property to false.  Done!
 
2.  Read-only properties are not exposed by XML webservices.
Apparently you have to have both a get and set on objects that you want to expose via your webservice.  This is by design and so now I've got some exceptions in place to keep people from setting values that they shouldn't be.  :-)

3.  Derived classes are not serialized by default like their base classes.
This had me banging my head on the desk while debugging this cool little "feature".  It makes sense now but at the time I was getting pretty frustrated.  Craig Andrea does an excellent job of explaining it an giving an example.  I've seen two ways to get at it this.  Both require using the XmlInclude attribute but one is placing it on the WebMethod and the other is placing it on the base class.  I'm choosing to place it on the base class.  I figure if I need to add another subclass to the mix this will be the only place I'll need to go.
 
Ahh the fun of developing code.  It can be frustrating at times but its still fun.

Tags:
Comments are closed