Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix No connection could be made because the target machine actively refused it 127.0.0.1:64527

I have an MVC application which depends on a web API application, I hosted the two on a shared hosting environment. API on the subdomain and MVC on the main domain the API is api.mydomain.com and the MVC is mydomain.com, the API works fine anytime I try it on postman or browser but the MVC cannot connect to it with the following error.

No connection could be made because the target machine actively refused it 127.0.0.1:64527 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:64527

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SocketException (0x274d): No connection could be made because the target machine actively refused it127.0.0.1:64527]
System.Net.Sockets.Socket.InternalEndConnect(IAsyncResult asyncResult) +6995036
System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) +84
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +256

[WebException: Unable to connect to the remote server]
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) +606 System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) +64

[HttpRequestException: An error occurred while sending the request.]
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() +28 SMMClient.<Setting>d__1191.MoveNext() in C:\Users\Dload\documents\visual studio 2017\Projects\SMM\SMMClient\Proc.cs:1369
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() +28 SIMSClient.Controllers.<Index>d__0.MoveNext() in C:\Users\Dload\documents\visual studio 2017\Projects\SMM\SMMClient\Controllers\HomeController.cs:19
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +97
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +228 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +48
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +48
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +38 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +48
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +28
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar) +152 System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +125

This is how I connect to the API from Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        Proc.Configure("https://api.mydomain.com/");
    }
}

This is how I call a resource

public static class Proc
{
    private static HttpClient _client;
    public static void Configure(string Baseurl)
    {
        _client = new HttpClient();
        _client.BaseAddress = new Uri(Baseurl);

        _client.DefaultRequestHeaders.Clear();
        //Define request data format  
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    }

    public static async Task<T> Settings<T>()
    {
        if (!string.IsNullOrWhiteSpace(Account.AccessToken))
            _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Account.AccessToken);

        var resp = await _client.GetAsync($"api/superadmin/settings");

        var txt = await resp.Content.ReadAsStringAsync();

        return JsonConvert.DeserializeObject<T>(txt);
    }
}

I'm able to connect to the live API from my local MVC application but I can not connect to the API from live MVC application.

I will be glad to get some help.

like image 492
banji Avatar asked Apr 09 '19 20:04

banji


People also ask

How do you fix no connection could be made because the target machine actively refused it?

You can try below solution: You might have a firewall rule in the way, or are trying to run it through a proxy without having the proxy up and running. The easiest way to check would be to disable your firewall or proxy and try again. Disable proxy at web.

How do you fix no connection could be made because the target machine actively refused it 10061?

1-Disable WMI services : run - services. msc - Windows Management Instrumentation(WMI) - stop the service. 2-Delete the files under C:\Windows\System32\wbem\Repository 3-Open regedit: Go to HKEY_LOCAL_MACHINE > Software and HKEY_CURRENT_USER > Software. Delete the Palo Alto Networks folder.

How do you solve no connection could be made because the target machine actively refused it in PHP?

means that no error in your code , but either you have firewall which blocks your connection or your sistem is listening in different PORT. to do: 1-verify your connecting port default is 3306. 2-try connect with use "127.0. 0.1" instead of "localhost" this maybe it listening on "127.0.

Could not connect to Redis at 127.0 0.1 6379 no connection could be made because the target machine actively refused it?

Firewall restriction is another common reason that can trigger the “could not connect to Redis connection refused”. By default Redis server listen to the TCP port 6379. If another application is using the port or if the firewall restrictions blocks the port, it can trigger the connection refused error.


1 Answers

This error occur when your firewall blocking the connection or process that is hosting the service is not listening on that port. Refer these links:

http://forums.asp.net/t/1223846.aspx/1

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

https://www.codeproject.com/Questions/426966/System-Net-Sockets-SocketExcep

like image 153
Muhammad Aqib Avatar answered Oct 15 '22 05:10

Muhammad Aqib