1

I am connecting to an ODATA Service via a C# ASP.NET application, which has service operations such as:

GetItems(int? itemID, double? price)

I can consume this without issues in my browser, e.g.

http://api.mycompany.com/companycatalogue/GetItems?itemID=4

I understand how to use LINQ to Entities to consume an ODATA service, but can't find a decent explanation of how to consume service operations like the one above in C#. I have made a web reference to the service in my Visual Studio solution.

So far, I have something like this for my usual consuming of the data:

using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
    CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities (new Uri("http://api.mycompany.com/companycatalogue/"));
    var result = from i in dataContext.Items select i;  //just an example

    //this is where I get into problems
    var operationResults = CompanyCatalogue.GetItems(6, 20.5); //I just made this up
}

Any pointers?

Zac
  • 1,722
  • 1
  • 19
  • 22

3 Answers3

3

OK, got the answer:

using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
    CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities(); 

    DataServiceQuery<GetItemsResult> q = dataContext.CreateQuery<GetItemsResult>("GetItems")
        .AddQueryOption("paramName", 6)
        .AddQueryOption("paramName2", 20.5);

    List<GetItemsResult> items = q.Execute().ToList();
}
Zac
  • 1,722
  • 1
  • 19
  • 22
1

This may be help for you. This sample code used to consume the service operation in the WFC Data Service. This service operation(GetRowCount) returns the integer(int) type value. input para name is "code"

    var q = context.CreateQuery<int>("GetRowCount").AddQueryOption("code", "'" + serviceProvider.Code + "'");
            int RecordCount = context.Execute<int>(new Uri(q.RequestUri.ToString().Replace("GetRowCount()", "GetRowCount"))).FirstOrDefault();
Laksh
  • 31
  • 1
0

Have you tried using HttpWebRequest?

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • Do you mean, using the url and parsing the output? – Zac Oct 07 '10 at 11:49
  • @Zac Yes. Alternatively, you can use HTTPClient from the WCF REST Starter Kit, it will do all the heavy lifting for you. Even deserializing into strong types. – Darrel Miller Oct 07 '10 at 12:41