26

I have a wcf service hosted in a website in IIS and I seem to have this issue. In my web.config I have this:

<system.web>
        <compilation>
            <assemblies>
                <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies>
        </compilation>
    </system.web>

All projects in the solution target framework 4.0.

LE: I get the error when I try to import System.Linq;

 using System.Linq;
ilter
  • 4,030
  • 3
  • 34
  • 51
gigi
  • 3,846
  • 7
  • 37
  • 50
  • 3
    Please post the code where the error is occurring. – Jesse C. Slicer Jan 30 '12 at 21:05
  • FYI, it's generally a bad idea to use web site "projects" for WCF services (or anything other than fairly simple web sites). – John Saunders Jan 30 '12 at 21:11
  • actually, it's a fairly simple web site :) – gigi Jan 30 '12 at 21:13
  • @JohnSaunders Do you mean web application projects? I was under the impression web site projects were preferred for larger sites. Could you cite a reference? http://stackoverflow.com/questions/398037/asp-net-web-site-or-web-application seems to indicate there are pros and cons to both. – pseudocoder Jan 30 '12 at 21:26
  • 5
    Web sites are not projects. They are unique in that they do not build. They have many unique problems that no other Visual Studio "project" type has, including strange things happening with namespace recognition. If you must use a web site "project" for your actual site,then at least use a web application project for your services. You'll be glad you did. – John Saunders Jan 30 '12 at 21:28
  • 1
    Don't edit your answer into the question. Post it as an answer and accept it. – CodesInChaos Jan 30 '12 at 22:59
  • @gigi If specifying the `targetFramework` attribute fixed your problem, then either a higher level web.config specified a different target version, or your IIS application pool is incorrectly configured. I would check your app pool first. – pseudocoder Jan 31 '12 at 15:29
  • @JohnSaunders The only thing preventing adoption of *web projects* over *"web-sites"* is that with a *"web-project"* all the code is compiled into assembly dlls; rather than having original source code on the web-server. – Ian Boyd Nov 03 '21 at 18:54

10 Answers10

20

Solution: It seems that the web config should be:

<system.web>
    <compilation debug="true" targetFramework="4.0"/>
</system.web>
gigi
  • 3,846
  • 7
  • 37
  • 50
4

Do you have a code file in your site with either Imports System.Linq (VB) or using System.Linq; (c#)?

Seems like the simplest answer is that it is a typo. Maybe the namespace should be corrected to System.Data.Linq.

Edit: System.Linq should be a valid namespace, as it "provides classes and interfaces that support queries that use Language-Integrated Query (LINQ)." (http://msdn.microsoft.com/en-us/library/system.linq.aspx). It is also imported by default in the system-level web.config.

So, not sure what is happening here if it is not related to my suggestion above. Maybe something wrong in your machine.config or system-level web.config?

Edit 2: I find it strange that you adding the System.Core assembly at this level. I believe this is the assembly that includes the System.Linq namespace. Maybe removing it would help?

Edit 3: System.Linq is imported by default in the machine-level web.config. You can remove the line in your code file.

pseudocoder
  • 4,314
  • 2
  • 25
  • 40
  • System.Linq is a namespace but it is in System.Core.dll assembly, which i referenced, damn this is strange. – gigi Jan 30 '12 at 21:28
  • @gigi yeah this is a strange one. Did this start out as a 3.5 project or something? And, just to clarify: Is it a web SITE project or web APPLICATION project? – pseudocoder Jan 30 '12 at 21:31
  • Removing System.Core is not helping, it is a web site and all the projects from the application are developed in .net 4.0 ( all started in .net 4.0 ) – gigi Jan 30 '12 at 21:33
  • @gigi I suggest removing the import line in your code file. It shouldn't be necessary anyway. – pseudocoder Jan 31 '12 at 15:17
3

Sometimes it "disappears" just for the fun of it. If you don't need it just delete it.

Else right click your website in the solution explorer and add a reference to it.

f2lollpll
  • 997
  • 6
  • 15
2

I had no Linq methods on my Controller (Count, Where, toList, etc...). Adding the namespace:

using System.Linq;

fixed the problem for me. Maybe will help someone in the future...

ilter
  • 4,030
  • 3
  • 34
  • 51
  • Thanks. Turns out that Visual Studio was too dumb to find this namespace by itself when "ctrl + ." on the compilation error. – Jérôme MEVEL Oct 03 '16 at 07:19
1

I solved this problem by adding the System.Linq namespace to the namespaces in the Views\Shared\Web.config file, e.g, as below, 2nd from the bottom, above the WebApplication1 namespace.

Note, this is in the Web.config in the Views folder and is not the web site's main 'Web.config`.

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Optimization"/>
            <add namespace="System.Web.Routing" />
            <add namespace="System.Linq" />
            <add namespace="WebApplication1" />
        </namespaces>
    </pages>
</system.web.webPages.razor>
ProfK
  • 49,207
  • 121
  • 399
  • 775
1

Another thing to check is is your .csproj file, specifically these items:

<TargetFrameworkProfile>Profile47</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>

If you don't have a folder for the Profile indicated, you might need to manually change it to one you do have. You might also be able to download the target from https://www.microsoft.com/net/download/visual-studio-sdks.

Unfortunately, my colleague had this happen with an old-style PCL project (profile-based PCL), so he wasn't able to go download the .NET Portable target, but I had to give him the DLLs out of my functioning folder (C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile47).

NH.
  • 2,240
  • 2
  • 23
  • 37
0

the problem is you didn't add

debug="true|false" targetFramework="4.0"

in compilation tag.

Jon Davis
  • 6,562
  • 5
  • 43
  • 60
kidistB
  • 26
  • 2
0

I had the same error but my web app was running on framework v2.0. Changed my application pool from v2.0 to v4.0. all working...happy days

0

The solution for me was to:

  1. Go to: Tools > Nuget Package Manager > Manage NuGet Packages for Solution

  2. Install the System.Linq package.

Andrew
  • 18,680
  • 13
  • 103
  • 118
0

Deleting the hidden .vs folder and restarting Visual Studio fixed it for me.

Andrew
  • 18,680
  • 13
  • 103
  • 118