1

I wonder if I could expose a web method with Func parameter!

[WebMethod]
public List<Entity> ReadEntities(Func<Entity, bool> predicate)
{
    using (var entities = new GwEntities())
        return entities.Entities.Where(predicate).ToList();
}

When I tried to update the web reference I had an exception from Visual Studio that:

System.Func[GwModel.Entity,SystemBoolean] caonnot be serialized because it does not have a parameterless constructor

Any workaround please!

Homam
  • 23,263
  • 32
  • 111
  • 187

3 Answers3

4

You cannot serialize your own code like this. Where should the serializer start and where stop? Should it transfer only the code of your method and all assemblies that are called from within the method? LINQ to objects does something similar. You should have a look at Expressions. These can be serialized across the wire.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Alois Kraus
  • 13,229
  • 1
  • 38
  • 64
  • There is also a question discussing that: http://stackoverflow.com/questions/217961/serializing-and-deserializing-expression-trees-in-c – ChrisWue Apr 25 '11 at 10:49
2

No can do. If you think of the func parameter as a reference to some code on the caller's side, how do you expect the server to be able to see that code?

geofftnz
  • 9,954
  • 2
  • 42
  • 50
1

i think you might not be able to implement client callbacks with classical asp.net web services. you can opt for WCF for this. KickStart.

Mubashir Khan
  • 1,464
  • 1
  • 12
  • 17