Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What causes a method to pass null values?

So I wrote a method of code and it pulls from the database correctly (I am using Dapper), but it doesnt pass off to the next method. Can anyone tell me why and what I am doing wrong? Not quite understanding what I am doing wrong here. I have tried a few different ways including below and making and IEnumerable list. I can see the variables in the logger so I know I am pulling them correctly, just not sure why they arent sending to the CheckSite().

public class UptimeService
{
    private readonly ILogger<UptimeService> _logger;
    private readonly IWebsiteData _webdb;
    private readonly IUptimeData _db;
    public UptimeService(IWebsiteData webdb, IUptimeData db ,ILogger<UptimeService> logger) 
    {

        _webdb = webdb;
        _logger = logger;
        _db= db;
    }
    public class SiteResponse
    {
        public int Websiteid { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public bool Status { get; set; }
        public long ResponseTime { get; set; }


    }


    public async Task GetAllWebsites()
    {
       
        var websites = await _webdb.GetWebsites();


        foreach (var website in websites)
        {
         
            _logger.LogInformation($"WEBSITE::::: {website.Url} | {website.Name} | {website.Websiteid}");
            CheckSite(website.Url, website.Name, website.Websiteid);
        }
        return ;
         
    }

    public SiteResponse CheckSite(string Url, string Name, int Websiteid)
    
    {
        var result = new SiteResponse();
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        var client = new HttpClient();
        _logger.LogInformation(
                            $"TEST URL: {result.Url}");
        try
        {
            var checkingResponse = client.GetAsync(Url).Result;
            
            result.Status = checkingResponse.IsSuccessStatusCode &&
                            checkingResponse.StatusCode == HttpStatusCode.OK;
        }
        catch
        {
            result.Status = false;
            // offline
        }
        stopwatch.Stop();
        var elapsed = stopwatch.ElapsedMilliseconds;
        result.ResponseTime = elapsed;

    
        if (result.Status)
        {
            // archive record
            RecordToDb(result);
        }
        else
        {
            _logger.LogInformation(
                             $"Status is {result.Status}");
        }
        return result;
    }


    public async void RecordToDb(SiteResponse response)
    {
        var newRecord = new UptimeModel
        {
            Time = DateTime.Now,
            Status = response.Status,
            ResponseTime = (int)response.ResponseTime,
            Websiteid = response.Websiteid,
            Name = response.Name,
   
        };
        _logger.LogInformation(
                            $"Trying to Save {response.Name}");
        await _db.InsertUptime(newRecord);
    }

    
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If the "result.Url" is empty here

_logger.LogInformation($"TEST URL: {result.Url}");

it’s because it’s a new instance of SiteResponse();

If this is what is showing as null. You’ll need to create constructures on the class. Something like this

public class SiteResponse
{
    public SiteResponse(){  }
    public SiteResponse(string url){
        Url = url;
    }
    public int Websiteid { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public bool Status { get; set; }
    public long ResponseTime { get; set; }
}

and then initialize the new one like

var result = new SiteResponse(Url);

Based on our comments below. I would refactor to something like this.

public class UptimeService
{
    private readonly ILogger<UptimeService> _logger;
    private readonly IWebsiteData _webdb;
    private readonly IUptimeData _db;
    public UptimeService(IWebsiteData webdb, IUptimeData db ,ILogger<UptimeService> logger) 
    {

        _webdb = webdb;
        _logger = logger;
        _db= db;
    }

    public class SiteResponse
    {
        public int Websiteid { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public bool Status { get; set; }
        public long ResponseTime { get; set; }
    }

    public async Task GetAllWebsites()
    {
       
        var websites = await _webdb.GetWebsites();

        foreach (var website in websites)
        {
         
            _logger.LogInformation($"WEBSITE::::: {website.Url} | {website.Name} | {website.Websiteid}");
            await CheckSite(website);
        }
        return ;
         
    }

    public async Task CheckSite(SiteResponse siteResponse)
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        var client = new HttpClient();
        _logger.LogInformation(
                            $"TEST URL: {siteResponse.Url}");
        try
        {
            var checkingResponse = await client.GetAsync(siteResponse.Url);
            
            siteResponse.Status = checkingResponse.IsSuccessStatusCode &&
                            checkingResponse.StatusCode == HttpStatusCode.OK;
        }
        catch
        {
            siteResponse.Status = false;
            // offline
        }
        stopwatch.Stop();
        var elapsed = stopwatch.ElapsedMilliseconds;
        siteResponse.ResponseTime = elapsed;

    
        if (siteResponse.Status)
        {
            // archive record
            RecordToDb(siteResponse);
        }
        else
        {
            _logger.LogInformation(
                             $"Status is {siteResponse.Status}");
        }
        return;
    }


    public async void RecordToDb(SiteResponse response)
    {
        var newRecord = new UptimeModel
        {
            Time = DateTime.Now,
            Status = response.Status,
            ResponseTime = (int)response.ResponseTime,
            Websiteid = response.Websiteid,
            Name = response.Name,
   
        };
        _logger.LogInformation(
                            $"Trying to Save {response.Name}");
        await _db.InsertUptime(newRecord);
    }    
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading