novotnyllc / Zeroconf

Bonjour support for .NET Core, .NET 4.6, Xamarin, and UWP
MIT License
367 stars 91 forks source link

Zeroconf.ZeroconfHost.AddService (Zeroconf.IService service) Null Key Exception - iOS, Bug #244

Open twofingerrightclick opened 2 years ago

twofingerrightclick commented 2 years ago

Using latest stable Zeroconf 3.6.11 I am trying to get an iOS Xamarin project working for iOS 14.5+ (previously worked on iOS versions prior). It only needs to search for a single service and so I have followed the instructions for adding NSBonjourServices in the Info.plist:

<key>NSLocalNetworkUsageDescription</key>
    <string>Looking for printers with mDNS/Bonjour services</string>
<key>NSBonjourServices</key>
    <array>
        <string>_printer._tcp</string>
    </array>

When calling ZeroconfResolver.ResolveAsync("_printer._tcp.local.") the following exception bubbles up from Zeroconf.ZeroconfHost.AddService (Zeroconf.IService service):

{System.ArgumentNullException: Value cannot be null.Parameter name: key at System.Collections.Generic.Dictionary2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) [0x00008] in <35fb05e83cf64d249b5a7be1a6f7838a>:0 at System.Collections.Generic.Dictionary2[TKey,TValue].setItem (TKey key, TValue value) [0x00000] in <35fb05e83cf64d249b5a7be1a6f7838a>:0 at Zeroconf.ZeroconfHost.AddService (Zeroconf.IService service) [0x00000] in //Zeroconf/ZeroconfRecord.cs:193 at Zeroconf.BonjourBrowser.RefreshZeroconfHostDict () [0x00121] in //Zeroconf/BonjourBrowser.cs:436 at Zeroconf.BonjourBrowser.ReturnZeroconfHostResults () [0x00028] in //Zeroconf/BonjourBrowser.cs:361 at Zeroconf.ZeroconfNetServiceBrowser.ResolveAsync (Zeroconf.ResolveOptions options, System.Action1[T] callback, System.Threading.CancellationToken cancellationToken, System.Net.NetworkInformation.NetworkInterface[] netInterfacesToSendRequestOn) [0x0010b] in /_/Zeroconf/ZeroconfNetServiceBrowser.cs:39 at Zeroconf.ZeroconfResolver.ResolveAsync (Zeroconf.ResolveOptions options, System.Action1[T] callback, System.Threading.CancellationToken cancellationToken, System.Net.NetworkInformation.NetworkInterface[] netInterfacesToSendRequestOn) [0x000e0] in /_/Zeroconf/ZeroconfResolver.Async.cs:105 at Zeroconf.ZeroconfResolver.ResolveAsync (System.Collections.Generic.IEnumerable1[T] protocols, System.TimeSpan scanTime, System.Int32 retries, System.Int32 retryDelayMilliseconds, System.Action1[T] callback, System.Threading.CancellationToken cancellationToken...

ZeroconfResolver.IsiOSWorkaroundEnabled resolves to true.

I am trying to run the demo iOS project included in the repo to confirm similar behavior, but I currently can get it working... it builds and deploys, but is just a black screen.

twofingerrightclick commented 2 years ago

So the exception comes from here: https://github.com/novotnyllc/Zeroconf/blob/238df25a26d2b0f214d7eee20f3d6a1e105ee9b8/Zeroconf/ZeroconfRecord.cs#L193 The ServiceName on the Service object is not being set (is null) as part of the iOS pipeline of the ResolveAsync. I will see if I can figure out where the ServiceName is supposed to be set, but isn't - Nullable enabled would be helpful here.

twofingerrightclick commented 2 years ago

I am thinking it should have been set about here in the BonjourBrowser.cs https://github.com/novotnyllc/Zeroconf/blob/238df25a26d2b0f214d7eee20f3d6a1e105ee9b8/Zeroconf/BonjourBrowser.cs#L399-L402

twofingerrightclick commented 2 years ago

@clairernovotny Please review https://github.com/novotnyllc/Zeroconf/pull/246