Problem with MongoClient in C # .NET

0

I have Mongo's dependencies needed for the project in VisualStudio 2017 Community but an error is generated when instantiating the MongoClient class

Error:

  

Unable to load the file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a' or one of its dependencies. The system can not find the specified file.

     

Description: Unhandled exception when executing the current Web request. Check the stack trace for more information about the error and where it originated in the code.

     

Exception details: System.IO.FileNotFoundException: Unable to load the file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a' or one of its dependencies . The system can not find the specified file.

Source code error:

  

Line 25: var mongoUrl = MongoUrl.Create (connectionString);

     

Line 27: var client = new MongoClient (connectionString);

     

Line 29: // Use the Mongo URL to avoid hard-coding the database name.

The code is as follows:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using MongoDB.Bson;  
using MongoDB.Driver;  
using System.Configuration;  

public partial class _Default : System.Web.UI.Page  
{  
    protected static IMongoClient _client;  
    protected static IMongoDatabase _database;  
    protected void Page_Load(object sender, EventArgs e)  
    {  

        //string connectionString = "mongodb://localhost:27017";
        //MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
        //MongoServerAddress mongoServerAdress = new MongoServerAddress(connectionString);

        // Get your connection string -- use the URL format as in example below:
        // name="MongoConnectionStr" connectionString="mongodb://localhost/bookstore"
        var connectionString = ConfigurationManager.ConnectionStrings["MongoConnectionStr"].ConnectionString;
        var mongoUrl = MongoUrl.Create(connectionString);

        var client = new MongoClient(connectionString); //<-- Error aquí

        // Use the Mongo URL to avoid hard-coding the database name.
        var db = new MongoClient(mongoUrl).GetDatabase(mongoUrl.DatabaseName);
    }
}
    
asked by Parzival 20.10.2017 в 16:23
source

1 answer

1

I solved the error in the following way:
 1. Open tab Sitio Web .
 2. Manage Paquetes Nuget .
 3. Then tab Examinar and search InteropServices .
 4. I selected System.Runtime.InteropServices.RuntimeInformation and System.Runtime.InteropServices and I chose the version that asked me for the error, 4.0.0 in both cases.
 5. I installed and said yes to everything.

I also eliminated the redirection of Web.config , it was a label below <assemblyIdentity> and this way they stopped jumping errors on the web.

  <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity 
              name="System.Runtime.InteropServices.RuntimeInformation" 
                publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        </dependentAssembly>
      </assemblyBinding>
  </runtime>
    
answered by 21.10.2017 в 14:19