1

I'm creating an asmx webservice in an existing .Net 3.5 website. I would like to return a post based on a key. I would like to return the post as an anonymous type, but it is giving me the following error:

Feature 'anonymous types' cannot be used because it is not part of the ISO-2 C# language specification.

Here is a screen-dump of the problem: Feature 'anonymous types' cannot be used because it is not part of the ISO-2 C# language specification

Any ideas how to solve this?

Note: my asmx is standing on its own. It contains the code, it has no .cs behind it.

Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
  • 1
    See this answer: does that fix it? http://stackoverflow.com/a/3314483/23354 - note: that just makes the compiler happy; I agree with Fredrik/Steve/Paul/Lloyd that this is **not** a good approach – Marc Gravell Jan 10 '12 at 09:34
  • 1
    Return a concrete type, new ConcreteType() { PostId = yada; } – Lloyd Jan 10 '12 at 09:34

4 Answers4

9

The first problem is that you are returning an object.

As WebService produce definitions (WSDL), how do you expect the definition to be generated without knowing the actual type ?

You should introduce a DTO styled - class (no logic, only data) like :

[Serializable]
public class Post
{
    public int PostID {get;set;}
    public int ThreadID { get; set; }
    public string Subject { get; set; }
    public string Body { get; set;}
}

And change your definition to return such class.

The amount of code is quite light.

[Edit] The above solution will solve your problem. However, the exact source of the problem is this one :

When you works in a .cs file, the compiler used is the compiler of the target framework of your project (3.5 here). The compilation occurs at coding time.

When you works in a .asmx file, the compilation will occurs when the asp.net application will be loaded. The compiler used in the compiler of the asp.net runtime, which is, for the .Net 3.5, the compiler for 2.0 runtime. It's because the Framework 3.5 is only a new set of classes, but the CLR is still in V2 (changed with the V4). In this case, even if your project is in 3.5, only the code in .cs files can use 3.5 language features. All code in aspx and asmx files can only use V2 language feature.

Steve B
  • 36,818
  • 21
  • 101
  • 174
2

Is this website or web application?

You probably have set older langversion on your project.

Look at this: http://msdn.microsoft.com/en-us/library/f4ckecs0(v=vs.110).aspx

If it's website, check what is set in web.config in compiler section. If there is an attribute:

<compiler language="c#;cs;csharp" extension=".cs" compilerOptions="/langversion:ISO-2"  ... >

remove langversion parameter, or change it to

compilerOptions="/langversion:3"
Jan Remunda
  • 7,840
  • 8
  • 51
  • 60
1

I think you have your answer.

If I were you, I'd simply create a POCO ( plain old C# object ) which models the anonymous structure you are looking to pass.

Paul Alan Taylor
  • 10,474
  • 1
  • 26
  • 42
1

WebService technology is based on xml documents known as WSDL, which contains description of functions, parameters and return types of your service. Clients of your webservice know what to expect from your service from this document. If you don't specify return type, document cannot be created. You have to return object of specific class in your function.

Adrian Serafin
  • 7,665
  • 5
  • 46
  • 67
  • I've uses anonymous object with [WebMethod] methods before (.Net 4). It worked perfectly (converting them to JSON). – Kees C. Bakker Jan 10 '12 at 09:55
  • 1
    it could be done, but i don't think this is what webservices are supposed to do. Knowing what you can get in response and being able to auto-generate classes from wsdl is one of the advantages that webservices have over for example http/rest or pure socket communication. If you don't want that why bother and use webservice? Just create some http site and accept post requests. – Adrian Serafin Jan 10 '12 at 09:58