Archives

Archives / 2009 / January
  • Integration Testing WCF Services with Unity

    I've been blogging a few times now about using Unity with WCF, but how do you integration test your service in an easy way without? The way I (and many others) do integration tests for a WCF service is by setting up my own service host and starting the service from test init:

        [TestClass]

        public class ServiceIntegrationTest

        {

            private static ServiceHost serviceHost;

     

            [ClassInitialize]

            public static void MyClassInitialize(TestContext testContext)

            {

                serviceHost = new ServiceHost(typeof(Service1), new [] { new Uri("http://127.0.0.1:8001/") });

                serviceHost.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "Service1");

                serviceHost.Open();

            }

     

            [ClassCleanup]

            public static void MyClassCleanup()

            {

                serviceHost.Close();

            }

     

            [TestMethod]

            public void Should_get_data()

            {

                var ep = new EndpointAddress("http://127.0.0.1:8001/Service1");

                var proxy = ChannelFactory<IService1>.CreateChannel(new BasicHttpBinding(), ep);

                var data = proxy.GetData(1);

                Assert.IsTrue(data == "You entered: 1", "Got wrong data back, got - '" + data + "'");

            }

        }

    By doing it this way I don't have to make sure Cassini is started up before the test or having to deploy the service to a local IIS. There is no need for web.config or app.config files and I don't have to add any service reference. This way of doing integration testing of services is described by many others, and it should work quite well on a TFS build server or similar.

    Integration Testing WCF Service with Unity

    A few blog posts away I wrote about using Unity with WCF, but how do you integration test a setup like that? Remeber that if you have created your own ServiceHostFactory, you specify the factory to use in the .svc markup using the Factory attribute this:

    <%@ ServiceHost Language="C#" Debug="true" Factory="IRM.Patterns.UnityService.UnityServiceHostFactory" Service="WcfService3.Service1" CodeBehind="Service1.svc.cs" %>

    The "problem" here is that the factory doesn't have any decent public methods to let you create the service host from a given service type. True, there is a CreateServiceHost method which accepts a string representation of the type, but that means your service host factory has to have a reference to the type in question. The way I went around that small issue is by creating a small "harness" around the factory, with a public method which accepts a Type:

        public class UnityServiceHostFactoryHarness : UnityServiceHostFactory

        {

            public ServiceHost CreateServiceHost(Type serviceType, string baseAddress)

            {

                return CreateServiceHost(serviceType, new[]

                                                          {

                                                              new Uri(baseAddress)

                                                          });

            }

        }

    A small change to the test-initialize method makes use of this test-harness:

            [ClassInitialize]

            public static void MyClassInitialize(TestContext testContext)

            {

                var serviceHostFactoryHarness = new UnityServiceHostFactoryHarness();

     

                serviceHost = serviceHostFactoryHarness.CreateServiceHost(typeof(Service1), "http://127.0.0.1:8001/");

                serviceHost.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "Service1");

                serviceHost.Open();

            }

    Now we're running service integration tests and we have the Unity container loaded as well. I'm sure there are other, smarter and for me uknown ways of achieving the same, but it works for me :) If you want sample code for the UnityServiceHostFactory, please let me know, but know that my code is based on the this sample.

  • ISO Recorder

    This free (Alex accepts PayPal donations) ISO Burner/recorder works pretty well on Vista and got both 32 and 64-bit versions. Quote from his site:

    ISO Recorder is a tool (power toy) ...snip...  to burn CD and DVD images (DVD support is only available on Windows Vista), copy disks, make images of the existing data CDs and DVDs and create ISO images from a content of a disk folder.

    Insert blank DVD, right click on an .iso file and select "open with ISO Recorder" and a few clicks later you got your DVD burned. Just the way I like it.