Automated testing, both unit and integration, is the holy grail these days. I’m all up for the concept, and I’ve done a fair part of testing myself already. I realized at one point that my unit test were really integration test. But started with stubs and mocks, making my unit tests happy and green. But we all have to start somewhere, right?
But there are pitfalls, a lot of them. I’ve had a few projects in row now where we have used WCF Services as ordinary Web Services. Cause we are communication with “others”. Fairly simple, just add basicHttpBinding on your endpoints.
Exception logging
I want exception logging to happened on all my public methods. I prefer doing this with a [ErrorHandlerBehavior] described in another article here on my blog. This works fine, I am kind a liking that some magic happens on my service class, with logging the exception and throws it again.
Application Initialization
But then I realize I need a Application Start, to make some global configuration and settings run. This might be NHibernate etc. Now if I presume my WCF Service would always run inside IIS, I could use Global.asax in my service. Just add this file to the project, and hook up like this:
This is supposed to be a ordinary Web Service, so there is no problems with that. Except for my automated tests. When my test project launches, the service is hosted in the WCF Service Host. Which isn’t IIS. Obviously. Now I have to make my own Service Host, which triggers something like application start.
I fooled around with the AppInitialize method: public static void AppInitialize();
Which supposedly can be put in any type that is defined in a C# file in the application’s \App_Code directory. When the AppDomain is started, ASP.NET checks whether there is a type that has such as method (exact name and signature) and invokes it. I’m not sure if this could have work with an WCF Service, but I couldn’t make this work.
Self Hosting
So I made my WCF Service self hosted when tests are ran, but “normal” IIS hosted when run in production and test environment.
I used the SelfHosting.cs and the test script would look something like this:
[TestMethod]
[ExpectedException(typeof(FaultException<string>))]
[DeploymentItem(@"C:\\Code\\Foo\\Bin\\")]
public void OrderService_MyException_FaultExceptionWithMyArgumentExceptionAsString()
{
using (var host = new SelfHosting())
{
host.Start();
using (var client = new OrderServiceClient())
{
try
{
client.MyException();
}
catch (FaultException<string> ex)
{
Assert.AreEqual("System.ArgumentException", ex.Message);
Assert.AreEqual("MyException", ex.Detail);
throw;
}
}
}
//there should have been done some error logging by now.
}

You might also consider WCFStorm (http://www.wcfstorm.com). We use it at work for doing integration, functional and load testing of our in-house wcf services.
ReplyDeleteVery efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. automated access
ReplyDelete