The type or namespace name 'var' could not be found in WCF Service Application
When开发者_如何转开发 I am trying to use "var" in the WCF Service application it is giving error "The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)"
You get this error if you try to use var
in a class member, e.g.:
public class Foo
{
var a = 4;
}
var
can only be used inside a method, not in classes, fields or method signatures.
See also: Why no var on fields?
I would imagine its because your targeting a framework before c#4.0. Try to go to your projects properties and set the target framework to 4.0
For my ASP.NET 3.5 project, I had to make sure that I had the 3.5 framework compiler setup in my web.config file like:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
I'm taking a wild guess here, but: var
may only be used for local variabels (inside a method).
Are you using it to define instance variables?
You should ensure, you are building your service for .NET 3.5 or higher. The "var" keyword was not supported before.
Maybe you accidentally call csc.exe
from a directory you don't expect? I solved the problem by running csc.exe
using full path:
set csc=c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
%csc% try.cs
rem This was wrong:
set path=%path%;c:\Windows\Microsoft.NET\Framework\v4.0.30319
csc.exe
rem This shows csc version, which turned out to be from Framework 2.0.
精彩评论