I ran into this problem a couple of days ago. I’m working on an n-tier application that uses WCF services hosted in IIS under Vista & Windows 2008. We’ve got unit tests to test each tier and they were mostly passing.
We unit test our service code both directly by reference (unhosted) and then unit test them again when they’re hosted in IIS. They worked great when they were unhosted but the hosted ones had intermittent problems. More and more it looked like IIS was hanging and once the hosted tests started failing, the only way to make them pass again was to restart IIS by running iisreset.
A weird thing was that all of these unit tests ran just fine on their own. They only failed when you ran them with other tests but there wasn’t any particular test that failed every time. The tests would run at normal speed and then one of them would just sit there forever and would eventually time out. Once you got the first failure/timeout, all the rest of the tests would fail, too.
Here’s the error:
PeopleManagement.UnitTests.HostedServiceTests.HostedPeopleManagementServiceMembershipSearchFixture.HostedServiceSearchMembershipsByMembershipType threw exception: System.TimeoutException: The request channel timed out while waiting for a reply after 00:00:59.9990001. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout. —> System.TimeoutException: The HTTP request to ‘http://localhost/PeopleManagement.IisServiceHost/PeopleManagementService.svc’ has exceeded the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout. —> System.Net.WebException: The operation has timed out.
Something about this had the smell of a threading problem.
I started discussing this problem with my friend and co-worker, Michael Stiefel, and he agreed that this sounded like a threading problem. Once we were both thinking “threading problem”, I started thinking about where there’s something multi-threaded in this unit test. The only multi-threaded part is IIS. Then like a flash of lightning, I noticed that it didn’t matter which tests I ran but HOW MANY I ran. It was always the 11th test that failed. Pick any 11 tests — the first 10 passed and the 11th always failed!
Then another flash of mental lightning and I realized that I’m probably doing something dumb in my code.
Answer: My team and I were creating instances of the Service Reference proxies to the WCF service but we never called Close() on the proxy. (Duh.)
Since we’re writing services that are only HTTP and since HTTP is stateless, it never occurred to me that I’d have any connection to close. (Nope. Not true.) Michael Stiefel dug up this quote from Michele Bustamante:
Don’t forget to close your proxy. If you forget, you will find some interesting results. You will get unexpected behavior and will not know why.
Whew! Ain’t that the truth!
-Ben
Leave a Reply