locked
How to add OData v4 Endpoint (using asp.net web API 2.2) to MVC client application RRS feed

  • Question

  • User-2145531484 posted

    Hi,

    I am new to web api. I have created an OData v4 endpoint using asp.net web API 2.2 using VS 2013. I have followed this article http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint. It's working fine. (tested through fiddler also)

    Service side:

    {
      "@odata.context":"http://localhost:57549/odata/$metadata","value":[
        {
          "name":"Courses","kind":"EntitySet","url":"Courses"
        },{
          "name":"Departments","kind":"EntitySet","url":"Departments"
        }
      ]
    }

    Client side:

    Now I have MVC client application (in VS 2012) to which I want to add service reference for my OData v4 Endpoint. I have tried using add service reference but it throws following error (I have followed this article:http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/calling-an-odata-service-from-a-net-client):

    There was an error downloading 'http://localhost:57549/odata/_vti_bin/ListData.svc/$metadata'.
    The request failed with HTTP status 404: Not Found.
    Metadata contains a reference that cannot be resolved: 'http://localhost:57549/odata'.
    The remote server returned an unexpected response: (405) Method Not Allowed.
    The remote server returned an error: (405) Method Not Allowed.
    If the service is defined in the current solution, try building the solution and adding the service reference again.

    How to resolve above error? Is that possible to add Odata endpoint (using asp.net web api 2.2) through Add Service Reference?

    Thanks,

    Wednesday, October 1, 2014 3:36 PM

Answers

  • User-2145531484 posted

    Hi Damien,

    There was an update for Nuget Package Manager. I figured that out from VS 2012 -->Tools --> Extensions and Updates..--> Updates

    Now I am able to add those packages.  I will consume that service with client and let you know.

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 3, 2014 1:38 PM

All replies

  • User1992938117 posted

    Can you host your service in local iis and then try...

    For service testing you can use LINQ Pad..

    Thursday, October 2, 2014 2:31 AM
  • User1066278571 posted

    Use the OData V4 code generator instead. You have an OData V4 service, not a V3 service.

    http://damienbod.wordpress.com/2014/07/09/web-api-odata-v4-using-an-odata-t4-generated-client-part-8/

    Greetings Damien

    Thursday, October 2, 2014 4:45 AM
  • User-2145531484 posted

    Hi Rajneesh,

    I have hosted my service in local IIS and tried but it didn't work. It is throwing me same error. 

    When I was creating my service, I have picked empty MVC web application project template and just followed following link to create the endpoints:

    http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint

    I have researched online and found people are saying that we can't add web api through add service reference because it is REST or HTTP.  And I have found following article on asp.net site which says we can add service using add service reference ( http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/calling-an-odata-service-from-a-net-client ). Only difference is I am using v4 and this article is for v3 protocol. Is that a difference?

    Both above statements are conflicts to each other. Not sure what is right?

    Thanks,

    Thursday, October 2, 2014 9:35 AM
  • User-2145531484 posted

    Hi Damien,

    How does protocol make a difference here? I am trying to understand..

    Thanks,

    Thursday, October 2, 2014 9:36 AM
  • User1066278571 posted

    OData V3 and OData V4 are not compatible in any way, almost nothing to do with each other. Either you use V3 or you use V4. A V3 client cannot work with a V4 server

    greetings Damien

    Thursday, October 2, 2014 3:08 PM
  • User-2145531484 posted

    Thanks Damien.

    I have followed your suggested article http://damienbod.wordpress.com/2014/07/09/web-api-odata-v4-using-an-odata-t4-generated-client-part-8/. Everything is looking good but I am getting few errors when I build the client application.

    Error in T4 .cs file:

    Error 2 The type or namespace name 'Edm' does not exist in the namespace 'Microsoft.OData' (are you missing an assembly reference?) 

    Code where an error is getting generated in T4 .cs file:

    private abstract class GeneratedEdmModel
    {
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "2.0.0")]
    private static global::Microsoft.OData.Edm.IEdmModel ParsedModel = LoadModelFromString();
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "2.0.0")]
    private const string Edmx = @"<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
    ...

    In my client references folder, I have Microsoft.OData.Client (v6.5.0.0) and Microsoft.OData.EntityFrameworkProvider (v 1.0.2.0). I don't have Microsoft.OData.Edm in my client references.

    What should I do?

    THanks,

    Thursday, October 2, 2014 5:20 PM
  • User1066278571 posted

    Here's a list of NuGet packages I have after installing from NuGet. If your missing one, install it.

    <packages>
      <package id="EntityFramework" version="6.1.1" targetFramework="net45" />
      <package id="Microsoft.OData.Client" version="6.6.0" targetFramework="net45" />
      <package id="Microsoft.OData.Core" version="6.6.0" targetFramework="net45" />
      <package id="Microsoft.OData.Edm" version="6.6.0" targetFramework="net45" />
      <package id="Microsoft.Spatial" version="6.6.0" targetFramework="net45" />
    </packages>

    You could compare your client to this one:

    https://github.com/damienbod/WebAPIODataV4SQLite/tree/master/WebAPIODataV4.Client

    It should also work with the latest packages, but I havn't tried that yet.

    hope this helps

    Greetings Damien

    Friday, October 3, 2014 12:37 AM
  • Friday, October 3, 2014 2:44 AM
  • User-2145531484 posted

    Hi Damien,

    I have tried to install all required packages but it keeps throwing following error:

    Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Client (≥ 5.2.2)'.
    Attempting to resolve dependency 'Newtonsoft.Json (≥ 6.0.4)'.
    Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Core (≥ 5.2.2 && < 5.3.0)'.
    Attempting to resolve dependency 'Microsoft.OData.Core (≥ 6.5.0 && < 7.0.0)'.
    Attempting to resolve dependency 'Microsoft.Spatial (= 6.5.0)'.
    Attempting to resolve dependency 'Microsoft.OData.Edm (= 6.5.0)'.
    Successfully installed 'Newtonsoft.Json 6.0.5'.
    Successfully installed 'Microsoft.AspNet.WebApi.Client 5.2.2'.
    Successfully installed 'Microsoft.AspNet.WebApi.Core 5.2.2'.
    Successfully installed 'Microsoft.Spatial 6.5.0'.
    Successfully installed 'Microsoft.OData.Edm 6.5.0'.
    Successfully installed 'Microsoft.OData.Core 6.5.0'.
    Successfully installed 'Microsoft.AspNet.OData 5.3.1-beta'.
    Successfully added 'Newtonsoft.Json 6.0.5' to MyCollegeWebApplication.
    Executing script file 'C:\Workspace\POC\MyCollegeWebApplication\packages\Newtonsoft.Json.6.0.5\tools\install.ps1'.
    Successfully added 'Microsoft.AspNet.WebApi.Client 5.2.2' to MyCollegeWebApplication.
    Successfully added 'Microsoft.AspNet.WebApi.Core 5.2.2' to MyCollegeWebApplication.
    Successfully uninstalled 'Microsoft.Spatial 6.5.0'.
    Successfully uninstalled 'Microsoft.AspNet.WebApi.Core 5.2.2'.
    Successfully uninstalled 'Microsoft.AspNet.WebApi.Client 5.2.2'.
    Successfully uninstalled 'Newtonsoft.Json 6.0.5'.
    Install failed. Rolling back...
    Could not install package 'Microsoft.Spatial 6.5.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references that are compatible with that framework. For more information, contact the package author.

    I have tried with .NETFramework, version=v4.5.1 also, but it is complaining the same. I am using VS 2012 to create my client web application. (I have created OData v4 end points service in VS 2013. ) Is VS 2012 creating the issue? 

    Also I would like to know how to find out which assembly references package contains?

    Thanks,

    Friday, October 3, 2014 11:17 AM
  • User-2145531484 posted

    Hi Damien,

    There was an update for Nuget Package Manager. I figured that out from VS 2012 -->Tools --> Extensions and Updates..--> Updates

    Now I am able to add those packages.  I will consume that service with client and let you know.

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 3, 2014 1:38 PM
  • User-2145531484 posted

    Hi Damien,

    I am using OData V3 Actions with web api 2.2. But somehow It's not working. Could you please look at the issue at ticket http://forums.asp.net/p/2013087/5791519.aspx?OData+V3+action+in+ASP+NET+web+api+doesn+t+get+trigger+ 

    Appreciate,

    Thanks

    Wednesday, October 15, 2014 9:48 AM