Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2. using AspNetCore.Health;
  3. using System.Data.SqlClient;
  4. using System.Data;
  5.  
  6. namespace BorrowWorks.Nortridge.LoanSystem.WebApi.Health
  7. {
  8.     public static class HealthCheckContextExtensions
  9.     {
  10.         public static HealthCheckContext AddConnectionStringCheck(
  11.             this HealthCheckContext checkContext,
  12.             string name,
  13.             string connectionString)
  14.         {
  15.             checkContext.Add(name, async () =>
  16.             {
  17.                 try
  18.                 {
  19.                     SqlConnection connection = new SqlConnection(connectionString);
  20.                     await connection.OpenAsync();
  21.  
  22.                     if ((connection.State & ConnectionState.Open) > 0)
  23.                     {
  24.                         connection.Close();
  25.  
  26.                         return HealthCheckResult.Healthy(name);
  27.                     }
  28.                     else
  29.                     {
  30.                         return HealthCheckResult.Unhealthy(name);
  31.                     }
  32.                 }
  33.                 catch
  34.                 {
  35.                     return HealthCheckResult.Unhealthy(name);
  36.                 }
  37.             });
  38.  
  39.             return checkContext;
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement