【问题标题】:SignalR - only works with localhostSignalR - 仅适用于本地主机
【发布时间】:2014-09-04 19:49:56
【问题描述】:

所以,我有一个 SignalR 自托管控制台应用程序和一个在 WAMP 上运行的网站。当我使用localhost 作为域名时,一切正常。显然这只适用于本地。

我希望它也可以在其他计算机上运行。所以我尝试在 c# 控制台应用程序和网站上将localhost 更改为我的本地 ip。当我有本地 IP 时,控制台应用程序崩溃,并出现错误:

未处理的类型异常 在 mscorlib.dll 中发生“System.Reflection.TargetInvocationException”

我还尝试在控制台应用程序中使用* 而不是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>

【问题讨论】:

  • 你能从浏览器调试中运行吗?我想知道它在哪里失败,调用 signalR 或其他什么?根据当前错误,它看起来像 .NET 库调用错误。
  • TargetInvocationException 应该有一个非空的 InnerException ,它应该告诉您有关您所看到的错误的更多详细信息。发布完整的异常细节,否则它可以是任何东西。
  • 喜欢这个? pastebin.com/c9KnKzR7

标签: c# javascript signalr


【解决方案1】:

以管理员身份运行程序解决了。

【讨论】:

  • 在这里查看答案:stackoverflow.com/a/22972905/2381899 这个不需要以管理员身份运行。
  • 这解决了我的问题!我一直在寻找有关 SignalR 为什么在本地为我工作而不是远程工作的几个小时。感谢您提供简单的解决方法!
猜你喜欢
  • 1970-01-01
  • 2018-11-16
  • 2017-02-25
  • 2023-04-05
  • 2023-03-31
  • 1970-01-01
  • 2011-03-30
  • 2012-10-08
  • 1970-01-01
相关资源
最近更新 更多