System.Net.Http.HttpRequestException was caught +

2019-06-20 17:47发布

问题:

Please help me I keep getting a System.Net.Http.HttpRequestException when I try to make a hubConnection. I have gone through many tutorials and downloaded example codes and nothing seems to work. I have also tried searching online for ways to configure my computer but it gets rather confusing too quickly. Any suggestion will be welcome.

InnerException : No connection could be made because the target machine actively refused it

Note: on localhost every thing is work fine

My asp.net c# server application running on IIS .its running on Live server. can access any where.

Follow Code of asp.net C# Server Side Application

Index.aspx Code is here :

 <html xmlns="http://www.w3.org/1999/xhtml">
<head >
   <title>SignalR Echo</title>
    <script src="Scripts/jquery-1.6.4.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery.signalR-2.1.2.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery.signalR-2.1.2.js"></script>
   <script src='<%: ResolveClientUrl("~/myhubs/hubs") %>'></script>
</head>
<body>
    <form id="form1" runat="server">
    <script type="text/javascript">
        function htmlEncode(value) {
            return $("<div/>").text(value).html();
        }

        function addMsg(msg) {
            $("#messages").append("<li>" + htmlEncode(msg) + "</li>");
        }

        $(function () {
            var chatHubProxy = $.connection.myChatHub;
            chatHubProxy.client.appendNewMessage = function (clientName, message) {
                addMsg(clientName + ": " + message);
            };

            // Start the hub connection
            addMsg("Connecting Hub...");
            $.connection.hub.url = "/myhubs"

            $.connection.hub.logging = true;
            $.connection.hub.start().done(function () {
                addMsg("Server is  running now.");
                $("#send").click(function () {
                    chatHubProxy.server.broadcastMessage("Server: ", $("#msg").val());
                });

            }).fail(function () {
                addMsg("Server is not running.");
            });
        });
    </script>
    <table>    
        <tr>
            <td><span>Message:</span></td>
            <td>
                <input type="text" id="msg" />
            </td>
            <td>
                <input type="button" value="Send" id="send" />
            </td>
            <td> &nbsp;</td>
            <td />
        </tr>
    </table>
    <ul id="messages"></ul>
    </form>
</body>
</html>

Mychathub.cs Code is here :

using Microsoft.AspNet.SignalR;
using System;

namespace SignalRHubServer
{
    /// <summary>
    /// The client must use camel-cased names to RPC this Hub and its methods.
    /// JS Example: 
    ///   var chatHub = $.connection.myChatHub;
    ///   chatHub.server.broadcastMessage("dzy", "Hello all!");
    /// </summary>
    public class MyChatHub : Hub
    {
        public async Task BroadcastMessage(string callerName, string message)
        {
            // Case-insensitive when the server RPC the client's methods
            await Clients.All.appendnewmessage(callerName, message);
        }


    }
}

startup.cs code is here :

using Microsoft.Owin;
using Owin;
using Microsoft.AspNet.SignalR;

[assembly: OwinStartup(typeof(SignalRHubServer.Startup))]
namespace SignalRHubServer
{

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR("/myhubs", new HubConfiguration());
            //app.MapSignalR();
        }
    }
}

My Client side i am using Console Application and this console application i am running on another PC .

here is my Client.cs Code :

using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SignalRHubClient
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string inputLine;
                var hubConn = new HubConnection("http://xxx.xxx.xxx.11:8080/myhubs");
                var chatHubProxy = hubConn.CreateHubProxy("myChatHub");
                chatHubProxy.On("appendNewMessage", delegate(string name, string message)
                {
                    Console.WriteLine("{0}: {1}", name, message);
                });

                hubConn.Start().Wait();
                Console.WriteLine("Success! Connected with client connection id {0}", hubConn.ConnectionId);
                if (hubConn.ConnectionId != null)
                {
                    string abd = "Tariq";
                    while (!string.IsNullOrEmpty(inputLine = Console.ReadLine()))
                    {
                        chatHubProxy.Invoke("broadcastMessage", abd, inputLine).Wait();

                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString(), "ERROR");
                Console.ReadLine();
            }
            Console.ReadLine();
        }
    }
}

I'm getting exception in Client side Console application . when bellow method is called. Note: need break point on catch line.

hubConn.Start().Wait();