从 Windows 服务访问 Web 应用程序中的 SignalR Hub

Accessing SignalR Hub in Web App from Windows Service

我已经创建了一个 SignalR 集线器并让它在我的网络应用程序中运行。现在我正在尝试获得一个单独的 Windows 服务来向该中心发送消息。根据我用于集线器连接 URL 的内容,我得到 401 Unauthorized 或 SocketException。我缺少什么才能让 Windows 服务能够向集线器发送消息?

在网络应用程序中启动 class:

[assembly: OwinStartup(typeof(MmaWebClient.Startup))]
namespace MmaWebClient
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

网络应用程序中的中心 class:

[HubName("scannerHub")]
public class ScannerHub : Hub
{
    public void Send(string message)
    {
        Clients.All.broadcastMessage(message);
    }
}

index.html 中的脚本引用:

<script src="../scripts/jquery-1.9.1.js"></script>
<script src="../scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="../signalr/hubs"></script>

JavaScript(有效)在我的 AngularJS 控制器中:

    if (!$scope.state.hub) {
        $scope.state.hub = $.connection.scannerHub;
        $scope.state.hub.client.broadcastMessage = function (message) {
            onSubIdOrCassetteIdEntered(scanText);
        }
        $.connection.hub.start()
            .done(function () {
                console.log('Connected to SignalR. Connection ID: ' + $.connection.scannerHub.connection.id +
                    '. URL: ' + $.connection.scannerHub.connection.baseUrl +$.connection.scannerHub.connection.appRelativeUrl);
            })
            .fail(function (response) {
                showInfoModal.show('Connection to SignalR Failed', 'This connection is necessary to read the ' +
                    'scans from the scanner. Response message: ' + response.message);
            });
    }

最后,在 Windows 服务中:

    static void Main(string[] args)
    {
        AutoResetEvent scanReceivedEvent = new AutoResetEvent(false);

        var connection = new HubConnection("http://localhost/MmaWebClient/signalr");
        //Make proxy to hub based on hub name on server
        var myHub = connection.CreateHubProxy("scannerHub");
        //Start connection

        connection.Start().ContinueWith(task => {
            if (task.IsFaulted) {
                Console.WriteLine("There was an error opening the connection:{0}",
                                  task.Exception.GetBaseException());
            } else {
                Console.WriteLine("Connected");
            }

        }).Wait();


        myHub.On<string>("broadcastMessage", param => {
            Console.WriteLine("Scan received from server = [{0}]", param);
            scanReceivedEvent.Set();
        });

        string input = "";

        do
        {
            Console.WriteLine("Enter a value to send to hub or Q to quit.");

            input = Console.ReadLine();

            if (input.ToUpperInvariant() != "Q")
            {
                myHub.Invoke<string>("Send", input);
                scanReceivedEvent.WaitOne(1000);
            }

        } while (input.ToUpperInvariant() != "Q");

        connection.Stop();
    }

当我在上面的代码中创建新的集线器连接时,我已经尝试了这三个 URL 没有成功:

var connection = new HubConnection("http://localhost/MmaWebClient/signalr");
var connection = new HubConnection("http://localhost:8080");
var connection = new HubConnection("http://localhost");

第一个URL:401未经授权
第二个 URL:套接字异常
第三个 URL: 404 未找到

不是确定的答案,但这是映射 SignalR 端点的替代方法。这个很难URL错误。

ASP.NET Startup.cs

public void Configuration(IAppBuilder app)
{    
    app.Map("/foo", map =>
    {
        //Specify according to your needs
        var hubConfiguration = new HubConfiguration
        {
           EnableDetailedErrors = true
        };
        map.RunSignalR(hubConfiguration);
    });

    ConfigureAuth(app);
}

客户端

var hubConnection = new HubConnection("http://localhost/foo", useDefaultUrl: false);
var proxy = hubConnection.CreateHubProxy("scannerHub");

我能够通过在连接上设置凭据(下面的第二行)来让它工作:

var connection = new HubConnection("http://localhost/MmaWebClient");
connection.Credentials = CredentialCache.DefaultNetworkCredentials;
var myHub = connection.CreateHubProxy("scannerHub");