Platform not supported exception from MongoDB.Driver

  Kiến thức lập trình

**So I’m trying to code a C# .NET Maui app for a pokedex. Shouldn’t be too complicated but for some reason the mongodb database I am running with will not cooperate and I cannot pull any data from it. **

2024-07-31 17:45:13.295804-0400 PokedexApp[82592:2135044] Collection retrieved successfully.

2024-07-31 17:45:13.295938-0400 PokedexApp[82592:2135044] Counting documents…

Exception thrown: ‘System.InvalidOperationException’ in Microsoft.Maui.dll
Exception thrown: ‘System.InvalidOperationException’ in Microsoft.Maui.dll
Exception thrown: ‘System.PlatformNotSupportedException’ in MongoDB.Driver.Core.dll
3
Exception thrown: ‘System.PlatformNotSupportedException’ in System.Private.CoreLib.dll
2024-07-31 17:45:13.472036-0400 PokedexApp[82592:2135044] [API] Failed to create 0x88 image slot (alpha=1 wide=0) (client=0x293736f1) [0x5 (os/kern) failure]

So this is my test because I have been running into this issue for awhile.

public async Task<bool> TestMongoConnection()
{
try
{
string connectionString = “mongodb+srv://<my username>:<mypass>@pokedex.mayccu6.mongodb.net/”;
string databaseName = “Pokedex”;
string collectionName = “Pokemon”;
//I replaced strings I know the string is correct but omitted for obvious reasons

    Console.WriteLine("Creating MongoClient...");
    var client = new MongoClient(connectionString);
    Console.WriteLine("MongoClient created successfully.");

    Console.WriteLine($"Getting database: {databaseName}");
    var database = client.GetDatabase(databaseName);
    Console.WriteLine("Database retrieved successfully.");

    Console.WriteLine($"Getting collection: {collectionName}");
    var collection = database.GetCollection<BsonDocument>(collectionName);
    Console.WriteLine("Collection retrieved successfully.");

    Console.WriteLine("Counting documents...");
    var result = await collection.CountDocumentsAsync(new BsonDocument());
    Console.WriteLine($"Successfully connected. Document count: {result}");
    return true;
    }
    catch (MongoConfigurationException mcex)
    {
    Console.WriteLine($"MongoDB configuration error: {mcex.Message}");
    Console.WriteLine($"Stack trace: {mcex.StackTrace}");
    return false;
    }
    catch (MongoAuthenticationException maex)
    {
    Console.WriteLine($"MongoDB authentication error: {maex.Message}");
    Console.WriteLine($"Stack trace: {maex.StackTrace}");
    return false;
    }
    catch (MongoConnectionException mcex)
    {
    Console.WriteLine($"MongoDB connection error: {mcex.Message}");
    Console.WriteLine($"Stack trace: {mcex.StackTrace}");
    return false;
    }
    catch (Exception ex)
    {
    Console.WriteLine($"Unexpected error: {ex.GetType().Name} - {ex.Message}");
    Console.WriteLine($"Stack trace: {ex.StackTrace}");
    return false;
    }
    }
}

So the issue happens when I am trying to let the user input a name into the search bar and then it passes their input into this class that is supposed to pull all the relevant data from the pokemon name that was passed, since I ran into this issue so many times i decided to test it instead to see if it recognizes any files and there should be 151 but it keeps returning 0 and I cannot figure out why

I have everything up to date and have the mongodb.driver installed and everything. I have all the packages and have been running this program successfully for awhile without trying to pull from the database, does anyone know a solution or way to figure out why it keeps throwing this exception?

I have been able to confirm that when I put in the same query I have in this directly into the database it does correctly return the information I want so I think it has to do with the connection but I am not sure. I am running this into XCode for an IOS app just in case that is relevant.

Sorry if that is confusing, the issue is that everytime I run it I get the exception thrown that
“‘System.PlatformNotSupportedException’ in MongoDB.Driver.Core.dll
3
Exception thrown:”
This is the rest of my code on this page

public PokemonPage(string searchText)
    {
        InitializeComponent();
        var viewModel = new PokemonDetailViewModel();
        BindingContext = viewModel;
        //viewModel.LoadPokemonDataAsync(searchText);
        TestConnection();

    }
    private async void TestConnection()
    {
    var viewModel = (PokemonDetailViewModel)BindingContext;
    bool connectionSuccessful = await viewModel.TestMongoConnection();
    if (!connectionSuccessful)
    {
    await DisplayAlert("Connection Error", "Failed to connect to the database", "OK");
    }
    }
    public class PokemonDetailViewModel
    {
    
    #region attribute binders
    
    private int number;
    public int Number
    {
    get => number;
    set { number = value; OnPropertyChanged(); }
    }

    private string name;
    public string Name
    {
    get => name;
    set { name = value; OnPropertyChanged(); }
    }
    
    
    private int _hp;
    public int HP
    {
        get => _hp;
        set
        {
            _hp = value;
            OnPropertyChanged();
            OnPropertyChanged(nameof(HPProgress));
        }
    }
    public double HPProgress => HP / 200.0;
    
    
    private double _atk;
    public double Attack
    {
        get => _atk;
        set
        {
            _atk = value;
            OnPropertyChanged();
            OnPropertyChanged(nameof(AttackProgress));
        }
    }
    public double AttackProgress => Attack / 200.0;
    
    
    private double _def;
    public double Defense
    {
        get => _def;
        set
        {
            _def = value;
            OnPropertyChanged();
            OnPropertyChanged(nameof(DefenseProgress));
        }
    }
    public double DefenseProgress => Defense / 200.0;
    
    private double _spatk;
    public double SpAtk
    {
        get => _spatk;
        set
        {
            _spatk = value;
            OnPropertyChanged();
            OnPropertyChanged(nameof(SpAtkProgress));
        }
    }
    public double SpAtkProgress => SpAtk / 200.0;


    private double _spdef;
    public double SpDef
    {
        get => _spdef;
        set
        {
            _spdef = value;
            OnPropertyChanged();
            OnPropertyChanged(nameof(SpDefProgress));
        }
    }
    public double SpDefProgress => SpDef / 200.0;


    private double _spd;
    public double Speed
    {
        get => _spd;
        set
        {
            _spd = value;
            
            OnPropertyChanged();
            OnPropertyChanged(nameof(SpeedProgress));
        }
    }
    public double SpeedProgress => Speed / 200.0;
    

    private string type1;
    public string Type1
    {
    get => type1;
    set { type1 = value; OnPropertyChanged(); }
    }

    private string type2;
    public string Type2
    {
    get => type2;
    set { type2 = value; OnPropertyChanged(); }
    }

    private string height;
    public string Height
    {
    get => height;
    set { height = value; OnPropertyChanged(); }
    }

    private string weight;
    public string Weight
    {
    get => weight;
    set { weight = value; OnPropertyChanged(); }
    }

    private string baseTotal;
    public string BaseTotal
    {
    get => baseTotal;
    set { baseTotal = value; OnPropertyChanged(); }
    }
        #endregion




        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public async Task LoadPokemonDataAsync(string pokemonName)
    {
        try
            {
            Console.WriteLine($"Attempting to connect to database and search for {pokemonName}");
            
            string connectionString = "mongodb+srv://tkhanpsn:[email protected]";
            string databaseName = "Pokedex";
            string collectionName = "Pokedex.Pokemon";

            var client = new MongoClient(connectionString);
            Console.WriteLine("MongoClient created successfully");

            var database = client.GetDatabase(databaseName);
            Console.WriteLine("Database retrieved successfully");
            

            var collection = database.GetCollection<PokemonDetailViewModel>(collectionName);
            Console.WriteLine("Collection retrieved successfully");


            //Use Builders<PokemonViewModel>.Filter instead of Builders<BsonDocument>.Filter
            var filter = Builders<PokemonDetailViewModel>.Filter.Eq($" Name", "{pokemonName}");
            var pokemon = await collection.Find(filter).FirstOrDefaultAsync();
            Console.WriteLine("Find operation completed");


                if (pokemon != null)
                {
                    // Map database fields to properties
                    Console.WriteLine($"Pokemon Found: {pokemonName}");
                    Number = pokemon.Number;
                    Name = pokemon.Name;
                    Attack = pokemon.Attack;
                    Defense = pokemon.Defense;
                    Speed = pokemon.Speed;
                    HP = pokemon.HP;
                    Type1 = pokemon.Type1;
                    Type2 = pokemon.Type2;
                    Weight = pokemon.Weight;
                    Height = pokemon.Height;
                    BaseTotal = pokemon.BaseTotal;

                    // Add other properties you need to set
                }
                else
                    {
                    await Application.Current.MainPage.DisplayAlert("Error", "Pokemon Not Found", "OK");
                    }
            }
            catch (Exception ex)
            {
            await Application.Current.MainPage.DisplayAlert("Error", $"An error occurred: {ex.Message}", "OK");
            }
        }

New contributor

Ethan Lavalley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT