1

How to pass parameters from client to method in server in order to get entity data.

At Server side :

List<Foo> getFoos = _serverObject.GetFoos(p1, p2); //Here's how I am getting foo from another server

class FooProvider
{ 
   public IQueryable<Foo> Foos
   {
      get{return getFoos.AsQueryable();}
   }
}

At Client Side :

var res = from f in ctx.Foos
          where f.p1 == p1_val && f.p2 == p2_val
          select f;

Now what I am trying to get is all Foo record depend on p1_val and p2_val parameters, but I would have to pass it to _serverObject.GetFoos(p1, p2); so that the p1 and p2 are set from client not from the server.

Please suggest some mechanism

ThomasBecker
  • 388
  • 6
  • 20
  • 1
    in OData service you need to have your entity context first then you can apply filter on it and get the data at client side. But as I understand correctly your context data is depended on the filter parameters.. It can be easily implemented if you consider another service for parameter – MKMohanty Sep 26 '14 at 15:33
  • I think it should be possible with single service as client is giving the queryable expression to server and from that it should load the parameters. Isn't it?? – ThomasBecker Sep 26 '14 at 15:36
  • You would need to pass a predicate. Look at the following: http://stackoverflow.com/questions/1299534/linq-passing-lambda-expression-as-parameter-to-be-executed-and-returned-by-meth – Sasse Sep 29 '14 at 10:06
  • That's my problem also with OData service that I cant use predicate as expression is implemented on client side on context rather than passing expression from client to server then implementing it . – ThomasBecker Sep 29 '14 at 14:23

1 Answers1

1

I do believe what you try to achieve is mostly related to

Using an ADO.NET Entity Framework DataSource(WCF Data Services)

and passing a parameter from client to server is only the tip of the iceberg however if I am wrong and all your problems will be resolved by only passing complex types p1, and p2 from client to server then do the following:

  1. In your WCF server simply define 2 new Data Contracts and call them p1 and p2 with all your desired fields
  2. Now on the client side when you add a service reference to the newly created service, you will be able to populate p1 and p2 and pass them to your server from your Proxy class when you call your service method

Moral of the story: You need to define those two types in your service as Data Contracts so you can use them as parameter in your proxy class on the client side.

Community
  • 1
  • 1
MHOOS
  • 5,146
  • 11
  • 39
  • 74
  • Thanks for your reply. Does OData supports DataContract? Conceptually I agree with you but I doubt in its implementation. I will test and let you know as soon as possible. – ThomasBecker Oct 01 '14 at 08:52
  • 1
    Have a look at the following post: http://stackoverflow.com/questions/15802285/can-we-convert-odata-metadata-to-c-sharp-code-datacontract – MHOOS Oct 01 '14 at 08:56
  • Thanks again, I have now the data from metadata at client side, but how should I pass this to server as Odata service does not have any service contract and operation contract. Should I use [webget]? – ThomasBecker Oct 01 '14 at 09:20
  • Could you please give some small implementation at client side. – ThomasBecker Oct 01 '14 at 09:27
  • I don't know which kind of binding you will use to access your service however I would consider you want to make your WCF service restful which consequently means you will be able to access it through a browser and you would need to annotate your get methods with that [webget] attributes. If that is going to be the case then follow http://msdn.microsoft.com/en-us/library/dd728279(v=vs.110).aspx – MHOOS Oct 01 '14 at 09:45
  • Problem now solved, I have to make properties in provider class and then I just have to map the parameters to that properties which I get from [WebGet] method. I am marking your solution as an Answer as it led me to the solution which I need. Thanks a lot!! :) Though I will put full solution of mine, may it will be helpful to others. – ThomasBecker Oct 01 '14 at 14:06