1
votes

So I have found various bits and pieces about SignalR v2, it appears to be VS2012/2013 (.net 4.5) only.

Can anyone supply a server/client example on how to self host in winforms/console/windows service using VS2010 (.net 4). Managed to get SignalR v1 + VS2010 + ASP.NET Chat application to work eventually with lower versions of client/server.

I see various articles with WebApp.Start(url) or WebApplication.Start(url) (Regarding hosting SignalR in console apps) But I either don't have the reference or something to get it to work with VS2010.

I am not sure if examples like; http://mscodingblog.blogspot.co.uk/2012/12/testing-signalr-in-wpf-console-and.html can be done in VS2010.

I have managed to convert SignalR Console app example into VB, eventually worked out that the version of the server needed to be 0.5.2 and Microsoft.AspNet.SignalR.Client 1.0.0-alpha2

I see SignalR v1 goes to 1.2.2, I assume I would need to match to a Server host/OWIN of similar nature, but can't find an example that works with VS2010.

Looked at SignalR support in .NET 4

http://mscodingblog.blogspot.co.uk/2012/12/testing-signalr-in-wpf-console-and.html

Thanks in advance.

1

1 Answers

2
votes

Eventually installed VS2013 CE to see examples.

Found some examples that worked with .net 4.0 using VS2013. e.g. http://mscodingblog.blogspot.co.uk/2012/12/testing-signalr-in-wpf-console-and.html

Copied the examples into projects in VS2010 .NET 4.0.

Added comments in code on versions etc., what worked or didn't.

SignalR Server example in VS2010 .NET 4.0

Imports Microsoft.AspNet.SignalR
Imports Microsoft.AspNet.SignalR.Hubs
Imports Microsoft.Owin.Hosting
Imports Owin

' Example SignalR Console Hosted Server application copied from VS2013 example, tested and working in VS2010 .NET 4.0

' 1) https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Core/1.2.2
' Install-Package Microsoft.AspNet.SignalR.Core -version 1.2.2 ' LATEST version Friday, August 29 2014 (.net 4.0)

' https://www.nuget.org/packages/Newtonsoft.Json/6.0.4
' (1) installed Newtonsoft.Json version 6.0.4 ' (Sunday, August 03 2014) (.net 4.0) '' not the latest version
'                           https://www.nuget.org/packages/Newtonsoft.Json/6.0.8
'       2013 example used ==> Install-Package Newtonsoft.Json -version 6.0.8 ' Sunday, January 11 2015 (.net 4.0) 
'                             tested in VS2010 seems to work as normal
'       test beta version
'                           https://www.nuget.org/packages/Newtonsoft.Json/7.0.1-beta1
'                             Install-Package Newtonsoft.Json -version 7.0.1-beta1 -pre ' Tuesday, February 17 2015 (.net 4.0)
'                             Tested in VS2010 seems to work as normal

' 2) https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Owin
' Install-Package Microsoft.AspNet.SignalR.Owin -version 1.2.2 ' LATEST version Friday, August 29 2014 (.net 4.0)

'   https://www.nuget.org/packages/Owin
'   (2) installed via Microsoft.AspNet.SignalR.Owin (Owin version 1.0)
'   manual install ==> Install-Package Owin -version 1.0 ' LATEST version Tuesday, November 13 2012 (.net 4.0)

' 3) https://www.nuget.org/packages/Microsoft.Owin/2.1.0
' Install-Package Microsoft.Owin -version 2.1.0 ' Tuesday, January 21 2014 (.net 4.0) '' not the latest 
'           https://www.nuget.org/packages/Microsoft.Owin/3.0.1 '' latest version 3.0.1 Friday, February 20 2015 untested (.net 4.5?) 
'                   Install-Package Microsoft.Owin -version 3.0.0-alpha1 fails to install due to .net 4.5?


' 4) https://www.nuget.org/packages/Microsoft.Owin.Host.HttpListener/2.1.0
' Install-Package Microsoft.Owin.Host.HttpListener -version 2.1.0 ' not the latest Tuesday, January 21 2014 (.net 4.0)
'           https://www.nuget.org/packages/Microsoft.Owin.Host.HttpListener '' latest version 3.0.1 Friday, February 20 2015 (.net 4.5?)
'                   Install-Package Microsoft.Owin.Host.HttpListener -version 3.0.1 ' fails to install .net 4.5?

' 5) https://www.nuget.org/packages/Microsoft.Owin.Hosting/2.1.0
' Install-Package Microsoft.Owin.Hosting -version 2.1.0 ' not the latest Tuesday, January 21 2014 (.net 4.0)
'           https://www.nuget.org/packages/Microsoft.Owin.Hosting '' latest version 3.0.1 Friday, February 20 2015 untested (.net 4.5?)
'                   Install-Package Microsoft.Owin.Hosting -version 3.0.1 ' fails to install .net 4.5?

Module Module1

    Sub Main()
        Dim url As String = "http://localhost:8080/"
        Using WebApp.Start(Of Startup)(url)
            Console.ForegroundColor = ConsoleColor.Green
            Console.WriteLine("Server running on {0}", url)
            Console.WriteLine("Press any key to start sending events to connected clients")
            Console.ReadLine()
            Dim context As IHubContext = GlobalHost.ConnectionManager.GetHubContext(Of MyHub)()
            For x As Integer = 0 To 100
                System.Threading.Thread.Sleep(3000)

                Console.WriteLine("Server Sending Value to Client X: " + x.ToString())
                context.Clients.All.addMessage(x.ToString())
            Next
            Console.ReadLine()
        End Using
    End Sub
    Public Class Startup
        Public Sub Configuration(ByVal app As IAppBuilder)
            Dim config = New HubConfiguration With {.EnableCrossDomain = True}
            app.MapHubs(config)
        End Sub
    End Class
    <HubName("myHub")> _
    Public Class MyHub
        Inherits Hub
        Public Sub Chatter(param As String)
            Console.WriteLine(param)
            Clients.All.addMessage(param)
        End Sub
    End Class
End Module

SignalR Client example in VS2010 .NET 4.0

Imports Microsoft.AspNet.SignalR.Client.Hubs
Imports Microsoft.AspNet.SignalR
Imports Microsoft.AspNet.SignalR.Client

' Example SignalR Client application copied from VS2013 example, tested and working in VS2010 .NET 4.0

' 1) https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Client/1.0.1
' Install-Package Microsoft.AspNet.SignalR.Client -version 1.0.1 ' not the latest version Thursday, February 28 2013 (.net 4.0)
' installed Install-Package Newtonsoft.Json -version 4.5.11
' ** example client with v1.0.1 works against v1.2.2 self host server .net 4.0 (console)

' 2) tested https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Client/1.2.2
' Install-Package Microsoft.AspNet.SignalR.Client -version 1.2.2 ' not the latest version Friday, August 29 2014 (.net 4.0)
' installed Install-Package Newtonsoft.Json -version 6.0.4
' ** example client with v1.2.2 works against v1.2.2 self host server .net 4.0 (console)

' fails on client/server versions mismatch
' 3) testing https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Client/2.0.0
' Install-Package Microsoft.AspNet.SignalR.Client -version 2.0.0 ' installed .net 4.0
' installed Install-Package Newtonsoft.Json -version 6.0.4
' ** issue with installation "hubconnection not defined" had to add Imports Microsoft.AspNet.SignalR.Client
' ** doesn't work with server self host v 1.2.2 (.net 4.0)


Module Module1

    Sub Main()
        Dim connection = New HubConnection("http://localhost:8080")

        Dim myHub = connection.CreateHubProxy("myHub")

        connection.Start().Wait()
        Console.ForegroundColor = ConsoleColor.Yellow
        myHub.Invoke(Of String)("chatter", "Hi!! Server") _
        .ContinueWith(
            Sub(task)
                If task.IsFaulted Then
                    Console.WriteLine("Could not Invoke the server method Chatter: {0}", _
                                      task.Exception.GetBaseException())
                Else
                    Console.WriteLine("Success calling chatter method")
                End If
            End Sub)

        myHub.On(Of String)("addMessage", _
            Sub(param)
                Console.WriteLine("Client receiving value from server: {0}", param.ToString())
            End Sub)
        Console.ReadLine()
    End Sub
End Module