0

I am using Nethereum to call a function called owner() in a BSC contract, which should return the contract owner's address, however when I use the CallAsync() method, I get an exception "No connection could be made because the target machine actively refused it"

My code (simplified):

using (var s = webRequest.GetResponse().GetResponseStream())
{
    using (var sr = new StreamReader(s))
    {
        string contractABIstr = sr.ReadToEnd();
        JObject contractABI = JObject.Parse(contractABIstr);
        string contractResults = (string)contractABI.SelectToken("result");

        var web3 = new Web3();
        var contract = web3.Eth.GetContract(contractResults, address);
        Nethereum.Contracts.Function = contract.GetFunction("owner");

        string owner = "";
        Task<string> task = function.CallAsync<string>();
        owner = await task;
    }
}

When calling the line owner = await task, I get an exception with message "Error occurred when trying to send rpc requests(s)", which has an inner exception with the message "An error occurred while sending the request.", which has its own inner exception with the message "Unable to connect to the remote server", and this one has its own inner exception saying "No connection could be made because the target machine actively refused it [IP address]"

Does anyone know what I'm doing wrong here? (I've simplified the code above but I do check that the function exists)

T. Baer
  • 69
  • 14
  • Error "target machine actively refused" is caused by the TCP layer in Net Library.It can mean a few things.1) You are trying to connect to the loopback IP address 127.0.0.1. Some machine the localhost is assigned the loopback. Doing a Ping LocalHost will tell if localhost is assigned loopback 2) The server isn't listening on the port number you are using. 3) A route doesn't exist to the IP address.After TCP connection is made then there are a lot of other things that can go wrong. The first step is to get the "Actively Refused" error solved. Then attack the other issues if connection fails. – jdweng Jun 27 '21 at 13:07
  • @jdweng How exactly does Ping LocalHost tell me if localhost is assigned loopback? – T. Baer Jun 27 '21 at 13:37
  • The response from a PING is the IP (not the name). So the results if it is the loopback is 127.0.0.1. – jdweng Jun 27 '21 at 17:40

1 Answers1

0

My problem turned out to be that I had initialised the Web3() with no parameters, so it defaults to using localhost as the RPC client. I fixed it by initialising it with the RPC client url that I needed, in this case:

string rpcClient = "https://bsc-dataseed.binance.org/";
Web3 web3 = new Web3(rpcClient);
T. Baer
  • 69
  • 14