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);
    }

How to configure tests based on build configuration

Filed under: Uncategorized — Tags: — Bill @ 11:52 am

Team build provides ability to build both Release and Debug bits in the same build. If there are unit tests, it will run tests for both Release bits and Debug bits. Sometimes, we might want to run tests or use run configure files based on the build configuration. For example, you might want to get code coverage for the Debug build, while you don’t want to instrument the Release build.

The target CoreTestConfiguration in the team build runs for each build configuration. So, we can override target BeforeTestConfiguration to set properties used by TestToolsTask based on build configuration.

The following code demonstrate how to parse in test run configure file based on the build configureation:

<Target Name=”BeforeTestConfiguration”>
<PropertyGroup Condition=”‘$(Configuration)’==’Debug'”>
<RunConfigFile>$(SolutionRoot)\Solution\Debug.testrunconfig</RunConfigFile>
</PropertyGroup>
<PropertyGroup Condition=”‘$(Configuration)’==’Release'”>
<RunConfigFile>$(SolutionRoot)\Solution\Release.testrunconfig</RunConfigFile>
</PropertyGroup>
</Target>

Custom build script succeeded while the Team Build failed

Filed under: Uncategorized — Tags: — Bill @ 11:52 am
Aaron Hallberg described how to kick off a custom build script in a team build. Please reference A Minimal TFSBuild.Proj File for more information. The interesting thing is that you may notice the team build fails even if the custom script executed successfully.

In a team build, the overall build status might be Succeeded, Partially Succeeded and Failed. The overall status will be

 

  • Succeeded if no error occurs.
  • Partially Succeeded if CompilationStatus is succeeded but error occurs in other part of the build
  • Failed if CompilationStatus is failed.

CompilationStatus and TestStatus are 2 properties created during a team build. When the build service is about to determine the overall build status, it checks CompilationStatus, TestStatus and other occurred errors. The default value for CompilationStatus and TestStatus is Unknown. Thus when you kick off custom build script by overwriting EndToEndIteration target, if CompilationStatus and TestStatus are not set, the overall status will be Failed even if no errors are listed in the build log. And SetBuildProperties task can be used to set them:


<SetBuildProperties TeamFoundationServerUrl=”$(TeamFoundationServerUrl)”
BuildUri=”$(BuildUri)”
CompilationStatus=”Succeeded”
TestStatus=”Succeeded” />
After both CompilationStatus and TestStatus are set to Succeeded, the only factor that determines the overall build status will be an internal log property that tracks whether any error occurs. If there’s no error in the custom build script, then the overall build status will be Succeeded.

 

Microsoft.VisualStudio.TeamFoundation.PowerPools.TfptPackage Load Failure

Filed under: Uncategorized — Tags: — Bill @ 11:51 am

A few days ago, one customer installed TFS Power Tool-July 2008 Release and got an red X on the Alerts node in Team Explorer. In this case, it turns out to be caused by a package load failure. The detail error message is:

Package ‘Microsoft.VisualStudio.TeamFoundation.PowerPools.TfptPackage,
Microsoft.VisualStudio.TeamFoundation.PowerTools, Version=9.0.2.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ has failed to load
properly (GUID = {2C5A58FF-51D1-46CC-B46C-CD458632B321}).
Please contact package vendor for assistance. Application restart is
recommended, due to possible environment corruption. Would you
like to disable loading this package in the future? You may use ‘devenv
/resetskippkgs’ to re-enable package loading.

My initial response is to download the power tool and reinstall it. But the same problem still occurs. After extensive research, I still didn’t get much progress. Later when I was looking at another issue, I opened the document for devenv /setup, and it mentioned that this switch has something to do with VSPackages! I then suggested my customer to run ‘devenv /setup’ first, then reinstall TFS Power Tool. And it worked! Everything goes well and the customer is very happy with the alert features.

Create a free website or blog at WordPress.com.