WCF serialization with class field in expression

Jan 6, 2012 at 11:22 AM


I am trying to pass an Expression<Func<x, bool>> across to a WCF service for use in a where clause. I know to pass it across I need to serialize it and therefore this project seems exactly what I want.

If I use a hardcoded value in the predicate then it all works fine, however I need to pass in the result of a textbox and when I do this the xml produced includes a memberaccess call to my business class and therefore when WCF deserializes it, it can't.


Works fine: wherePredicate = (bk => bk.BookTitle.ToLower().Contains("betty")); => hardcoded value

Doesn't work: wherePredicate = (bk => bk.BookTitle.ToLower().Contains(filter)); => where filter is a variable in my method.

In xml produced:

- <MemberExpression NodeType="MemberAccess" CanReduce="false">
- <Member MemberType="Field" FieldName="filter">
- <DeclaringType>
  <Type Name="MyBusinessClass.MyViewModel+<>c__DisplayClass6" />
- <Expression>
- <ConstantExpression NodeType="Constant" CanReduce="false">
- <Type>
  <Type Name="MyBusinessClass.MyViewModel+<>c__DisplayClass6" />
- <Type>
  <Type Name="System.String" />

I have tried creating Expression.Constant in the hope that this would fix it but I still get the same trouble whatever I try.

Any help would be very appreciated as I have been looking at this for days now!



Jul 4, 2012 at 4:49 PM

I too am getting this error and would really love to find a solution.

I've provided a list of the  Types it need to the TypeResolver (which is a list of types of my 'models') but it fails on pretty much the same thing as Claire above, with the DisplayClass nonsense.


Any ideas? Anybody? I thought I was onto a winner when I had constants in my Expression but as soon as I switch to making those dynamic (from variables) it started to go wrong :-(