2014-09-04 129 views
0

因此,我有一個SignalR自行託管的控制檯應用程序和一個運行在WAMP上的網站。當我使用localhost作爲域名時,一切正常。顯然這隻適用於本地。SignalR - 僅適用於localhost

我也想讓它在其他電腦上工作。所以我試圖將localhost更改爲本地ip,無論是在c#console應用程序中,還是在網站上。控制檯應用程序崩潰時,我有我的本地IP,出現錯誤:

的類型 「System.Reflection.TargetInvocationException」未處理的異常出現在mscorlib.dll

我也曾嘗試在控制檯應用程序中使用*而不是localhost。像這樣:

string url = "http://*:8080/servers/2/"; 

同樣在那裏,它崩潰了。我究竟做錯了什麼?

控制檯應用程序代碼:

namespace SignalRSelfHost 
{   
    class Program 
    {  
     public static IPAddress ipAd { get; set; } 
     public static TcpListener myList { get; set; } 
     static void Main(string[] args) 
     {  
      // This will *ONLY* bind to localhost, if you want to bind to all addresses 
      // use http://*:8080 to bind to all addresses. 
      // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx 
      // for more information. 

      string url = "http://localhost:8080/servers/2/"; 
      using (WebApp.Start(url)) 
      { 
       Console.WriteLine("Server running on {0}", url); 
       Console.ReadLine(); 
      }     
     }    
    } 
    class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.Map("/signalr", map => 
      { 
       // Setup the CORS middleware to run before SignalR. 
       // By default this will allow all origins. You can 
       // configure the set of origins and/or http verbs by 
       // providing a cors options with a different policy. 
       map.UseCors(CorsOptions.AllowAll); 
       var hubConfiguration = new HubConfiguration 
       { 
        // You can enable JSONP by uncommenting line below. 
        // JSONP requests are insecure but some older browsers (and some 
        // versions of IE) require JSONP to work cross domain 
        // EnableJSONP = true 

       }; 
       // Run the SignalR pipeline. We're not using MapSignalR 
       // since this branch already runs under the "/signalr" 
       // path. 
       map.RunSignalR(hubConfiguration); 
      }); 
     } 
    } 
    public class MyHub : Hub 
    { 
     public void Send(string message) 
     { 
      Clients.All.addMessage(message); 

     } 
    } 
} 

我的網站的代碼:

<input type="text" id="message" /> 
     <input type="button" id="sendmessage" value="Send" /> 
     <input type="hidden" id="displayname" /> 
    <ul id="discussion"></ul> 

<!--Script references. --> 
    <!--Reference the jQuery library. --> 
    <script src="/assets/js/jquery-1.6.4.min.js"></script> 
    <!--Reference the SignalR library. --> 
    <script src="/assets/js/jquery.signalR-2.0.3.min.js"></script> 
    <!--Reference the autogenerated SignalR hub script. --> 
    <script src="http://localhost:8080/servers/<?php echo $server->row()->id; ?>/signalr/hubs"></script> 
    <!--Add script to update the page and send messages.--> 
    <script type="text/javascript"> 
     $(function() { 
      var url = "http://localhost:8080/servers/<?php echo $server->row()->id; ?>/signalr"; 
      //Set the hubs URL for the connection 
      $.connection.hub.url = url; 

      // Declare a proxy to reference the hub. 
      var chat = $.connection.myHub; 

      // Create a function that the hub can call to broadcast messages. 
      chat.client.addMessage = function (message) { 
       // Html encode display name and message. 
       var encodedMsg = $('<div />').text(message).html(); 
       // Add the message to the page. 
       $('#discussion').append('<li>' + encodedMsg + '</li>'); 
      }; 
      // Get the user name and store it to prepend to messages. 
      //$('#displayname').val(prompt('Enter your name:', '')); 
      // Set initial focus to message input box. 
      //$('#message').focus(); 
      // Start the connection. 
      $.connection.hub.start().done(function() { 
       $('#sendmessage').click(function() { 
        // Call the Send method on the hub. 
        chat.server.send($('#message').val()); 
        // Clear text box and reset focus for next comment. 
        $('#message').val('').focus(); 
       }); 
      }); 
     }); 
    </script> 
+0

你能從瀏覽器調試運行嗎?我想知道它在哪裏失敗,調用signalR或其他東西?根據當前錯誤,它看起來像.NET庫調用錯誤。 – 2014-09-04 20:36:19

+0

'TargetInvocationException'應該有一個非空的InnerException,它應該告訴你關於你所看到的錯誤的更多細節。發佈完整的例外細節,否則它可以是任何東西。 – Pawel 2014-09-04 21:10:37

+0

是否這樣? http://pastebin.com/c9KnKzR7 – Dexception 2014-09-04 21:27:01

回答

7

通過運行程序作爲管理員解決它。

+0

對我有幫助。非常感謝。 :) – 2015-12-14 14:26:00

+0

在這裏看到答案:http://stackoverflow.com/a/22972905/2381899這不需要以管理員身份運行。 – Guy 2016-06-14 09:40:49