带有 console/windows 服务示例的 signalR 自托管 VS2010

signalR self-host with console/windows service examples VS2010

所以我找到了关于 SignalR v2 的各种零碎信息,它似乎只是 VS2012/2013 (.net 4.5)。

任何人都可以提供一个 server/client 示例,说明如何使用 VS2010 (.net 4) 在 winforms/console/windows 服务中自行托管。设法让 SignalR v1 + VS2010 + ASP.NET 聊天应用程序最终与较低版本的 client/server.

一起工作

我看到各种文章 WebApp.Start(url) 或 WebApplication.Start(url) (Regarding hosting SignalR in console apps) 但是我要么没有参考资料,要么没有什么东西可以让它与 VS2010 一起工作。

我不确定例子是否像; http://mscodingblog.blogspot.co.uk/2012/12/testing-signalr-in-wpf-console-and.html 可以在VS2010中完成。

我设法将SignalR Console app example转换成VB,最终得出服务器的版本需要是0.5.2和Microsoft.AspNet.SignalR.Client 1.0.0-alpha2

我看到 SignalR v1 升级到 1.2.2,我假设我需要匹配具有类似性质的服务器 host/OWIN,但找不到适用于 VS2010 的示例。

看过SignalR support in .NET 4

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

提前致谢。

最后安装VS2013 CE看例子

找到一些使用 VS2013 与 .net 4.0 一起工作的示例。 例如http://mscodingblog.blogspot.co.uk/2012/12/testing-signalr-in-wpf-console-and.html

将示例复制到 VS2010 .NET 4.0 中的项目中。

在代码中添加了关于版本等的注释,哪些有效或无效。

VS2010 .NET 4.0 中的 SignalR 服务器示例

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

VS2010 .NET 4.0 中的 SignalR 客户端示例

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