I have an ASP MVC 2 project, originally written in VS 2008 but now upgraded to VS 2010. I want, however, to remain with .net 3.5 since the production environment does not always have .net 4 installed yet. I thought this was possible. When I upgraded the project I answered no to the question about upgrading the .net framework version. I have checked and .net 3.5 is set as the target framework in the advanced compile options for the project.
My problem is that I can write code which builds and runs fine in VS but which doesn't work in a .net 3.5 environment. For example, I added a function with an optional paramater of type Integer? (nullable integer). This appears to be allowed in 4 but not in 3.5? Anyway, works fine in VS 2010. However, when I try to build and deploy I run into one of two problems. (We use custom build/deploy scripts - build uses msbuild on a separate build server.)
Porblem 1 is if i try to use the 3.5 version of msbuild. Then the build fails with message "error BC31405: Optional parameters cannot have structure types.".
Problem 2 is if I try to use the .net 4 version of msbuild on the build server (remember that the target framework is still 3.5, the only change is which msbuild executable the build script uses). In this case it builds, but when I try to run the site on a server which has 3.5 but not .net 4 installed I get the error "Could not load file or assembly 'ProjectName.XmlSerializers' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded."
According to MSDN (http://msdn.microsoft.com/en-us/library/bb398202.aspx) all I need to do is make sure the target framework is right (and the references, but there doesn't seem to be a problem there in that it builds fine against 3.5 as long as I avoid certain syntaxes, etc.)
The problem is that I get no error in VS to tell me there could be a problem if I use those structures which won't build. Accoridng to the Visual Studio Multi-Targeting Overview article on MSDN (http://msdn.microsoft.com/en-us/library/bb398197.aspx) "when you work on a project that targets an earlier version of the .NET Framework, Visual Studio dynamically makes changes in the development environment, such as the following ... It compiles and builds with the appropriate version of the compiler and with the appropriate compiler options" So why does it compile in VS but not with msbuild?
I wondered if it was the toolset property that was the problem (if I specify /tv:3.5 on the command line to msbuild 4 I get the same build errors as with msbuild 3.5). According to the MSBuild Multitargeting article (http://msdn.microsoft.com/en-us/library/ee395432.aspx) "You designate the tool set by setting the ToolsVersion attribute of the Project element of a project file". However, if I c开发者_Go百科hange the ToolsVersion="4.0" to ToolsVersion="3.5" in the .vbproj file then the Visual Studio Conversion Wizard pops up when i try to reload the proejct.
So, how can I either:
A - Get VS 2010 to accept that it's a .net 3.5 project and use the appropraite background compiler and therefore show me errors during development (before it gets to the build server)?
or
B - Build the project is such a way (with msbuild 4 for example) that it will build but still run in a .net 3.5 only environment?
Seems like you are trying to use C# 4.0 Language Features
in a .Net Framework 3.5
application.
Refrain from using new features introduced in C# 4.0
such as:
- Optional parameters
- Dynamic Programming
- Covariance/Contravariance
- And others listed here.
And you should be fine.
精彩评论