Skip to content

InvalidCastException when trying to add device context #1695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
IasonasNikolaou opened this issue Jun 7, 2022 · 6 comments
Closed

InvalidCastException when trying to add device context #1695

IasonasNikolaou opened this issue Jun 7, 2022 · 6 comments

Comments

@IasonasNikolaou
Copy link

IasonasNikolaou commented Jun 7, 2022

Please mark the type framework used:

  • [x ] Other: Maui

Please mark the type of the runtime used:

  • [x ] .NET Framework
  • Version: 6.0.5

Please mark the NuGet packages used:

  • [ x] Sentry
  • Version: 3.17.1

Steps to reproduce the issue or link to a repository with a small reproducible code.
Add sentry to a maui or probably even a .net project. I don't think thats a maui related issue tbh.

I've got this piece of code in my Main which worked fine until i decided i also needed to add device info

SentrySdk.ConfigureScope(scope =>
            {
                scope.User = new User
                {
                    Id = "1",
                    Username = "Testuser",
                };
                scope.Contexts["os"] = new
                {
                    name = AndroidInfo.platform,
                    version = AndroidInfo.version,
                };
                scope.Contexts["device"] = new
                {
                    name = AndroidInfo.deviceName,
                    family = AndroidInfo.manufacturer,
                    model = AndroidInfo.device,
                    manufacturer = AndroidInfo.manufacturer,
                }; 
            });

"os" and "user" are working just fine on their own, however "device" returns this exception

[DOTNET] System.InvalidCastException: Specified cast is not valid.
[DOTNET]    at Sentry.Internal.Extensions.CollectionsExtensions.GetOrCreate[Device](ConcurrentDictionary`2 dictionary, String key) in /_/src/Sentry/Internal/Extensions/CollectionsExtensions.cs:line 13
[DOTNET]    at Sentry.Contexts.get_Device() in /_/src/Sentry/Protocol/Contexts.cs:line 30
[DOTNET]    at Sentry.Internal.MainSentryEventProcessor.Process(SentryEvent event) in /_/src/Sentry/Internal/MainSentryEventProcessor.cs:line 45
[DOTNET]    at Sentry.SentryClient.DoSendEvent(SentryEvent event, Scope scope) in /_/src/Sentry/SentryClient.cs:line 201

I should note that all of these are strings and do have values.
If i try to add the device context as something else (ex:scope.Contexts["deviceinfo"]) it works just fine.
Is there any way to get around this error?

@IasonasNikolaou
Copy link
Author

Upon further experimentation, if i cast the object when i add it to the contexts, it works.

using Sentry.Protocol;
.
.
.
 scope.Contexts["device"] = new Device
                {
                    Name = AndroidInfo.deviceName,
                    Family = AndroidInfo.manufacturer,
                    Model = AndroidInfo.device,
                    Manufacturer = AndroidInfo.manufacturer,
                };   

@SimonCropp
Copy link
Contributor

SimonCropp commented Jun 7, 2022

      scope.Contexts["device"] = new
                {
                    name = AndroidInfo.deviceName,
                    family = AndroidInfo.manufacturer,
                    model = AndroidInfo.device,
                    manufacturer = AndroidInfo.manufacturer,
                }; 

this is an anonymous type so i am not surprised it fails.

i think we need some type checking in GetOrCreate. i will take a look

@SimonCropp
Copy link
Contributor

as an alternative is you can do this via

       SentrySdk.ConfigureScope(scope =>
        {
            var device = scope.Contexts.Device;
            device.Name = AndroidInfo.deviceName;
            device.family = AndroidInfo.manufacturer;
            device.model = AndroidInfo.device;
            device.manufacturer = AndroidInfo.manufacturer;
        });

@mattjohnsonpint
Copy link
Contributor

@jaisonass - Thanks for reporting this issue with the InvalidCastException.

FYI - We are working on adding first-class support for both MAUI (#1651) and for Android (net6.0-android) in general. Both are coming very soon.

This will include device information like you are doing here, so some of your work will be redundant. You might just want to wait.

@SimonCropp
Copy link
Contributor

@jaisonass closing this one. the better exception message will ship in the next release.

Repository owner moved this from Needs Discussion to Done in [DEPRECATED] Mobile SDKs Jun 7, 2022
@IasonasNikolaou
Copy link
Author

@SimonCropp Yep, thank you both for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

3 participants