How to add `System.Web.Extensions` assembly to .net core project in vscode
Asked Answered
F

4

25

I have installed c# support for vscode(version is 1.15.0) and created a HelloWorld project via dotnet new console.

Now in Program.cs, I would like to use JavaScriptSerializer class which depends on System.Web.Extensions assembly. I typed using System.Web.Script.Serialization; and run dotnet restore ,but vscode cannot resolve it. The error is,

The type or namespace name 'Web' does not exist in the namespace 'System' (are you missing an assembly reference?)

It seems that System.Web is not part of .net core, but is there any way to add the assembly to the project?

I cannot find a project.json file which is refered in other posts since it is a csproj project.

Flaxseed answered 15/8, 2017 at 6:56 Comment(3)
Why don't you use Newtonsoft.Json for serialization?Perseverance
@Perseverance Actually I am using Newtonsoft.Json for serialization. Here I just want to know how to add assembly.Flaxseed
Please remember to upvote helpful answers and to accept the answer that solved your question to mark this question as resolved.Lindseylindsley
E
22

System.Web.Extensions is part of full .net framework . If you want to serialize and deserialize object,You can use Newtonsoft.Json,

#using Newtonsoft.Json
....
JsonConvert.DeserializeObject(json);

Update

Just get package name and version number from NuGet and add to .csproj then save. You will be prompted to run restore that will import new packages.

<ItemGroup>
     <PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
  </ItemGroup>
Elmerelmina answered 15/8, 2017 at 7:7 Comment(5)
But I want to know how to add assembly like System.Web.Extensions or other in vscode.Flaxseed
you can use dotnet cli dotnet add package Newtonsoft.JsonElmerelmina
Just do not use System.Web. .Net Core is designed to be platform independent. System.Web is Windows native. As Santosh already pointed out: Use Json.NetGallardo
@santoshsingh thanks for update. i tried your code and made it. console gives Package Microsoft.EntityFrameworkCore 2.0.0 is not compatible with netcoreapp1.1(.NETCoreApp,Version=v1.1). Package Microsoft.EntityFrameworkCore 2.0.0 supports: netstandard2.0 (.NETStandard,Version=v2.0) One or more packages are incompatible with .NETCoreApp,Version=v1.1. ,which @marco and @Lindseylindsley both explained.Flaxseed
@santoshsingh Will multiple target framework solve OP's issue?Naevus
L
11

Here I just want to know how to add assembly.

In general, you would have nodes like this in your .csproj:

<Reference Include="System.Web.Extensions" />

However, since you are using .NET Core, you cannot reference assemblies that are targeted to the full .NET Framework. In this case, System.Web.Extensions is one of those, so you cannot use it in your .NET Core project.

Lindseylindsley answered 15/8, 2017 at 7:37 Comment(5)
@Lindseylindsley Will multiple target framework solve OP's issue?Naevus
@Naevus Sure, but then you’re still not using .NET Framework assemblies with .NET Core. You would be using .NET Framework assemblies when targetting .NET Framework, and would still be left figuring out what to do when targetting .NET Core.Lindseylindsley
@Lindseylindsley I want to target both .net core 2.0 and net40 in only one file. All other files have to work on .net core 2.0. I am trying to avoid #if #endif in other files except that one multiframework fileNaevus
@Naevus Yeah, you could do that then but you will have to provide some proper “fallback” for the .NET Core target then. If you need help with that, feel free to open a new question and link it to me.Lindseylindsley
@Lindseylindsley re "Will multiple target framework solve OP's issue?" It depends. If the target is a full .NET framework, it may work. If one or more of the targets is netstandard, then it won't.Elastance
S
2

Replace System.Web.Extensions.Serialization with System.Text.Json.Serialization. The usage of the two is very similar.

Shiverick answered 20/12, 2020 at 20:26 Comment(0)
P
-6

dotnet restore

dotnet restore - Restores the dependencies and tools of a project.

dotnet restore [<ROOT>] [--configfile] [--disable-parallel] [--ignore-failed-sources] [--no-cache] [--no-dependencies] [--packages] [-r|--runtime] [-s|--source] [-v|--verbosity]
dotnet restore [-h|--help]

more help More Help here

Puttee answered 15/8, 2017 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.