< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.DnsCache
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/Common/src/CoreWCF/Channels/DnsCache.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 87
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/Common/src/CoreWCF/Channels/DnsCache.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Diagnostics;
 6using System.IO;
 7using System.Net;
 8using System.Net.Sockets;
 9using System.Runtime.InteropServices;
 10
 11namespace CoreWCF.Channels
 12{
 13    internal static class DnsCache
 14    {
 15        private const string LinuxProcHostnamePath = "/proc/sys/kernel/hostname";
 16
 17        // Double-checked locking pattern requires volatile for read/write synchronization
 18        private static volatile string s_machineName;
 19
 020        private static object ThisLock { get; } = new object();
 21
 22        public static string MachineName
 23        {
 24            get
 25            {
 026                if (s_machineName == null)
 27                {
 028                    lock (ThisLock)
 29                    {
 030                        if (s_machineName == null)
 31                        {
 32                            try
 33                            {
 034                                s_machineName = Dns.GetHostEntry(string.Empty).HostName;
 035                            }
 36                            catch (SocketException exception)
 37                            {
 038                                DiagnosticUtility.TraceHandledException(exception,
 039                                        TraceEventType.Information);
 040                            }
 41                        }
 42
 043                        if (s_machineName == null) // prior attempt failed
 44                        {
 45                            try
 46                            {
 47                                // This uses a different native API so might work where the previous one didn't.
 048                                s_machineName = Dns.GetHostName();
 049                            }
 50                            catch (SocketException exception)
 51                            {
 052                                DiagnosticUtility.TraceHandledException(exception,
 053                                        TraceEventType.Information);
 054                            }
 55                        }
 56
 057                        if (s_machineName == null && RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && File.Exists(Li
 58                        {
 59                            try
 60                            {
 61                                // Final attempt, try to get the hostname from the proc filesystem if running on Linux
 062                                string[] hostnameFile = File.ReadAllLines(LinuxProcHostnamePath);
 063                                if (hostnameFile.Length > 0 && !string.IsNullOrEmpty(hostnameFile[0]))
 64                                {
 065                                    s_machineName = hostnameFile[0];
 66                                }
 067                            }
 68                            catch (Exception exception) // There's no common base exception that File.ReadAllLines can t
 69                            {
 070                                DiagnosticUtility.TraceHandledException(exception,
 071                                    TraceEventType.Information);
 072                            }
 73                        }
 74
 75                        // Final fallback if every other mechanism fails
 076                        if (s_machineName == null)
 77                        {
 078                            s_machineName = "localhost";
 79                        }
 080                    }
 81                }
 82
 083                return s_machineName;
 84            }
 85        }
 86    }
 87}

Methods/Properties

ThisLock()
MachineName()