diff --git a/src/core/Akka/Actor/Address.cs b/src/core/Akka/Actor/Address.cs index 9e638d01428..8ff18069456 100644 --- a/src/core/Akka/Actor/Address.cs +++ b/src/core/Akka/Actor/Address.cs @@ -59,7 +59,7 @@ public int Compare(Address x, Address y) /// public static readonly Address AllSystems = new Address("akka", "all-systems"); - private readonly Lazy _toString; + private string _toString; private readonly string _host; private readonly int? _port; private readonly string _system; @@ -78,7 +78,7 @@ public Address(string protocol, string system, string host = null, int? port = n _system = system; _host = host?.ToLowerInvariant(); _port = port; - _toString = CreateLazyToString(); + _toString = null; } /// @@ -114,19 +114,14 @@ public Address(string protocol, string system, string host = null, int? port = n /// public bool HasGlobalScope => !string.IsNullOrEmpty(Host); - private Lazy CreateLazyToString() + private static string CreateLazyToString(Address addr) { - return new Lazy(() => - { - var sb = new StringBuilder(); - sb.AppendFormat("{0}://{1}", Protocol, System); - if (!string.IsNullOrWhiteSpace(Host)) - sb.AppendFormat("@{0}", Host); - if (Port.HasValue) - sb.AppendFormat(":{0}", Port.Value); - - return sb.ToString(); - }, true); + if (!string.IsNullOrWhiteSpace(addr.Host) && addr.Port.HasValue) + return $"{addr.Protocol}://{addr.System}@{addr.Host}:{addr.Port}"; + if (!string.IsNullOrWhiteSpace(addr.Host)) // host, but no port - rare case + return $"{addr.Protocol}://{addr.System}@{addr.Host}"; + + return $"{addr.Protocol}://{addr.System}"; } /// @@ -140,7 +135,15 @@ public int CompareTo(Address other) } /// - public override string ToString() => _toString.Value; + public override string ToString() + { + if (_toString == null) + { + _toString = CreateLazyToString(this); + } + + return _toString; + } /// public bool Equals(Address other) @@ -279,7 +282,7 @@ public static Address Parse(string address) if (string.IsNullOrEmpty(uri.UserInfo)) { var systemName = uri.Host; - + return new Address(protocol, systemName); } else