Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Kotlin the data class and the classAnother thing you should never do in VB.Net »
    comments

    In my previous post I showed you how to get an object from a call to a SignalR method.

    That call on the client side returns a Json.Net JObject. This is dynamic object.

    However, the implementation of this object is not very VB.Net friendly.

    If you try this.

    1. Imports System.Runtime.CompilerServices
    2. Imports Newtonsoft.Json
    3. Imports Newtonsoft.Json.Linq
    4. Imports Microsoft.AspNet.SignalR.Client.Hubs
    5.  
    6. Module Module1
    7.  
    8.     Sub Main()
    9.         Dim connection = New HubConnection("http://localhost:50865")
    10.  
    11.         Dim plants = connection.CreateHubProxy("Plants")
    12.  
    13.         plants.On(Of String)("addMessage", Sub(data) Console.WriteLine(data.ToString()))
    14.  
    15.         connection.Start().Wait()
    16.  
    17.         Dim line As String = Nothing
    18.         Console.WriteLine("type the id + enter to send, type exit + enter to exit.")
    19.         While line <> "exit"
    20.             line = Console.ReadLine()
    21.             plants.Invoke(Of Object)("GetPlant", line).ContinueWith(Sub(data)
    22.                                                                         If data.Result IsNot Nothing Then
    23.                                                                             Console.WriteLine(data.Result.ToString)
    24.                                                                             Dim result = data.Result
    25.                                                                             Console.WriteLine(result.Id)
    26.                                                                             Console.WriteLine(result.Genus)
    27.                                                                             Console.WriteLine(result.Species)
    28.                                                                             For Each commonName In result.CommonNames
    29.                                                                                 Console.WriteLine(commonName)
    30.                                                                             Next
    31.                                                                         Else
    32.                                                                             Console.WriteLine("No plant with that id.")
    33.                                                                         End If
    34.                                                                     End Sub)
    35.         End While
    36.     End Sub
    37.  
    38. End Module

    You will get an exception on result.Id

    System.MissingMemberException was unhandled by user code
    HResult=-2146233070
    Message=Public member 'Id' on type 'JObject' not found.
    Source=Microsoft.VisualBasic
    StackTrace:
    at Microsoft.VisualBasic.CompilerServices.Symbols.Container.GetMembers(String& MemberName, Boolean ReportErrors)
    at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
    at Microsoft.VisualBasic.CompilerServices.NewLateBinding.FallbackGet(Object Instance, String MemberName, Object[] Arguments, String[] ArgumentNames)
    at CallSite.Target(Closure , CallSite , Object )
    at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
    at Invoker(CallSiteBinder , Object , Object[] )
    at Microsoft.VisualBasic.CompilerServices.IDOUtils.CreateRefCallSiteAndInvoke(CallSiteBinder Action, Object Instance, Object[] Arguments)
    at Microsoft.VisualBasic.CompilerServices.IDOBinder.IDOGet(IDynamicMetaObjectProvider Instance, String MemberName, Object[] Arguments, String[] ArgumentNames, Boolean[] CopyBack)
    at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
    at SignalRConsole.Module1._Lambda$__2(Task`1 data) in E:\Users\christiaan\Documents\Visual Studio 2012\Projects\SignalRTesting\SignalRConsole\Module1.vb:line 25
    at System.Threading.Tasks.ContinuationTaskFromResultTask`1.InnerInvoke()
    at System.Threading.Tasks.Task.Execute()
    InnerException:

    The problem is the LateGet. But you can found out all about why this is a problem for VB.Net in the following post by Matthew Doig.

    And none of the Json.net converters can solve this for us.

    But we can use another library that does work with VB.Net.

    And it's called JSonFX. Just add it to your project via Nuget and then we can change our code to this.

    1. Imports System.Runtime.CompilerServices
    2. Imports Newtonsoft.Json
    3. Imports Newtonsoft.Json.Linq
    4. Imports Microsoft.AspNet.SignalR.Client.Hubs
    5.  
    6. Module Module1
    7.  
    8.     Sub Main()
    9.         Dim connection = New HubConnection("http://localhost:50865")
    10.  
    11.         Dim plants = connection.CreateHubProxy("Plants")
    12.  
    13.         plants.On(Of String)("addMessage", Sub(data) Console.WriteLine(data.ToString()))
    14.  
    15.         connection.Start().Wait()
    16.  
    17.         Dim line As String = Nothing
    18.         Console.WriteLine("type the id + enter to send, type exit + enter to exit.")
    19.         While line <> "exit"
    20.             line = Console.ReadLine()
    21.             plants.Invoke(Of Object)("GetPlant", line).ContinueWith(Sub(data)
    22.                                                                         If data.Result IsNot Nothing Then
    23.                                                                             Console.WriteLine(data.Result.ToString)
    24.                                                                             Dim reader = New JsonFx.Json.JsonReader()
    25.                                                                             Dim result = reader.Read(data.Result.ToString)
    26.                                                                             Console.WriteLine(result.Id)
    27.                                                                             Console.WriteLine(result.Genus)
    28.                                                                             Console.WriteLine(result.Species)
    29.                                                                             For Each commonName In result.CommonNames
    30.                                                                                 Console.WriteLine(commonName)
    31.                                                                             Next
    32.                                                                         Else
    33.                                                                             Console.WriteLine("No plant with that id.")
    34.                                                                         End If
    35.                                                                     End Sub)
    36.         End While
    37.     End Sub
    38.  
    39. End Module

    Now our result will be.

    type the id + enter to send, type exit + enter to exit.
    1
    {
    "Id": 1,
    "Genus": "Fagus",
    "Species": "Sylvatica",
    "CommonNames": [
    "Common Beech"
    ]
    }
    Someone requested id: 1
    1
    Fagus
    Sylvatica
    Common Beech

    Which is ultra cool.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)