views:

34

answers:

2

I am trying to write some unit tests (more integration tests actually) to hit a live IIS server hosting my WCF service. Whenever I run a test though, if one of my Assert statements fails on the client side, my WCF service seems to lock up- and I have to do an iisreset to get things back online.

For example, I have in a test method 3 calls from my service client to WCF service- Call1, Call2 and Call3. The first time through, Call1 works great, Call2 works great and then Call3 fires the Assert because some data is not correct. On the next time through the test, Call1 fails with the following error:

"An exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll but was not handled in user code

Additional information: An error occurred while receiving the HTTP response to http://localhost/Kiosk/KioskSite.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."

After doing an iisreset everything is okay again.

Anyone have any ideas what might be going on?

A: 

These do not sound like Unit tests. Unit tests isolate pieces of code and test them individually, it sound liek you are writing integration tests.

Why are you testing the WCF call at all can you not test the server side method in isolation without worrying about WCF?

Burt
Thats why I said they were more integration tests. And the server side method tests work just fine. Maybe I shouldnt be using the test framework at all for these kinds of tests- but I need to make sure that I can retrieve data, and its the correct data. The other alternative is a repository pattern but I think this is still valid.
Nicros
I don't think you should be using a test framework unless they are automated UI driven tests. It is the same as testing ASP.Net posts correctly if you know what I mean.
Burt
I disagree with Burt - I've used NUnit for integration tests and they worked fine - once I sorted out the cleanup after each test. It seems that the WCF service can get itself into a bad state, which is a problem with the service, not the test framework.
Andrew Shepherd
What did you need to do to sort out the cleanup?
Nicros
+1  A: 

If you have to restart IIS then it seems you have a problem with state management in your service. Your test has been a successful test in that it's exposed this problem - a badly behaved client can freeze the service.

I can suggest three things to try in your investigation:

1) Create a console host for your service and see if the problems still happen. This will determine if it's an IIS issue.

2) Change the concurrency mode on your service, using a service behavior.

   [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple,UseSynchronizationContext=false)]
   class MyServiceImplementation : IMyService
   {
      // ...
   }

3) Make absolutely sure that you are calling Close on any proxies you create, even when a test fails. A service can have only a limited number of proxies connected to it at once. (The default is 10)

[Test]
void ATestMethod()
{
     MyService proxyInstance = new MyService();
     try
     {
           Assert.IsTrue(proxyInstance.MethodC());
     }
     finally
     {
          proxyInstance.Close();
     }
}
Andrew Shepherd
No luck. I created a console app with a simple assembly reference to my service client project. Same error, even with the behavior you refer to above. I am not aware of creating any proxies- what else could it be?
Nicros
I've put more about proxies in my answer. A proxy is the class that exposes all the methods in the contract, and for each method bundles up the parameters into a message and sends it to the service. If you are calling a service the standard way, then you would be creating a proxy.
Andrew Shepherd
Gotcha, this is indeed what I am doing. The problem persists despite closing the proxy, but I am narrowing it down to something on the server side. Hopefully will nail it soon.
Nicros