Gaurav’s Monologue

on Software Architecture, .NET Framework and Connected System Technology

Archive for November, 2008

Web Services Test Automation

Posted by intrepiddeveloper on November 28, 2008

Generate the Service Proxy and Configuration from the exposed WSDL of the service being tested. The simplest option is to use SvcUtil and drop the proxy and config in your test projects solution folder.

svcutil.exe /t:code https://MyWebService.com/Service.svc / out:C:\[project path ]\Proxy.cs /config:C:\ [project path ]\App.config

I run a simple scripted bat file to do this task. The other option is to automate this step with a build task so when you compile your solution the build will generate and drop the Proxy and Configuration at the right location.

Now that you have a service proxy to code against, you can use testing frameworks like NUnit [www.nunit.org/] to write simple unit tests for each service operation or write more detailed scenario tests which make a series of service calls to accomplish a functional business scenario.

This is a sample shell class for hosting the tests as below.

public class TestWebServices {
    public TestWebServicesClient ClientProxy { get; set; }
    public ChannelFactory<itestwebservices> factory { get; set; }
    public ITestWebServices Proxy { get; set; }

[SetUp]
public void TestSetUp()
{
     ServicePointManager.ServerCertificateValidationCallback +=
     new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate);
}

/// <summary>
/// All Clean-up work goes here
/// </summary>
[TearDown]
public void TestTearDown()
{

}

//This is to take care of SSL certification validation which are not issued by Trusted Root CA. Recommended for testing  only.
protected bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
     //Cert Validation Logic
     return true;
}

[TestFixtureTearDown]
public void CloseChannel()
{
     factory.Close();
}
[TestFixture]
public class Agent : BaseTest
{
    [TestFixtureSetUp]
    public void Initialize()
    {
     try
       {
            factory = new ChannelFactory</itestwebservices><itestwebservices>("ConfigurationBinding");
            factory.Credentials.UserName.UserName = "admin";
            factory.Credentials.UserName.Password = "admin";
            factory.Credentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;
            Proxy = factory.CreateChannel();
        }
    catch (Exception ex)
    {
        Assert.Fail(string.Format("Test Setup threw an Exception: {0} ", ex.Message));
    }
   }

    [Test()]
    public void TestNumber01();
    [Test()]
    public void TestNumber02();
    [Test()]
    public void TestScenarioNumber01();
  }

}

And finally you can run this test assembly in the stand alone NUnit console.

NUnitConsole


Posted in Testing, WCF | Leave a Comment »

AddressFilter Mismatch

Posted by intrepiddeveloper on November 22, 2008

Recently I came across an interesting issue relating to AddressFiltering in a WCF service. The service was hosted in IIS and exposed two endpoints i.e. Service.svc/endpoint1 and Service.svc/endpoint2. Since both endpoints exposed the same service interface with different bindings, each endpoint address had to be unique, however the opposite is not true i.e. Multiple endpoints with same binding exposing different service interface can use the same address.

The IIS server hosting the service sits behind a Firewall/SSL-off loader/Reverse Proxy network layer. So the client would send messages to https://www.MyServiceHost.com/Service.svc/endpoint1 which would internally get striped-off the SSL and get routed to the IIS server as http://IISHostServer/Service.svc/endpoint1. At this point the servicehost would throw an error message along the lines of  “AddressFilter mismatch at the EndpointDispatcher”.

The dispatcher could not resolve which endpoint the messages should be forward to. AddressFilterMode is a service behavior option which controls how the endpoint filters addresses.

The AddressFilter options are:

  • Exact: matches to the exact endpoint address.
  • Prefix:  matches the incoming “TO” address to endpoint address as long as it starts with same prefix address.
  • Any: matches all messages to the given endpoint.

The simple fix was to decorate the service behavior with [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]

There is a good article on MSDN Magzine that explains Message Addressing here.

Posted in .NET, WCF | Comments Off