Error: System.IO.FileNotFoundException: The web application at <servername> could not be found (Console Applications with SP2010)
Posted
Tuesday, December 1, 2009 10:33 AM
by
CoreyRoth
If you have tried to build a quick console application using Visual Studio 2010 to work with SharePoint 2010, you might find that you run into the following error.
Unhandled Exception: System.IO.FileNotFoundException: The Web application at http://sp2010 could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
Consider the following console application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (SPSite siteCollection = new SPSite("http://sp2010"))
{
Console.WriteLine("Site Title: {0}", siteCollection.RootWeb.Title);
}
Console.ReadLine();
}
}
}
All this console application does is display the title of the root web of the site collection. However, when I run it, I get the FileNotFound error.
You first might think to check your path or something like that, but this is actually caused by a completely different issue. As you know by now, SharePoint 2010 is a 64 bit application. However, when you create a new Console Application, it does not default to x64, it defaults to x86 as shown below.
View your project properties, go to the Build tab and change the platform target to x64. Run your console application again and everything should work as expected now.
This doesn’t just apply to Console applications, but could also apply to any other type of external application you might build. Just make sure it is set to x64. Technically Any CPU would work as well, but SharePoint only runs on x64 so you might as well just use that.