Failure to compile Linq Method in App_Code - asp.net

I've been scratching my head for what seems like ages. I'm sure its really simple to fix but I can't see it.
I have a class in App_Code that uses a bit of Linq.
var siteMap = SiteMapWrapper.BuildSiteMap(true);
var currentTopLevelParent = siteMap.Single(s => s.IsActive);
if (currentTopLevelParent != null)
I've developed this locally and all works fine. When I transfer to IIS hosting the same class fails to compile. I receive:
does not contain a definition for 'Single' and no extension method 'Single'
accepting a first argument of type 'SiteMapWrapper' could be found (are you
missing a using directive or an assembly reference?)
I confirmed that the virtual dir is running .NET 2.0 as it should. I also confirmed that the correct assemblies are being loaded in the web.config.
<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
I've tried adding namespaces directives in web.config also but no luck. Can anyone think of anything else to try?
Many thanks.

I think you will find that the server does not have 3.5 installed.

Add using System.Linq; to the top of the .cs file.
Also, make sure that SiteMapWrapper implements IEnumerable<T>.
EDIT: I didn't notice that it works on the local machine.
Make sure that the server has the correct versions of all of the dependent assemblies.

LINQ actually requires .NET 3.0 (3.5 is better), not 2.0. Make sure the AppPool is configured appropriately.

Change the Build Action Property of your .cs file from Content to Compile.
See this article for more info.

Related

Web.Config Assembly/Reference Issue

Today I had to re-create my web.config file, and have lost my previous code.
I have my database connection and roles working fine, however, my assemblies never seem to work... In my back-end code, I always get the error
The type or namespace name 'Insert Here' does not extist in the namespace 'Insert Here' (are you missing an assembly reference?)
My current config file's assembly section has been created by Visual Web Developer by using the Add Reference option:
<configuration>
<system.web>
<compilation>
<assemblies>
<add assembly="mscorlib"/>
<add assembly="System.Web.ApplicationServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="*" />
</assemblies>
</compilation>
---rest of code here---
Just incase it means anything, the tutorials I followed when making my database Membership friendly directed my to the aspnet_regsql.exe in the C:\Windows\Microsoft.NET\Framework\v2.0.50727 directory, but the versions in my web.config are 4.0.0.0. Could this have something to do with it? Should I use the exe found in the v4.0.30319 folder?
If someone could please help me try to get this fixed... I really can't make any progress on my site anymore as all non-database stuff is done...
I figured there was something wrong with my web.config files, so I just made a new website, copied everything over from the original folder and it works.
I know it may not have been the best solution, but it took maybe 10 minutess, far less than the time I've already wasted.

The type or namespace name 'Linq' does not exist in the namespace 'System'

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;
Solution: It seems that the web config should be:
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
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.
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.
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...
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>
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).
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
the problem is you didn't add
debug="true|false" targetFramework="4.0"
in compilation tag.
The solution for me was to:
Go to: Tools > Nuget Package Manager > Manage NuGet Packages for Solution
Install the System.Linq package.
Deleting the hidden .vs folder and restarting Visual Studio fixed it for me.

JavaScriptSerializer Error Message - Duplicate type, how do I resolve?

I'm trying to serialize some strings and return through an ajax call and I'm seeing an odd error. Any help would be very much appreciated:
Compiler Error Message: </b>CS0433:
The type 'System.Web.Script.Serialization.JavaScriptSerializer' exists in both
'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\1.0.61025.0__31bf3856ad364e35\System.Web.Extensions.dll' and
'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll'
The resolution I found to this was: Using the code for a JavascriptSerializer seems to throw exceptions when used within a webform. Place the class call in a web service and it worked perfectly. Perhaps there's some restrictions on the class?!
In my case, this was happening in an existing project. To fix it, I removed the entry for Web Extensions in the Web.config assemblies section. Once I did this it worked fine
<compilation debug="true">
<assemblies>
...
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
...
</assemblies>
</compilation>

VS2008 targeting ASP.net 2.0: Project-level imports warning

I have a web app built against asp.net 2.0, but keep getting the following warning:
Namespace or type specified in the project-level Imports 'System.Xml.Linq' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
I cannot find anywhere that actually Linq is actually referenced. I've even opened up the .vbproj in a text editor, and it's not hidden in there either.
Any ideas where this is set?
Open your web.config and remove the reference to Linq in there. You should need to delete these lines from under the "assemblies" section:
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
Have you checked in your web.config file?
You also get this error if you have an older version of .Net Framework.
Go To Project->Properties->Advanced Compile Options->Target framework(all configurations)
If it is set to .NET Framework 2.0, try changing it to 3.5.
That fixed it for me.

Import Namespace System.Query

I am trying to load Linq on my .Net 3.5 enabled web server by adding the following to my .aspx page:
<%# Import Namespace="System.Query" %>
However, this fails and tells me it cannot find the namespace.
The type or namespace name 'Query' does not exist in the namespace 'System'
I have also tried with no luck:
System.Data.Linq
System.Linq
System.Xml.Linq
I believe that .Net 3.5 is working because var hello = "Hello World" seems to work.
Can anyone help please?
PS: I just want to clarify that I don't use Visual Studio, I simply have a Text Editor and write my code directly into .aspx files.
I have version 2 selected in IIS and I
Well, surely that's your problem? Select 3.5.
Actually, here's the real info:
http://www.hanselman.com/blog/HowToSetAnIISApplicationOrAppPoolToUseASPNET35RatherThan20.aspx
What does the part of your web.config file look like?
Here's what it looks like for a brand new ASP.NET 3.5 project made with Visual Studio 2008:
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
I found the answer :) I needed to add the following to my web.config:
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
Then I could add the following to my code:
<%# Import Namespace="System.Linq" %>
#Will,
Thanks for your help. I have accepted one of your answers :)
Make sure your project is set to target 3.5, and not 2.0.
As others have said, your 'var' test is a test of C#3 (i.e. VS2008), not the 3.5 framework.
If you set the project framework target settings properly, you should not expect to need to manually add dll references at this point.
The var hello stuff is compiler magic and will work without Linq.
Try adding a reference to System.Core
Sorry, I wasn't clear. I meant add System.Core to the web project's references, not to the page.
The Import on the page are basically just using statements, allowing you to skip the namespace on the page.
The csproj file might be missing the System.Core reference.
Look for a line in the csproj file like this:
<Reference Include="System" />
And add a line below it like this:
<Reference Include="System.Core" />

Resources