Skip to content
This repository has been archived by the owner on Mar 26, 2019. It is now read-only.

InvalidCastException when adding sql check #24

Closed
ghost opened this issue Jun 12, 2018 · 2 comments
Closed

InvalidCastException when adding sql check #24

ghost opened this issue Jun 12, 2018 · 2 comments
Labels
Projects
Milestone

Comments

@ghost
Copy link

ghost commented Jun 12, 2018

This is the error in health endpoint

{
healthy: {
CPU Health Check: "OK",
Drive Health Check: "OK"
},
unhealthy: {
DB Connection: "EXCEPTION: InvalidCastException - Unable to cast object of type 'System.Int32' to type 'System.Int64'. InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Int64'. at App.Metrics.Health.Checks.Sql.SqlHealthCheckBuilderExtensions.<>c__DisplayClass0_0.<AddSqlCheck>b__0(CancellationToken cancellationToken) in C:\projects\health\src\App.Metrics.Health.Checks.Sql\SqlHealthCheckBuilderExtensions.cs:line 48 ",
Private Memory Size: "FAILED. 87666688 > 1000 bytes",
Virtual Memory Size: "FAILED. 2218767613952 > 1000 bytes",
Working Set: "FAILED. 81408000 > 1000"
},
status: "Unhealthy"
}

and the configure for health with deaults :

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            return WebHost.CreateDefaultBuilder(args)
                .ConfigureHealthWithDefaults(
                builder =>
                {
                    const int threshold = 1000;
                    // Check that the current amount of private memory in bytes is below a threshold
                    builder.HealthChecks.AddProcessPrivateMemorySizeCheck("Private Memory Size", threshold);
                    // Check that the current amount of virtual memory in bytes is below a threshold
                    builder.HealthChecks.AddProcessVirtualMemorySizeCheck("Virtual Memory Size", threshold);
                    // Check that the current amount of physical memory in bytes is below a threshold
                    builder.HealthChecks.AddProcessPhysicalMemoryCheck("Working Set", threshold);

                    // Check db status
                    builder.HealthChecks.AddSqlCheck("DB Connection", () => new SqlConnection("myconnectionstring"), TimeSpan.FromSeconds(10));
                })
                .UseHealth()
                .UseHealthEndpoints()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseConfiguration(Configuration)
                .UseSerilog()
                .UseIISIntegration()
                .UseKestrel()
                .UseStartup<Startup>();
        }

@ghost
Copy link
Author

ghost commented Jun 13, 2018

Inspecting your sources, i think i found the bug :

command.CommandText = "SELECT 1";
var commandResult = (long)command.ExecuteScalar();

the result is an int and you try to convert it to a long.

@alhardy
Copy link
Contributor

alhardy commented Jun 14, 2018

@sorcer1 Thanks for taking a look, IDbConnection.ExecuteScalar() returns an object, the same app use using a SqliteConnection which returns a long, would have to add some type of safety around that.

PR? ;)

@alhardy alhardy added the bug label Jun 14, 2018
@alhardy alhardy added this to the 2.1.0-alpha milestone Jun 14, 2018
@alhardy alhardy added this to TODO in 2.1.0 Jun 14, 2018
@alhardy alhardy moved this from TODO to Ready for Release in 2.1.0 Jun 29, 2018
@alhardy alhardy moved this from Ready for Release to Alpha Released in 2.1.0 Jul 8, 2018
@alhardy alhardy moved this from Alpha Released to Stable Released (Done) in 2.1.0 Dec 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
No open projects
2.1.0
Stable Released (Done)
Development

No branches or pull requests

1 participant