首页 > 解决方案 > 使用 C# 部署 Solidity 合约

问题描述

我正在以太坊区块链上开发一个应用程序。我使用Solidity for contract 和Nethereum c# library 来连接到合约。我是以太坊的新手,这是我在区块链中的第一个应用程序!!我想先在一个TESTChain中部署我的应用程序,

问题是C# 代码在发送部署合同的请求后没有回复任何内容。这是我的代码

合同在混音上创建得很好,在运行选项卡中它可以工作

  public class DeploymentTest : ContractDeploymentMessage
        {
      public static string BYTECODE = @"0x....";// removed it because of long string

      public DeploymentTest() : base(BYTECODE) { }
        }

      public async Task ConnectToTest()
            {
                var url = "https://github.com/Nethereum/TestChains";
                var pass = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
                var acc = new Account(pass);
                var chain = new Web3(acc, url);
                var deploymentMessage = new DeploymentTest();
                var deploymentHandler =  chain.Eth.GetContractDeploymentHandler<DeploymentTest>();

                 // this point just waited and does not return anything
                var transactionReceipt = await deploymentHandler.SendRequestAndWaitForReceiptAsync(deploymentMessage);
                var contractAddress = transactionReceipt.ContractAddress;

                }

任何人都可以帮助我,我不坚持使用代码进行部署,无论如何都可能发生,但部署后我需要连接它并调用一些函数。谢谢

已编辑

我下载了适用于 Windows 的 TESTChain 网络,它工作正常,似乎 http:localhost 的默认端口是 8545。但它仍然没有连接到链

    var web3test = new Web3(); // also tried new Web3(http://localhost:8545);
    var isMining = await web3test.Eth.Mining.IsMining.SendRequestAsync();
    var accounts = await web3test.Eth.Accounts.SendRequestAsync();

是我的连接有问题吗?我完全禁用了我的 MacAfee 防火墙。

在此处输入图像描述

已编辑 2

我更换了我的机器,它可以在我的另一台笔记本电脑上运行,我找不到问题。我不知道我是否只是卸载 mcaffe 是否可以?有人对防火墙问题或其他问题有任何想法吗?

标签: c#blockchainethereumsolidity

解决方案


我发现了问题,它在我的c#代码中调用代码的函数正在等待,

Main()
{
TestConnection().wait(); // this is not proper 
}

so I changed it to TestConnection(); and it works

推荐阅读