1

I have a controller that I've built (extending from System.Web.Http.Odata.ODataController) and I think it's going to work fine - it's mostly based on pure scaffolding provided by my Visual Studio IDE.

The application will build, but, errors out during application start-up. When it goes to perform the initial route configuration, it fails on the last line of this WebApiConfig class in my App_Start folder - the call to config.MapODataServiceRoute throws an InvalidCastException because the value of builder.GetEdmModel() isn't understood:

Imports System.Web.Http
Imports System.Web.Http.Cors
Imports System.Web.Http.OData.Builder
Imports System.Web.OData.Extensions

Public Class WebApiConfig
    Public Shared Sub Register(ByVal config As HttpConfiguration)
    ' Web API configuration and services

    'enable cross orgin scripting 'CORS'
    Dim cors = New EnableCorsAttribute("*", "*", "*")
    config.EnableCors(cors)

    ' Web API routes
    config.MapHttpAttributeRoutes()

    config.Routes.MapHttpRoute(
        name:="DefaultApi",
        routeTemplate:="api/{controller}/{id}",
        defaults:=New With {.id = RouteParameter.Optional}
    )


    Dim builder As New ODataConventionModelBuilder
    builder.EntitySet(Of BusinessOrder)("BusinessOrder")
    config.MapODataServiceRoute("odata", Nothing, builder.GetEdmModel())
    End Sub
End Class

Here's the specific language of the InvalidCastException:

Unable to cast object of type 'Microsoft.Data.Edm.Library.EdmModel' to type 'Microsoft.OData.Edm.IEdmModel'.

Hmmm.. the docs for ODataConventionModelBuilder and MapODataServiceRoute seem to indicate I'm dealing with compatible types. If I read the error right, I'm getting back an instance of Microsoft.Data.Edm.Library.EdmModel from the call to GetEdmModel, which I would think ought to be conformant to the IEdmModel interface, per the docs.

Here's my web application's NuGet packages.config - hopefully this helps you understand the libraries being referenced by my web app:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net45" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net45" />
  <package id="jQuery" version="1.10.2" targetFramework="net45" />
  <package id="jQuery.UI.Combined" version="1.11.4" targetFramework="net45" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net45" />
  <package id="knockoutjs" version="2.3.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.OData" version="5.6.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Cors" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.OData" version="5.5.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net45" />
  <package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net45" />
  <package id="Microsoft.OData.Core" version="6.11.0" targetFramework="net45" />
  <package id="Microsoft.OData.Edm" version="6.11.0" targetFramework="net45" />
  <package id="Microsoft.Spatial" version="6.11.0" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Modernizr" version="2.6.2" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
  <package id="System.Spatial" version="5.6.0" targetFramework="net45" />
  <package id="WebGrease" version="1.5.2" targetFramework="net45" />
</packages>
4

2 回答 2

1

您正在混合 OData 的版本。命名空间 System.Web.Http.OData 用于 v3,命名空间 System.Web.OData 用于 v4。

Imports System.Web.Http.OData.Builder
Imports System.Web.OData.Extensions 

我不知道您要使用哪个版本,但它应该是一致的。尝试将两者都更改为 Http

Imports System.Web.Http.OData.Builder
Imports System.Web.Http.OData.Extensions 

或者两者都没有 Http

Imports System.Web.OData.Builder
Imports System.Web.OData.Extensions 
于 2015-07-02T12:13:00.640 回答
0

我决定通过运行以下命令来升级我的 NuGet 引用之一:Update-Package Microsoft.OData.Core(它将 6.11.0 的 3 个包升级到 6.12.0)

于 2015-10-07T17:25:36.627 回答