我有一个我已经构建的控制器(从System.Web.Http.Odata.ODataController扩展),我认为它可以正常工作 - 它主要基于我的Visual Studio IDE提供的纯脚手架。
应用程序将构建,但是,在应用程序启动期间出错。当它执行初始路由配置时,它在我的App_Start文件夹中此WebApiConfig类的最后一行失败 - 对config.MapODataServiceRoute
的调用抛出InvalidCastException,因为builder.GetEdmModel()
的值未被理解:
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
这是InvalidCastException的特定语言:
Unable to cast object of type 'Microsoft.Data.Edm.Library.EdmModel' to type 'Microsoft.OData.Edm.IEdmModel'.
嗯.. ODataConventionModelBuilder和MapODataServiceRoute的文档似乎表明我正在处理兼容类型。如果我正确读出错误,我将从调用GetEdmModel获取Microsoft.Data.Edm.Library.EdmModel
的实例,根据文档,我认为应该符合IEdmModel接口。
这是我的网络应用程序的NuGet packages.config
- 希望这有助于您了解我的网络应用程序引用的库:
<?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>
答案 0 :(得分: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
答案 1 :(得分:0)
我决定通过运行以下命令来升级我的一个NuGet引用:Update-Package Microsoft.OData.Core(将3个软件包放在6.11.0到6.12.0)