带有控制台/Windows服务示例VS2010的signalR自主机
Posted
技术标签:
【中文标题】带有控制台/Windows服务示例VS2010的signalR自主机【英文标题】:signalR self-host with console/windows service examples VS2010 【发布时间】:2015-05-02 03:37:55 【问题描述】:所以我找到了关于 SignalR v2 的各种点点滴滴,它似乎只是 VS2012/2013 (.net 4.5)。
任何人都可以提供有关如何使用 VS2010 (.net 4) 在 winforms/console/windows 服务中自我托管的服务器/客户端示例。设法让 SignalR v1 + VS2010 + ASP.NET 聊天应用程序最终与较低版本的客户端/服务器一起工作。
我看到各种带有 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】:最终装了VS2013 CE看例子。
找到了一些使用 VS2013 与 .net 4.0 配合使用的示例。 例如http://mscodingblog.blogspot.co.uk/2012/12/testing-signalr-in-wpf-console-and.html
将示例复制到 VS2010 .NET 4.0 中的项目中。
在版本等的代码中添加了 cmets,哪些有效或无效。
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
【讨论】:
以上是关于带有控制台/Windows服务示例VS2010的signalR自主机的主要内容,如果未能解决你的问题,请参考以下文章
在 Windows VS2010 上构建 NaoQI C++ SDK 示例项目 helloworld 时出错