My tracert method no longer works with .net 6 but was working fine on .net standard 2 with Xamarin.Forms before the migration to .NET MAUI

John Norton 66 Reputation points
2022-11-26T20:24:39.957+00:00

Still finalizing a lot of code to .net 6 after migrating from XF to MAUI, I find that my tracert method returns TIMEOUTS in the route up to the host now. Can anyone look at my code and see if I need to change anything? As far as I can tell it should be working ok.

public async Task<bool> DoTrace(string ipAddress)  
    {  
        if (ipAddress.Contains("https://"))  
        {  
            _remove = ipAddress.Remove(0, 8);  
        }  
        else if (ipAddress.Contains("http://"))  
        {  
            _remove = ipAddress.Remove(0, 7);  
        }  
  
        if (_remove.Contains(':'))  
        {  
            var s = _remove.IndexOf(':');  
            var t = _remove[..(s)];  
            _remove = t;  
        }  
  
        Debug.WriteLine("Hostname was " + _remove);  
  
        for (var zeroBasedHop = 0; zeroBasedHop < MAX_HOPS; zeroBasedHop++)  
        {  
            var result = await TraceReply(_remove, zeroBasedHop).ConfigureAwait(false);  
            if (result) Debug.WriteLine("Hop " + zeroBasedHop);  
        }  
  
        return await Task.FromResult(PingObservableCollection.Count > 0);  
    }  
  
    private IPAddress previousAddress;  
  
    public IPAddress PreviousAddress  
    {  
        get => previousAddress;  
        set => SetProperty(ref previousAddress, value);  
    }  
  
  
    private async Task<bool> TraceReply(string hostNameOrAddress, int zeroBasedHop)  
    {  
        using var pingSender = new Ping();  
        var hop = zeroBasedHop + 1;  
  
        var pingOptions = new PingOptions();  
        pingOptions.DontFragment = true;  
        pingOptions.Ttl = hop;  
  
        var pingReply = await pingSender.SendPingAsync(  
            hostNameOrAddress,  
            REQUEST_TIMEOUT,  
            _buffer,  
            pingOptions  
        );  
  
        var reply = pingReply;  
  
        if (!Equals(PreviousAddress, reply.Address) && reply.Status != IPStatus.TimedOut)  
        {  
            if (reply != null)  
            {  
                Debug.WriteLine(reply.Address);  
  
                PreviousAddress = reply.Address;  
  
                NewP = new PReply()  
                {  
                    Address = reply.Address,  
                    Status = reply.Status,  
                    RoundtripTime = reply.RoundtripTime  
                };  
            }  
  
            BeginInvokeOnMainThread(() => { PingObservableCollection.Add(NewP); });  
  
            return true;  
        }  
  
        return false;  
    }  

I sure appreciate your time and effort in helping me solve this!

John

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,212 questions
{count} vote