无法将Nethereum与Infura连接

时间:2019-03-15 08:59:46

标签: c# ethereum

我是Nethereum的新手,我正在阅读文档。我正在尝试连接到infura并获取以太坊基金会帐户的余额(如文档提示:https://nethereum.readthedocs.io/en/latest/Nethereum.Workbooks/docs/nethereum-gettingstarted-infura/):

    public static async void run()
    {
        var web3 = new Web3("https://mainnet.infura.io");
        var balance = await web3.Eth.GetBalance.SendRequestAsync("0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe");
        Console.WriteLine(balance.ToString());
    }

当尝试在没有更多信息,没有异常,一点都没有的情况下应用SendRequestAsync时,此代码将停止。它刚刚以代码0退出。这是什么问题?

1 个答案:

答案 0 :(得分:0)

我认为问题在于该文档未演示完整的控制台应用程序。

这是Nethereum游乐场http://playground.nethereum.com/csharp/id/1001

中的完整示例

注意:由于游乐场展示了可以轻松复制和粘贴的完整程序,因此我们现在不再使用交互式工作簿。


using System;
using System.Text;
using Nethereum.Hex.HexConvertors.Extensions;
using System.Threading.Tasks;
using Nethereum.Web3;

public class Program
{

    static async Task Main(string[] args)
    {

        // This sample shows how to connect to Ethereum mainnet using Infura
        // and check an account balance:

        // We first need to generate an instance of web3, using INFURA's mainnet url and 
        // our API key.
        // For this sample, we’ll use a special API key `7238211010344719ad14a89db874158c`,
        // but for your own project you’ll need your own key.
        var web3 = new Web3("https://mainnet.infura.io/v3/7238211010344719ad14a89db874158c");

        // Check the balance of one of the accounts provisioned in our chain, to do that, 
        // we can execute the GetBalance request asynchronously:
        var balance = await web3.Eth.GetBalance.SendRequestAsync("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae");
        Console.WriteLine("Balance of Ethereum Foundation's account: " + balance.Value);

    }

}