How to reference an assembly within the context of an ASP.NET web application

0

I am facing a pretty harsh issue with my ASP.NET web app, I am using visual studio community 2017, target framework .NET core 2.2.

I have to parse a JSON string and found that the System.Web.Script.Serialization can be pretty useful in this situation. Unfortunately, it seems like visual studio is unable to reach for the namespace. Any idea as to how to let the IDE find it ? Otherwise, any suggestion as to how to download/parse JSON files in another way would be greatly appreciated.

I have tried to add a reference in the project's dependencies towards the corresponding assembly (System.Web.Extensions.dll) which let visual studio find the namespace but resulted in the following internal server error :

BadImageFormatException: Could not load file or assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

While roaming around, I saw somebody's suggestion to delete the assembly (System.Web.Extensions.dll) from the \bin file located within the projet. This also ended in an error : HTTP Error 500.0 - ANCM In-Process Handler Load Failure

Here is the piece of code specific to the part where I want to parse the JSON file, basically, if I comment it, the web app runs normally.


using System.Web.Script.Serialization;

namespace myApp.Models

{

/* Class : MashModel
 * Downloads, parses and stores a list of elements from the JSON file
 * Author : SoraGtg
 */
public class Elements
{
    public List<Elements> elements { get; set; }

    public void GatherElts()
    {
        var json = new WebClient().DownloadString("url/to/json");
        this.elements = new JavaScriptSerializer().Deserialize<List<Cat>>(json);
    }
}

}


To this point I am not trying anything in the view and controller parts with the data issued by the json (elements property).

I would like the web app to be deployed and to embed the features offered by System.Web.Extensions assembly, but it won't deploy because it cannot reach for them (cannot reference the assembly, cannot find it by itself).

Thank you !

asp.net-core
javascriptserializer
asked on Stack Overflow Jun 13, 2019 by J. Valjean • edited Jun 23, 2019 by Stephen Kennedy

1 Answer

1

I think you're trying to use a library thats not part of Net Core, and therefore it cannot load the DLL. Using something like NewtonsoftJson instead would solve your problem. They have a version for Net Core: https://github.com/JamesNK/Newtonsoft.Json

answered on Stack Overflow Jun 14, 2019 by Robert Perry • edited Jul 18, 2019 by Stephen Kennedy

User contributions licensed under CC BY-SA 3.0