signalR自主机与控制台/ Windows服务示例VS2010

时间:2015-03-01 20:33:40

标签: vb.net visual-studio-2010 signalr owin self-hosting

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

任何人都可以使用VS2010(.net 4)提供有关如何在winforms / console / windows服务中自托管的服务器/客户端示例。管理使SignalR v1 + VS2010 + ASP.NET Chat应用程序最终能够与较低版本的客户端/服务器一起工作。

我看到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,我假设我需要匹配类似性质的服务器主机/ OWIN,但找不到适用于VS2010的示例。

看着SignalR support in .NET 4

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

提前致谢。

1 个答案:

答案 0 :(得分:2)

最终安装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 Server示例

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