Off The Gold Off The Gold - 2 months ago 42
Vb.net Question

Unable to cast object of type System.Collections.Generic.IEnumerable - Web API issue

So I am trying to create a simple web API 2 with VB in VS2015. However the code I did get working only returns a simple string, where the Web Api expects serialized data so XML or JSON could be returned.

'Here I initialize and populate my object
Public widgetObj As New widgetModelClass("bottle", 1)


' Below kind of works, but simply returns a string **instead** of
' the serialized data for XML or JSON the Web API should return.
' it returns: {"Name":"bottle","ID":1}
Public Function GetValues() As widgetModelClass
Return widgetObj
End Function


The below code fails with the exception:


Unable to cast object of type System.Collections.Generic.IEnumerable


Public Function GetValues() As IEnumerable(Of widgetModelClass)
Return widgetObj
End Function


I have defined my model class as follows

Public Class widgetModelClass
Public Sub New(ByVal name As String, ByVal id As Integer)
Me.Name = name
Me.ID = id
End Sub

Private m_Name As String
Public Property Name() As String
Get
Return m_Name
End Get
Set
m_Name = Value
End Set
End Property

Private m_ID As Integer
Public Property ID() As Integer
Get
Return m_ID
End Get
Set
m_ID = Value
End Set
End Property
End Class


The full error message:


An error has
occurred.Unable to cast object of type
'TestApp.widgetModelClass' to type
'System.Collections.Generic.IEnumerable
1[TestApp.widgetModelClass]'.</ExceptionMessage><ExceptionType>System.InvalidCastException</ExceptionType><StackTrace>
at lambda_method(Closure , Object , Object[] ) at
System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object
instance, Object[] methodParameters) at
System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object
instance, Object[] arguments) at
System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext
controllerContext, IDictionary
2 arguments, CancellationToken
cancellationToken)
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

Answer

In your sample you return widgetObj but it is not of an instanse of the IEnumerable(Of widgetModelClass) as you want.

You have to change declaration to return one instanse of the widgetModelClass:

Public Function GetValues() As widgetModelClass
    Return widgetObj
End Function

or your return statement to return IEnumerable(Of widgetModelClass):

Public Function GetValues() As IEnumerable(Of widgetModelClass)
    Return New List(Of widgetModelClass)(New widgetModelClass() { widgetObj } )
End Function
Comments