0
votes

I'm re-writing a console application using Visual Studio 2010. The original application was written using Visual Studio 2008 and works OK, but has no tests associated with it. Hence, the idea of re-writing it with tests.

Both applications are working with a Sharepoint 2007 site.

The project compiles but when I try and run it the code below is throwing errors.

        SPSite spsite = null;
        SPSecurity.RunWithElevatedPrivileges(delegate() {
            spsite = new SPSite("http://sharepointdev");
        });

        return spsite;

I'm running this on Windows Server 2008. I've set the platform target of the build to be x86 (this is in the properties of my project) and in Security I've checked that this is a full trust application. I'm also running the project as an administrator. I've also set the .NET Framework to be 3.5

Is it just a case that SharePoint 2007 just doesn't want to play with Visual Studio 2010, or is there something else I've not yet considered?

I've tried searching on the web and stackoverflow but all the articles I've seen deal with trying to get Visual Studio 2010 to work with Sharepoint 2010.

The exact error I get is 'Object reference not set to an instant of an object' with the debugger high-lighting the SPSecurity call.

If I just use this code

using(SPSite spsite = new SPSite("http://sharepointdev")){

Then I get a FileNotFoundException, which isn't exactly true! I've got plenty of applications that do find a site using that URL. What I suspect the error message is trying to say is that my 2010 application is not being allowed to access the site, but I've not found any clues as to why that should be.

Any clues, hints or suggestions gratefully accepted.

EDIT

I've lifted the code from my VS 2010 project and dropped it into a new VS 2008 project and it ran straight-away.

FURTHER EDIT

I created a simple little console application in VS 2010. By default it uses .NET Framework 4, I had to set this to .NET Framework 3.5. I also set the platform target to be 'Any CPU' and it works. This makes me wonder if there is an issue with the Test Project associated with my first application?

I re-created the console application but this time without a Test Project associated with it (the test project was a class library and worked with NUnit). It ran with no problems. I guess the problem lies within the test project and something there that the solution doesn't like. Probably there's a build there it doesn't like

1
Unrelated to your specific problem: In your first code listing you're creating a new SPSite object and returning it to the caller. I really hope you're disposing this object when you're done with it. You will get a lot of leaked memory and bad performance (the app pool recycles from reaching its memory threshold) if you don't take care to do this. - Erik Noren
Hi Eric, Not to worry I am disposing that object. The first bit of code comes from a function, which is called from within a using block. Thank you for pointing out a potential problem though. - Daniel Hollinrake
Weird - was the test project targeting .NET 4.0? - Erik Noren
Hi Eric. Initially, yes it was but I switched it to .NET 3.5 and still got that error. The only things I'm using in the test project is NUnit and Rhino.Mocks. I marked your response as the answer since it helped me a great deal and will help anyone else who comes across this question. If I get chance I'll see what happens when I add a test project to the re-created app and let you know. - Daniel Hollinrake

1 Answers

2
votes

TL;DR; answer: Switch to Any CPU build - don't choose x86.

I've run into this problem before with a console utility. I got the same FileNotFound error but it's referring to the DLL, not your SharePoint site. Digging a bit I discovered a deeper error of BadImageFormat and realized it was complaining about a DLL or EXE. I guessed it was due to the linking from x86 to MSIL. There's a lot of things that have to happen to marshal calls between the two and I guess it led to an incompatibility. When I switched it (and verified all my support library projects) built to MSIL/Any CPU the application worked with no problems and no other changes.

I use Visual Studio 2010 to develop for SharePoint 2007 all day for a long time now and the only problems I've encountered aren't related to that combination. More often than not it's a quirk of a 3rd party add-on I'm using. For almost all of my development I start with the WSP Builder templates but they are buggy and have a few quirks you have to work around (some severe enough to take down your SharePoint server) so I don't blame VS 2010 directly.