ARoss
ARoss

Reputation: 23

Netcore 3.0 versus 5.0 difference, NullReferenceException with WriteValueWithResultAsync (BLE Lib)

I'm attempting to segregate functionality from the UWP example for BLE into a separate library for testing.

I was successful in setting it up as a standalone test library (using netcore 3.0)

However, in migrating to my GUI app (Now using netcore 5.0) WinRT can no longer be referenced so I had to remove a reference to Windows contracts.

Now I'm running and able to find my BLE device fine (even the correct read/write characteristics) but now when I write to write to the device characteristic I get a breaking crash with the the NullReferenceException.

All of the variables inside the involved object seem not null except for Device ID in the Gatt Characteristic which hides an Interop COM exception, however, this is also true of my "console app" in 3.0 and it does not have this issue and works fine (even with the same conditions present)

How can I debug this deeper? Is this a security issue?

I am targetting net5.0-windows10.0.19041

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Library.BleLib.BleComms.<WriteBufferToSelectedCharacteristicAsync>d__21.MoveNext() in C:\Testing\Net\trunk\Externals\BleLib\BleComms.cs:line 200

Upvotes: 1

Views: 167

Answers (2)

ARoss
ARoss

Reputation: 23

I'll let the community decide what's the better answer here, Arya Ding is also correct.

In my case, I wanted to stick with Netcore because a good portion of the codebase was already written with it. By downgrading to 3.0 and adding the NuGet reference:

Microsoft.Windows.SDK.Contracts 

Everything worked as expected after that.

Upvotes: 0

dear_vv
dear_vv

Reputation: 2358

You have gotten the expected behavior. Currently, uwp app doesn’t support to reference .net core class library. If you want to reference a custom library in uwp app, it is recommended to use .net standard class library.

For your case, you could right-click the class library then select Edit Project File, change the TargetFramework from .NET Core to .NET Standard. As follows:

<PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>

Upvotes: 1

Related Questions