Enabling LINQ with ASP.NET

By Walter Oliver

November 2, 2007

Overview

ASP.NET is a unified Web development model that includes the services necessary to build Web applications with a minimum of coding. ASP.NET is part of the .NET Framework, and when coding ASP.NET applications you have access to classes in the .NET Framework. You can code your applications in any language compatible with the common language runtime (CLR), including Microsoft Visual Basic, C#, JScript .NET, and J#. These languages enable you to develop ASP.NET applications that benefit from the common language runtime, type safety, inheritance, and so on.

Language-Integrated Query (LINQ) is a set of features in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store.

Important: When hosting Web Applications that use LINQ, you might have to change the policy files for code-access security.

The following sections describe these settings. See Using LINQ with ASP.NET for more information.

Using LINQ with Medium Trust

To use LINQ in a Web application that is running under medium trust, you must include two elements (SecurityClass and IPermission) in the policy file that is defined for Medium trust. By default, the web_mediumtrust.config file is the policy file for medium trust and already contains these elements.

Within the SecurityClasses element, add a SecurityClass element with the following attributes:

<SecurityClass
Name="ReflectionPermission"
Description="System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

Within the PermissionSet element that has the Name attribute set to "ASP.Net", add an IPermission element that has the following attributes:

<IPermission
class="ReflectionPermission"
version="1"
Flags="RestrictedMemberAccess"
/>

Using LINQ with High Trust

To use LINQ in a Web application that is running under high trust, you must include one element in the policy file that is defined for High trust. By default, the web_hightrust.config file is the policy file for high trust. This file already includes an IPermission element within a PermissionsSet element that references the ReflectionPermission class. You must modify this element when you use LINQ.

Within the PermissionSet element that has the Name attribute set to "ASP.Net", find the IPermission element for ReflectionPermission and set it as follows:

<IPermission class="ReflectionPermission" version="1" Flags="ReflectionEmit, RestrictedMemberAccess" />