2016-06-22 183 views
0

我創建了一個SignalR集線器,並使其在我的Web應用程序中工作。現在我試圖讓一個單獨的Windows服務向該集線器發送消息。根據我用於集線器連接URL的內容,我得到401未授權或SocketException。我錯過了什麼讓Windows服務能夠發送消息到集線器?從Windows服務訪問Web應用程序中的SignalR Hub

啓動類網絡應用:

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

集線器類網絡應用:index.html中

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

腳本引用:

<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 Unauthorized
第二個網址:套接字例外
第三個網址:404未找到

+0

你嘗試過'http:// localho st/MmaWebClient'?對於C#SignalR客戶端,我使用的是沒有signalr/hubs部分的url,它工作正常。 – Wojtek

+0

當我這樣做時,我得到401未授權。 –

回答

2

我能夠得到它通過設置連接上的憑據(第二行,下同)工作:

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

不是一個確定的答案,但是這裏有一個映射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"); 
+0

我試過了,得到了404 Not Found。我不得不評論ConfigureAuth線路,因爲它不適用於我。 –

+0

您是否使用自定義端口運行網頁? – supertopi

相關問題