Could not load file or assembly 'Newtonsoft.Json' error when an async method is executed

-1

In my mvc application I'm calling one async method, on button click, which will register device into iot hub.

The code works fine in the console app but is giving an issue in the mvc app.

error:

The type initializer for 'Microsoft.Azure.Devices.HttpClientHelper' threw an exception.

Inner exception :

Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Newtonsoft.Json packages is already installed into project 10.0.3 version code

  public async Task<ActionResult> AddDeviceAsync()
    {         
        Device device;
        try
        {
            connectionString = ConfigurationManager.AppSettings["DefaultIoTHubConnection"];          

            registryManager = RegistryManager.CreateFromConnectionString(connectionString);

            List<string> collection = new List<string>();
            for (int i = 0; i < 4; i++)
            {
                collection.Add("dummy_device_" + i);
            }

            foreach (var deviceId in collection)
            {
                try
                {
                    // register device into IoT hub 
                    device = await registryManager.AddDeviceAsync(new Device(deviceId)); // getting exception here
c#
json
azure
c#-4.0
azure-iot-hub
asked on Stack Overflow Oct 16, 2018 by Neo • edited Oct 16, 2018 by Lewis86

1 Answer

1

this problem arises when the CLR executes compiled code and it is not able to find an assembly in the app domain.

The idea here is that the code you are executing was compiled by referencing the version 6.0.0.0 of Newtonsoft.Json, but at runtime you are executing that code in an application domain where the referenced assembly is not available (or the referenced version of the assembly is not available).

Does your asp.net mvc project reference the Newtonsoft.Json nuget package ? What version of the package are you referencing ?

answered on Stack Overflow Oct 16, 2018 by Enrico Massone

User contributions licensed under CC BY-SA 3.0