Check all the website components are behaving responsibly
+61 2 9953 3000

Overview

A quick guide on how to integrate SSW HealthCheck into existing ASP.NET MVC application. For more information please visit www.sswhealthcheck.com.

Integration

Prerequisites

  1. Visual Studio 2013 (for older versions of Visual Studio, please install NuGet Package installer from http://nuget.codeplex.com/releases)
  2. Your existing ASP.NET MVC 5 Application

Integration Steps

  1. Go to your Nuget Package Manager Console and typeInstall-Package SSW.HealthCheck.Mvc5
    instructions1

    Figure: Installing nugget package

    Note: Alternatively you can use Tools | Library Package Manager | Manage NuGet Packages for Solution and follow the prompts.

  2. Your project must have SignalR. Go to Startup.cs file, and make sure SignalR is mapped. If it is not, then map it.
    instructions2

    Figure:Add one line of code to add SignalR to your project

  3. Integration is completed now. You can run the application by hitting F5 and navigating to /HealthCheck page
    instructions3

    Figure: HealthCheck page loaded with 3 tests.

Built-in Tests

All the built in tests are added by default. Remove the built in tests that you don’t need.

To remove existing tests, Go to App_Start folder and remove corresponding test from HealthCheck service:

instructions4

Figure: Remove tests you don’t want to use

 

Creating Custom Tests

Custom application tests/checks can be added to SSW HealthCheck by simply implementing ITest interface.

Steps

Scenario: We have an existing API that has 3 commands: Ping1, Ping2 and Ping3. We need to write a test which will test accessibility of all those commands.

  1. Add a new folder to your solution called Test.
  2. Add new file to this folder called Api.cs. This will be used as a dummy class to mock API.
    instructions5

    Figure: Api class in your solution

  3. Paste the following code into your Api.cs class. Note: Ping1, Ping2 will always return true, while Pin3 will randomly return true or false.
    1. using System.Threading;
    2.     public class Api
    3.     {
    4.         public static bool Ping1()
    5.         {
    6.             Thread.Sleep(500);
    7.             return true;
    8.         }
    9.         public static bool Ping2()
    10.         {
    11.             Thread.Sleep(500);
    12.             return true;
    13.         }
    14.         public static bool Ping3()
    15.         {
    16.             Thread.Sleep(500);
    17.             return (new Random()).Next(0, 2) > 0;
    18.         }
    19. }
  4. Add a new file ApiTest.cs to your Test folder. This will represent your test class
    instructions6

    Figure: ApiTest file

  5. In order to create SSW HealthCheck test all you have to do is to implement ITest interface. Paste the following code into your ApiTest.cs file
    1. using System.Threading;
    2.     using SSW.HealthCheck.Infrastructure;
    3.     public class ApiTest : ITest
    4.     {
    5.         public ApiTest()
    6.         {
    7.             this.Name = “Api Test”;
    8.             this.Description = “Check if api commands are accessible”;
    9.             this.IsDefault = true;
    10.             this.Order = 0;
    11.         }
    12.         public void Test(ITestContext context)
    13.         {
    14.             var testSuccess = true;
    15.             var testPing1 = Api.Ping1();
    16.             context.WriteLine(
    17.                 testPing1 ? EventType.Success : EventType.Error,
    18.                 “Ping 1 {0}”,
    19.                 testPing1 ? “passed” : “failed”);
    20.             testSuccess &= testPing1;
    21.             context.UpdateProgress(0, 1, 3);
    22.             var testPing2 = Api.Ping2();
    23.             context.WriteLine(
    24.                 testPing2 ? EventType.Success : EventType.Error,
    25.                 “Ping 2 {0}”,
    26.                 testPing2 ? “passed” : “failed”);
    27.             testSuccess &= testPing1;
    28.             context.UpdateProgress(0, 2, 3);
    29.             var testPing3 = Api.Ping3();
    30.             context.WriteLine(
    31.                 testPing3 ? EventType.Success : EventType.Error,
    32.                 “Ping 3 {0}”,
    33.                 testPing3 ? “passed” : “failed”);
    34.             testSuccess &= testPing3;
    35.             context.UpdateProgress(0, 3, 3);
    36.             Thread.Sleep(500);
    37.             if (!testSuccess)
    38.             {
    39.                 Assert.Fails(“API commands are failing.”);
    40.             }
    41.         }
    42.         public string Name { get; private set; }
    43.         public string Description { get; private set; }
    44.         public bool IsDefault { get; private set; }
    45.         public int Order { get; set; }
    46.     }

    Name property – name of your test

    Description property – description of your test

    IsDefault property – indicates whether test will run automatically when page is loaded or it needs to run on demand by user

    Order property – Order in which test will appear in the list of tests.

    Test Method – This is where the actual test is performed. In order to fail the test use Assert.Fails(“<your error message>”). To output custom log record use context.WriteLine(). To update progress use context.UpdateProgress().

  6. Once test is implemented, it needs to be registered. Go to App_Start folder and add ApiTest to HealthCheck service:
    instructions7

    Figure: Registering your new service

  7. Run your web application by hitting F5, go to /HealthCheck page and make sure new test is added.
    instructions8

    Figure: New API test added to HealthCheck page

One last thing

Add a link to sswhealthcheck.com on your homepage