HCV- adding code–part 4

Now it is time to write some code .  However, the implementation should be in a different class than the health check itself – to can be tested.

The code is as follows:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
 
[assembly: InternalsVisibleTo("TestHealthCheckVersion")]
 
namespace HCVersion
{
    internal class HCV
    {
        static Version versionEntry;
        static Version versionHCV;
        static HCV()
        {
            var ass = Assembly.GetEntryAssembly();
            versionEntry = ass.GetName().Version;
            versionHCV = Assembly.GetExecutingAssembly().GetName().Version;
 
        }
        public string GetStartingAssemblyInformation()
        {
            return versionEntry.ToString();
        }
        public string GetHCVAssemblyInformation()
        {
            return versionHCV.ToString();
        }
    }
}

The internals visible to is for running tests successfully.

The code for getting the real Health Check is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Threading;
using System.Threading.Tasks;
 
namespace HCVersion
{
    public class HealthCheckVersion : IHealthCheck
    {
        public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var hcv = new HCV();
            var res = HealthCheckResult.Healthy(hcv.GetStartingAssemblyInformation(),
                new Dictionary<string, object>()
                {
                    {"Entry",hcv.GetStartingAssemblyInformation() },
                    {"HCV",hcv.GetHCVAssemblyInformation() }
                });
 
            return Task.FromResult( res);
        }
    }
}

You will find the code at https://github.com/ignatandrei/HealthCheckVersion/releases/tag/testWorks

HealthCheckVersion

Health Check for Version
 NameLink
1Idea and Githubhttp://msprogrammer.serviciipeweb.ro/2020/07/20/healthcheckversion-idea-part-1/
2Documentationhttp://msprogrammer.serviciipeweb.ro/2020/07/21/healthcheckversiondocumentation-part-2/
3Testshttp://msprogrammer.serviciipeweb.ro/2020/07/22/hcv-adding-testspart-3/
4Codehttp://msprogrammer.serviciipeweb.ro/2020/07/23/hcv-adding-codepart-4/
5Test Applicationhttp://msprogrammer.serviciipeweb.ro/2020/07/27/hcvadding-test-applicationpart-5/
6CIhttp://msprogrammer.serviciipeweb.ro/2020/07/28/hcvautomatic-cipart-6/
7NuGethttp://msprogrammer.serviciipeweb.ro/2020/07/29/hcvpreparing-for-nugetpart-7/
8PostMortemhttp://msprogrammer.serviciipeweb.ro/2020/07/30/hcv-postmortempart-8/