The Replication libraries installed as part of the SQL Server install need to be paired with an older, compatible version of SMO. The version of SMO you are using has switched from the .NET Framework SqlClient (System.Data.SqlClient) to the newer standalone one (Microsoft.Data.SqlClient).
So downgrade the SMO NuGet package to 150.18208.0, and change the code to :
var connection = new System.Data.SqlClient.SqlConnection(@"Server=.;Database=master;Integrated Security=true;");
var serverConnection = new Microsoft.SqlServer.Management.Common.ServerConnection(connection);
var replicationServer = new Microsoft.SqlServer.Replication.ReplicationServer(serverConnection);
bool installed = replicationServer.DistributorInstalled;