Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Error when using $expand using oData api controller

$
0
0

Hello,

I'm experimenting with an oData endpoint in combination with entity framework. I have created the following controller class:

Imports System.Net
Imports System.Web.Http
Imports System.Linq
Imports System.Web.Http.OData.Query
Imports System.Web.Http.Results

Public Class DocumentVersionsController
    Inherits ApiController

    ' GET api/<controller><Queryable> _
    Public Function GetValues() As IEnumerable(Of iDocument.iDocumentEntities.DocumentVersion)
        Dim objEntities As New iDocument.iDocumentEntities.iProva_iDocument_1Entities()
        objEntities.Configuration.LazyLoadingEnabled = False
        Return objEntities.DocumentVersions
    End Function

    ' GET api/<controller>/5
    Public Function GetValue(ByVal id As Integer) As iDocument.iDocumentEntities.DocumentVersion
        Dim objEntities As New iDocument.iDocumentEntities.iProva_iDocument_1Entities()
        objEntities.Configuration.ProxyCreationEnabled = False
        Return (From objDocumentVersion In objEntities.DocumentVersions Where objDocumentVersion.DocumentVersionID = id).FirstOrDefault()
    End Function

    ' POST api/<controller>
    Public Sub PostValue(<FromBody()> ByVal value As String)

    End Sub

    ' PUT api/<controller>/5
    Public Sub PutValue(ByVal id As Integer, <FromBody()> ByVal value As String)

    End Sub

    ' DELETE api/<controller>/5
    Public Sub DeleteValue(ByVal id As Integer)

    End Sub
End Class

The first issue I ran into was the following error when accessing the endpoint:

Type 'System.Data.Entity.DynamicProxies.DocumentVersion_AF66BC349CC56D7820B5082D52DAB765BFE75FD64E0B7359D2C953CD484EC675' with data contract name 'DocumentVersion_AF66BC349CC56D7820B5082D52DAB765BFE75FD64E0B7359D2C953CD484EC675:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

It turns out to fix this, i need to add a row of code to the GetValues function:

objEntities.Configuration.ProxyCreationEnabled = False

This fixes the issue and the document versions are exposed.

But when i try to expand a data type by calling: http://<myroot>/api/documentversions?$expand=Owner

i get the following error:

Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[System.Web.Http.OData.Query.Expressions.SelectExpandBinder+SelectAllAndExpand`1[Infoland.Suite.iDocument.iDocumentEntities.DocumentVersion]]' to type 'System.Collections.Generic.IEnumerable`1[Infoland.Suite.iDocument.iDocumentEntities.DocumentVersion]'.

Now i have two questions. What is the reason I need to disable proxy creation, and why am I getting the error when i expand the Owner field? When I expand the owner field by using the "Include" function server side, no error occurs.

If any information is missing to give an proper answer please let me know. I'm quite new to these web api's and I'm trying to understand what is happening.


Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>