开发者

Unit test break/assert lockin up WCF service?

开发者 https://www.devze.com 2023-01-14 08:11 出处:网络
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

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?


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();
     }
}


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号