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

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;
            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:
    public void FooTest()
        MyWcfServiceClient target = new MyWcfServiceClient();
        target, TestContext, “WebApplication1”));
        string expected = “Hello World”;
        string actual;
        actual = target.HelloWorld();
        Assert.AreEqual( expected, actual);


  1. Very helpful post. The last few days I have been researching/studying the capabilities of Visual Studio’s Test Tools, and I was looking for an MSDN entry on “How to test WCF services”. Your post is exactly what I was looking for, and your example works like a charm. Thanks…

    Stelios Tzavaras
    Athens, Greece

    Comment by Stelios Tzavaras — December 22, 2008 @ 3:48 pm

  2. I actually did not even need the redirect method call:

    ///A test for GetItem
    [AspNetDevelopmentServer(“test”, @”C:\tfs\R&D\MyWCFService\”)]
    public void TestGetItem()



    Comment by Cameron — February 13, 2009 @ 9:12 pm

  3. Tweaked your solution a bit to remove the need of reflection (generics FTW!). Also used the EndpointAddressBuilder to make sure all endpoint information is copied.

    public static bool TryUrlRedirection(ClientBase client, TestContext context, string identifier)
    where T: class
    identifier = string.Format(“AspNetDevelopmentServer.{0}”, identifier);
    Uri webServerUri = context.Properties[identifier] as Uri;

    EndpointAddressBuilder builder = new EndpointAddressBuilder(client.Endpoint.Address);
    builder.Uri = new Uri(client.Endpoint.Address.Uri.OriginalString.Replace(client.Endpoint.Address.Uri.Authority, webServerUri.Authority));

    client.Endpoint.Address = builder.ToEndpointAddress();
    catch (Exception)
    return false;

    return true;

    Comment by riezebosch — April 2, 2009 @ 9:31 am

    • Thank you for the EndpointAddressBuilder part.

      Comment by Bill — June 20, 2009 @ 12:09 pm

  4. Hello, Very nice site. Universe help us, dont worry man.

    Comment by djijr5 — May 17, 2009 @ 8:40 am

  5. Hi,

    This is of big help, but I must be missing a step. The AspNetDevelopmentServer is trying to host the Service at a random port, but the Service has an endpoint with port number defined in the web.config, so it throws an exception: No protocol binding matches the given address. How do I fix this?


    Comment by Dawid — June 19, 2009 @ 1:10 am

    • Hi Dawid

      I can’t say it for sure now. In the Property page of the WCF project, please try setting a specific port in the Web tab.

      Comment by Bill — June 19, 2009 @ 7:59 am

    • Yup, I have that. Looks like AspNetDevelopmentServer ignores those settings and instead uses the passed in parameters, except a port number. Thanks for your help.

      Comment by dawid — June 19, 2009 @ 2:52 pm

      • Well. I reviewed the post today. The AspNetDevelopmentServer does actually try to host web application at random ports. That’s why we need to use TryUrlRedirection to redirect the web service proxy before involking the web service.

        By the way, I refined this post and published it on today. I also attached a sample project there. Hope it helps.

        Comment by Bill — June 20, 2009 @ 12:04 pm

      • I’m just criouse, do you know Ewald Hofman? He’s in Avandae too.

        Comment by Bill — June 20, 2009 @ 12:22 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: