Bill Wang’s Blog

October 31, 2008

Test WCF web service – Hosted in ASP.NET Development Server

Filed under: Uncategorized — Tags: , — Bill @ 11:53 am

A refined post of this topic is available in http://billwg.blogspot.com/2009/06/testing-wcf-web-services.html.

Today, a customer asked me how to test WCF web service. I searched internet and found no document addressing this. So I think it will be necessary to write a blog describing test different kinds of WCF web service.

As we know, WCF can be hosted in IIS with some .svc files. During the development, we normally use file system based web site project instead of IIS based. If TFS is used, we often use ASP.NET Web Application project template to develop web application.

The MSDN document Testing Web Services describes how to test .asmx web service locally. It uses WebServiceHelper.TryUrlRedition to redirect the url of web service proxy dynamically. For WCF web service, we use svcutil command-line tool to generate web service client and app.config file. And it needs us to redirect web service url in a different way.

The follow is the steps to create a .svc web service and create a test project. It’s similar to creating tests for .asmx web service.

  1. Open Visual Studio and create a ASP.NET Web application.
  2. Add a WCF Service item to the project.
  3. Create the unit test method for a web service method in the standard way of generating unit test.
  4. Remove the generate attribute for the test method. We can just use TestMethod attribute and AspNetDevelopmentServer attribute.
  5. Run command svcutil to generate client. Svcutil is in %ProgramFiles%\Microsoft SDKs\Windows\v6.0A\bin.
  6. Add the generated web service client and app.config file to the test project.
  7. In the test project, create a new class WcfWebServiceHelper to redirect Url:
    public static bool TryUrlRedirection(object client, TestContext context, string identifier)
    {
        bool result = true;
        try
        {
            PropertyInfo property = client.GetType().GetProperty(“Endpoint”);
            string webServer = context.Properties[
                string.Format(“AspNetDevelopmentServer.{0}”, identifier)].ToString();
            Uri webServerUri = new Uri(webServer);
            ServiceEndpoint endpoint = (ServiceEndpoint)property.GetValue(client, null);
            string serviceAddress = endpoint.Address.Uri.OriginalString;
            serviceAddress = serviceAddress.Replace(
                endpoint.Address.Uri.Authority, webServerUri.Authority);
            endpoint.Address = new EndpointAddress(serviceAddress);
        }
        catch (Exception)
        {
            result = false;
        }
        return result;
    }          
  8. In the unit test method, insert a call to TryUrlRedirection before calling any web method. And the following is a sample of test method:
    [TestMethod()]
    [AspNetDevelopmentServer(“WebApplication1″,@”C:\WebApplication1”)]
    public void FooTest()
    {
        MyWcfServiceClient target = new MyWcfServiceClient();
        Assert.IsTrue
        (WcfWebServiceHelper.TryUrlRedirection(
        target, TestContext, “WebApplication1”));
        string expected = “Hello World”;
        string actual;
        actual = target.HelloWorld();
        Assert.AreEqual( expected, actual);
    }
Advertisements

Blog at WordPress.com.