24

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.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
39ecneret
  • 661
  • 3
  • 7
  • 17
  • 2
    Why don't you use Newtonsoft.Json for serialization? – koelkastfilosoof Aug 15 '17 at 07:04
  • @koelkastfilosoof Actually I am using Newtonsoft.Json for serialization. Here I just want to know how to add assembly. – 39ecneret Aug 15 '17 at 07:13
  • Please remember to upvote helpful answers and to [accept the answer](https://meta.stackexchange.com/a/5235/141542) that solved your question to mark this question as resolved. – poke Aug 15 '17 at 09:54

4 Answers4

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>
santosh singh
  • 27,666
  • 26
  • 83
  • 129
  • 2
    But I want to know how to add assembly like `System.Web.Extensions` or other in vscode. – 39ecneret Aug 15 '17 at 07:23
  • 1
    you can use dotnet cli dotnet add package Newtonsoft.Json – santosh singh Aug 15 '17 at 07:25
  • 3
    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.Net – Marco Aug 15 '17 at 07:30
  • @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 @poke both explained. – 39ecneret Aug 15 '17 at 12:30
  • @santoshsingh Will multiple target framework solve OP's issue? – ManirajSS Jul 24 '18 at 06:04
10

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.

poke
  • 369,085
  • 72
  • 557
  • 602
  • @poke Will multiple target framework solve OP's issue? – ManirajSS Jul 24 '18 at 06:05
  • @ManirajSS 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. – poke Jul 24 '18 at 07:42
  • @poke 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 file – ManirajSS Jul 24 '18 at 07:46
  • 1
    @ManirajSS 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. – poke Jul 24 '18 at 07:48
  • @poke 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. – Manfred Jan 15 '20 at 20:44
2

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

张智凯
  • 21
  • 1
-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