برچسب: class

  • C# Tip: use the Ping class instead of an HttpClient to ping an endpoint

    C# Tip: use the Ping class instead of an HttpClient to ping an endpoint


    Just a second! 🫷
    If you are here, it means that you are a software developer.
    So, you know that storage, networking, and domain management have a cost .

    If you want to support this blog, please ensure that you have disabled the adblocker for this site.
    I configured Google AdSense to show as few ADS as possible – I don’t want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.

    Thank you for your understanding.
    Davide

    What if you wanted to see if a remote website is up and running?

    Probably, the first thing that may come to your mind is to use a common C# class: HttpClient. But it may cause you some trouble.

    There is another way to ping an endpoint: using the Ping class.

    Why not using HttpClient

    Say that you need to know if the host at code4it.dev is live. With HttpClient you might use something like this:

    async Task Main()
    {
        var url = "https://code4it.dev";
    
        var isUp = await IsWebsiteUp_Get(url);
    
        Console.WriteLine("The website is {0}", isUp ? "up" : "down");
    }
    
    private async Task<bool> IsWebsiteUp_Get(string url)
    {
        var httpClient = new HttpClient(); // yes, I know, I should use HttpClientFactory!
        var httpResponse = await httpClient.GetAsync(url);
        return httpResponse.IsSuccessStatusCode;
    }
    

    There are some possible issues with this approach: what if there is no resource available in the root? You will have to define a specific path. And what happens if the defined resource is under authentication? IsWebsiteUp_Get will always return false. Even when the site is correctly up.

    Also, it is possible that the endpoint does not accept HttpGet requests. So, we can use HttpHead instead:

    private async Task<bool> IsWebsiteUp_Head(string url)
    {
        var httpClient = new HttpClient();
        HttpRequestMessage request = new HttpRequestMessage
        {
            RequestUri = new Uri(url),
            Method = HttpMethod.Head // Not GET, but HEAD
        };
        var result = await httpClient.SendAsync(request);
        return result.IsSuccessStatusCode;
    }
    

    We have the same issues described before, but at least we are not bound to a specific HTTP verb.

    By the way, we need to find another way.

    How to use Ping

    By using the Ping class, we can get rid of those checks and evaluate the status of the Host, not of a specific resource.

    private async Task<bool> IsWebsiteUp_Ping(string url)
    {
        Ping ping = new Ping();
        var hostName = new Uri(url).Host;
    
        PingReply result = await ping.SendPingAsync(hostName);
        return result.Status == IPStatus.Success;
    }
    

    The Ping class comes in the System.Net.NetworkInformation namespace, and allows you to perform the same operations of the ping command you usually send via command line.

    Conclusion

    We’ve seen why you should use Ping instead of HttpClient to perform a ping-like operation.

    There’s more than this: head to this more complete article to learn more.

    👉 Let’s discuss it on Twitter or on the comment section below.

    🐧





    Source link

  • Mark a class as Sealed to prevent subclasses creation &vert; Code4IT

    Mark a class as Sealed to prevent subclasses creation | Code4IT


    Just a second! 🫷
    If you are here, it means that you are a software developer.
    So, you know that storage, networking, and domain management have a cost .

    If you want to support this blog, please ensure that you have disabled the adblocker for this site.
    I configured Google AdSense to show as few ADS as possible – I don’t want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.

    Thank you for your understanding.
    Davide

    The O in SOLID stands for the Open-closed principle: according to the official definition, “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.”

    To extend a class, you usually create a subclass, overriding or implementing methods from the parent class.

    Extend functionalities by using subclasses

    The most common way to extend a class is to mark it as abstract :

    public abstract class MyBaseClass
    {
      public DateOnly Date { get; init; }
      public string Title { get; init; }
    
      public abstract string GetFormattedString();
    
      public virtual string FormatDate() => Date.ToString("yyyy-MM-dd");
    }
    

    Then, to extend it you create a subclass and define the internal implementations of the extention points in the parent class:

    public class ConcreteClass : MyBaseClass
    {
      public override string GetFormattedString() => $"{Title} | {FormatDate()}";
    }
    

    As you know, this is the simplest example: overriding and implementing methods from an abstract class.

    You can override methods from a concrete class:

    public class MyBaseClass2
    {
      public DateOnly Date { get; init; }
      public string Title { get; init; }
    
      public string GetFormattedString() => $"{Title} ( {FormatDate()} )";
    
      public string FormatDate() => Date.ToString("yyyy-MM-dd");
    }
    
    public class ConcreteClass2 : MyBaseClass2
    {
      public new string GetFormattedString() => $"{Title} | {FormatDate()}";
    }
    

    Notice that even though there are no abstract methods in the base class, you can override the content of a method by using the new keyword.

    Prevent the creation of subclasses using the sealed keyword

    Especially when exposing classes via NuGet, you want to prevent consumers from creating subclasses and accessing the internal status of the structures you have defined.

    To prevent classes from being extended, you must mark your class as sealed:

    public sealed class MyBaseClass3
    {
      public DateOnly Date { get; init; }
      public string Title { get; init; }
    
      public string GetFormattedString() => $"{Title} ( {FormatDate()} )";
    
      public string FormatDate() => Date.ToString("yyyy-MM-dd");
    }
    
    public class ConcreteClass3 : MyBaseClass3
    {
    }
    

    This way, even if you declare ConcreteClass3 as a subclass of MyBaseClass3, you won’t be able to compile the application:

    Compilation error when trying to extend a sealed class

    4 reasons to mark a class as sealed

    Ok, it’s easy to prevent a class from being extended by a subclass. But what are the benefits of having a sealed class?

    Marking a C# class as sealed can be beneficial for several reasons:

    1. Security by design: By marking a class as sealed, you prevent consumers from creating subclasses that can alter or extend critical functionalities of the base class in unintended ways.
    2. Performance improvements: The compiler can optimize sealed classes more effectively because it knows there are no subclasses. This will not bring substantial performance improvements, but it can still help if every nanosecond is important.
    3. Explicit design intent: Sealing the class communicates to other developers that the class is not intended to be extended or modified. If they want to use it, they accept they cannot modify or extend it, as it has been designed in that way by purpose.

    This article first appeared on Code4IT 🐧

    Wrapping up

    I hope you enjoyed this article! Let’s keep in touch on Twitter or LinkedIn! 🤜🤛

    Happy coding!

    🐧





    Source link