How to: Set the Correct Target Framework and CPU
Applies to: SharePoint Foundation 2010
This topic explains how to set the correct target Microsoft .NET Framework and the correct target CPU platform for your Microsoft SharePoint Foundation development projects in Microsoft Visual Studio.
The rules for setting the target Microsoft .NET Framework and the CPU platform are as follows:
All Visual Studio projects developed against either the client or server object models of SharePoint Foundation must set Microsoft .NET Framework 3.5 as the target framework. This rule applies even if the Visual Studio project type is not one of the SharePoint 2010 project types.
All Visual Studio projects developed against the server object model of SharePoint Foundation, and thus any of the SharePoint 2010 project types, must set either x64 or Any CPU as the target CPU platform. There are advantages and disadvantages to either choice.
x64 is the more accurate choice, because a solution that is developed against the SharePoint Foundation server object model cannot target "any" CPU. However, to target x64, you must repeat the procedure titled To enable x64 as a CPU platform target (included later in this topic) for each Visual Studio solution that targets x64. Moreover, because some commonly called Microsoft assemblies, such as System.Data.dll and mscorlib.dll are themselves compiled for Any CPU, you may get compiler warnings (not errors) that a referenced assembly targets a different processor.
Any CPU is the easiest choice because it is always available as an option and requires no special procedure to enable. However, it is somewhat misleading, since it seems to imply, falsely, that the resulting assembly could be used on an x86 computer. Moreover, assemblies produced for Any CPU may take up more disk space than assemblies produced for x64.
Setting a project that targets the server object model to the x86 CPU platform target does not generally produce a compiler error. But you will get runtime errors and unexpected runtime behavior. For example, code that gets a reference to the SharePoint Foundation SPFarm object returns null and code that gets a reference to child objects, such as the Web application, will throw FileNotFoundExceptions.
A Visual Studio project developed against the client object model should normally be set to Any CPU as the target CPU platform, because you usually want your client applications to run on both x86 and x64 computers. You can, however, specify x86 or x64 if the application is intended to run on only one kind of client computer. To target x64, you need to take the steps listed in the procedure To enable x64 as a CPU platform target later in this topic.
A Visual Studio solution can contain multiple projects; and in the same solution, you can have a SharePoint 2010 project types as well as other projects that are not SharePoint 2010 project types. If a SharePoint 2010 project references another kind of project, or vice versa, they must have the same target CPU platform. For example, if your solution is a console application that runs on SharePoint Foundation servers, you might want to take advantage of the automated deployment and retraction functionality of SharePoint 2010 project types. In that case, your Visual Studio solution can have an Empty SharePoint Project and a Console Application project. You add the executable of the console application to the package of the SharePoint project by using the Add Additional Assemblies feature of the Advanced tab of the Package Properties window. When you add the Console Application project, it defaults to x86 as the target CPU platform, but the Empty SharePoint Project (like all SharePoint 2010 project types) defaults to Any CPU. After you add the executable to the package, you will get a compiler error until the two projects are set to the same target CPU platform. You must either change the console application to target Any CPU or change both projects to target x64.
To enable x64 as a CPU platform target
After the project is created, on the Visual Studio toolbar, open the Solution Platforms drop-down list box.
Click Configuration Manager.
In the Configuration Manager dialog, open the Active solution platform drop-down list box and click <New> ….
In the New Solution Platform dialog, select x64 in the Type or select the new platform drop-down list box.
Select x86 in the Copy settings from drop-down list box.
In the Configuration Manager dialog, be sure the box in the Build column is checked for all projects in the solution.