Would you prefer to know find out how to construct a dapp with C#? Furthermore, would you want to find the quickest solution to construct a Web3 app with C# and .NET? If that’s the case, then this text is for you! When utilizing Moralis’ enterprise-grade APIs and SDK for blockchain improvement, you’ll be able to construct a dapp with C# and .NET effortlessly. In truth, with Moralis, we will accomplish this improvement process in 4 easy steps:
Create a C# appImport and arrange the most recent Moralis .NET SDKIntegrate your software with Moralis servicesFetch blockchain information from any supported chain
To deal with the above steps simply, it’s essential to additionally handle two stipulations. It’s essential to create your free Moralis account and set up and arrange Visible Studio. Moreover, your Moralis account will provide you with entry to your Moralis Web3 API key, which can be your gateway to Web3 improvement. As such, we are going to first present you find out how to get hold of that key. Furthermore, it’s value stating that Moralis is all about cross-chain interoperability, letting you utilize the identical traces of code to deal with the entire main programmable blockchains. Additionally, you’ll be able to attain a wider viewers and by no means get caught to a specific blockchain. Because of this, you future-proof your dapp improvement.
Nonetheless, even if we are going to construct a dapp with C# and .NET on this tutorial, it is best to needless to say Moralis can be cross-platform interoperable. So, you need to use fashionable platforms similar to Firebase, Supabase, Unity, and lots of others to dive into the Web3 area.
Construct a Dapp with C# and .NET – Taking Care of Stipulations
We are going to first present you find out how to get hold of your Moralis Web3 API key to provoke this text. In any case, doing so is an important piece of the “construct a dapp with C#” quest. So, ensure to create your free Moralis account now. You might use the “create your free Moralis account” hyperlink above or go to the Moralis homepage. There, you’ll have to click on on one of many “Begin for Free” buttons:
Whichever of the 2 choices you select, you’ll land on this web page:
Wanting on the above screenshot, you’ll be able to see that you just’ll have to enter your e-mail handle and create your password. However you might also use your Google account. In case you go along with the previous possibility, ensure to verify your account. You do that by clicking on the affirmation hyperlink that can land in your e-mail inbox.
Together with your Moralis account up and working, you’ll be capable to entry your Moralis admin space. From there, you’ll be capable to entry your Web3 API key in two methods.
First, you’ll be able to simply click on on the “Web3 APIs” possibility within the facet menu:
Then select between “EVM API” and “Solana API”. Whenever you wish to concentrate on Ethereum or different EVM-compatible chains, the previous can be your go-to possibility. Lastly, click on on “Copy API key” after which “Web3 Api Key”:
With the above click on, you’ll copy your Web3 API key. As a affirmation of that motion, you will note a notification within the top-right nook:
Second, you’ll be able to copy your Moralis Web3 API key out of your “Account Settings” web page. To get there, click on on the “Account” possibility within the facet menu. As soon as on the “Account Setting” web page, choose the “Keys” tab after which copy your Web3 API key:
Set up and Set Up Visible Studio
The following prerequisite is to put in and arrange Visible Studio. To make use of this free IDE, use your favourite browser and search engine to question it for “Visible Studio”:
When you land on the official Visible Studio web site, click on on “Free Visible Studio”. This can take you to the “obtain” part, the place it’s essential to click on on the “Free obtain” button:
Lastly, set up Visible Studio in your laptop.
Construct a Dapp with C# and .NET – Step 1: Create a C# App
You’ll begin this step of the “construct a dapp with C#” process by opening Visible Studio. Subsequent, create a brand new undertaking. Furthermore, ensure to pick out “C# Console” because the template:
Then, it’s essential to configure your new undertaking. You do that by coming into your undertaking title and site. Be happy to comply with our lead and title your undertaking “ConsoleDemo”:
Wanting on the above screenshot, you’ll be able to see that we used the “MoralisDemo” folder below location. Furthermore, identical to with the undertaking title, be at liberty to comply with our lead. Lastly, choose the “.NET 6.0” framework and click on on the “Create” button:
By creating your C# software, Visible Studio will generate a primary “C# Console” undertaking for you:
Furthermore, as indicated by the above picture, Visible Studio ought to create the “Program.cs” script in your behalf.
Construct a Dapp with C# and .NET – Step 2: Import and Set Up the Newest Moralis .NET SDK
By including Web3 performance, you’ll be able to convert the above-created C# app right into a dapp. Nevertheless, it’s essential to import Moralis’ SDK so as to add that performance. So, first, it’s essential to handle “NuGet Packages”. You could find this feature below “Instruments” > “NuGet Bundle Supervisor” > “Handle NuGet Packages for Answer…”:
As soon as contained in the “Handle NuGet Packages for Answer…” possibility, ensure to checkmark the “Embody prerelease” field. Then, enter “Moralis” into the search bar. Among the many given outcomes, choose the most recent Moralis package deal and click on on the “Set up” button:
Construct a Dapp with C# and .NET – Step 3: Combine Your App with Moralis Companies
With Moralis’ .NET SDK put in, you’re able to combine your app with Moralis providers. This step will allow you to construct a dapp with C#. So, begin by opening the “Program.cs” file within the “Answer Explorer” window. Then, choose the present content material of that file and delete it. Subsequent, paste within the following two “utilizing” statements:
utilizing Moralis;
utilizing Moralis.Web3Api.Fashions;
With the “utilizing” statements in place, proceed by including “namespace“, “class“, and primary public static “Important“. Moreover, it’s essential to ensure to set “MoralisClient.ConnectionData” with the beforehand obtained Moralis Web3 API key. So, simply change the “YOUR MORALIS WEB3API KEY” together with your key. These are the traces of code it’s essential to use to cowl that:
namespace ConsoleDemo
{
inside class Program
{
static void Important(string[] args)
{
// Setup Moralis
MoralisClient.ConnectionData = new Moralis.Fashions.ServerConnectionData()
{
ApiKey = “YOUR MORALIS WEB3API KEY”
};
}
}
}
Subsequent, it’s essential to create the “DisplayCryptoData” static async perform below the “Important” perform. Furthermore, this perform ought to settle for two parameters – “handle” (string) and “chainId” (ChainList). Nonetheless, the return kind must be “Process”:
inside static async Process DisplayCryptoData(string handle, ChainList chainId)
{
}
The above perform additionally determines that your software will settle for the identical two arguments – the handle and the chain ID. Since it’s important to make sure that these arguments are appropriately handed and legitimate, we advocate including the traces of code that can validate the arguments (see beneath). Lastly, it’s essential to add the “Process.Run” assertion to name the “DisplayCryptoData” async perform.
Your “Important” Operate
With the entire above in place, the next are the traces of code that your “Important” perform ought to include:
static void Important(string[] args)
{
if (args.Size < 2)
{
Console.Write(“Utilization: ConsoleDemo.exe ADDRESS CLIENT_ID”);
return;
}
string handle = args[0];
int chainId = 1;
if (!int.TryParse(args[1], out chainId))
{
Console.Error.WriteLine(“CHAIN_ID have to be a quantity.”);
}
// Setup Moralis
MoralisClient.ConnectionData = new Moralis.Fashions.ServerConnectionData()
{
ApiKey = “YOUR MORALIS WEB3API KEY”
};
Process.Run(async () =>
{
await DisplayCryptoData(handle, (ChainList)chainId);
}).Wait();
}
With the above traces of code, you could have all the pieces in place to construct a dapp with C# that can be capable to fetch on-chain information. Within the remaining step of this quest, you’ll have an opportunity to learn to fetch and show the native stability, ERC-20 token balances, and NFTs’ metadata.
Construct a Dapp with C# and .NET – Step 4: Fetch Blockchain Information from Any Supported Chain
So as to add Web3 performance to your software, we are going to concentrate on the “DisplayCryptoData” perform. Basically, you’ll want so as to add the suitable traces of code to this beforehand created async perform. For starters, you’ll want to show the handle in query:
Console.WriteLine($”For handle: {handle}…n”);
Fetching and Displaying Native Steadiness
You’ve now put in and built-in the Moralis SDK within the earlier steps. As such, now you can make the most of the Moralis Web3 API. Additionally, you need to use intuitive endpoints to fetch all kinds of on-chain information. In terms of fetching the native stability, the “GetNativeBalance” endpoint does the trick. So, that is the road of code it’s essential to add to the “DisplayCryptoData” perform:
// Load native stability for handle
NativeBalance bal = await MoralisClient.Web3Api.Account.GetNativeBalance(handle, chainId);
As well as, you additionally want the next traces of code to correctly format and show the native stability:
double nativeBal = 0;
double.TryParse(bal.Steadiness, out nativeBal);
Console.WriteLine($”Your native stability is {nativeBal / Math.Pow(10,18)}”);
Fetching and Displaying ERC-20 Token Balances
In terms of fetching and displaying ERC-20 token balances, you get to comply with the identical ideas as with the native stability. In fact, it’s essential to use a unique endpoint. On this case, will probably be “GetTokenBalances“:
// Load ERC-20 Token Checklist for handle
Checklist<Erc20TokenBalance> erc20Balnaces = await MoralisClient.Web3Api.Account.GetTokenBalances(handle, chainId);
Moreover, not like the native stability, there may be a number of ERC-20 token sorts in a pockets handle. As such, we have to use a listing and show it correctly. Nonetheless, you also needs to needless to say there may not be any ERC-20 tokens in a specific Web3 pockets. These are the traces of code that can correctly show ERC-20 token balances:
Console.WriteLine(“nnYour ERC 20 Tokens:”);
if (erc20Balnaces != null && erc20Balnaces.Depend > 0)
{
// Print out every token with image and stability.
foreach (Erc20TokenBalance tb in erc20Balnaces)
{
Console.WriteLine($”t{tb.Image} – {tb.Identify}: {tb.NativeTokenBalance}”);
}
}
else
{
Console.WriteLine(“tNone”);
}
Fetching and Displaying NFTs
In terms of fetching NFTs, or ought to we are saying their metadata, issues comply with the identical ideas as within the above two examples. In fact, we have to use an acceptable endpoint. On this case, “GetNFTs” does the trick:
// Load first 10 NFTs for the handle
NftOwnerCollection nfts = await MoralisClient.Web3Api.Account.GetNFTs(handle, (ChainList)chainId, “”, null, 10);
These are the traces of code displaying the title, stability, and metadata for every of the above-fetched ten NFTs:
// Load first 10 NFTs for the handle
NftOwnerCollection nfts = await MoralisClient.Web3Api.Account.GetNFTs(handle, (ChainList)chainId, “”, null, 10);
Console.WriteLine(“nnYour NFTs:”);
if (nfts != null && nfts.Outcome.Depend > 0)
{
// Print out every token with image and stability.
foreach (NftOwner nft in nfts.Outcome)
{
Console.WriteLine($”t{nft.Identify}: {nft.Quantity}ntMetaData: {nft.Metadata}nn”);
}
}
else
{
Console.WriteLine(“tNone”);
}
Notice: It’s the NFTs’ metadata that incorporates URLs of the information (e.g., PNG). So, by creating a correct frontend, you would simply show fetched NFTs.
Final however not least, you’ll be able to view all the code to construct a dapp with C# on GitHub. Furthermore, it is best to use the Moralis documentation for additional help.
Run Your Dapp and Debug
With all of the code in place, you’ll be able to run your dapp in Visible Studio. In the event you use the code as supplied herein, the output can be:
Utilization: ConsoleDemo.exe ADDRESS CLIENT_ID
As such, we encourage you to click on on “Open debug launch profiles UI” (inside “Answer Explorer” > “Properties” > “Debug” > “Normal”):
Then, populate the “Command line arguments” entry area with a pockets handle and chain ID:
Notice: We advocate you utilize your pockets handle. Additionally, use the chain ID that matches the chain on which you could have some stability. To seek out ID numbers for the programmable blockchains, use the “Supported Chains” web page within the Moralis documentation. For instance, the quantity “1” used above corresponds to the Ethereum chain.
In the event you run your dapp once more after coming into your pockets handle and a sequence ID, it would work correctly. Accordingly, the console will show the native stability, ERC-20 token balances, and NFTs’ metadata for a given handle and chain. Moreover, this additionally signifies that you’ve efficiently created a backend dapp with C# and .NET!
Easy methods to Construct a Dapp with C# and .NET in 4 Steps – Abstract
This text demonstrated find out how to construct a dapp with C# and .NET. Moreover, it confirmed you the way to do this by taking you thru these 4 steps:
Create a C# appImport and arrange the most recent Moralis .NET SDKIntegrate your software with Moralis servicesFetch blockchain information from any supported chain
With the abilities and information obtained herein, you now know find out how to create primary backend dapps. So, you could be able to take your C# and Web3 fundamentals to the following stage. Therefore, we encourage you to make use of certainly one of our tutorials and construct a full-stack killer dapp. Keep in mind that you need to use different programming languages and legacy improvement platforms. Whether or not you wish to dive deeper into C# and .NET dapp improvement or for those who’d prefer to discover Firebase, for instance, it is best to use the Moralis documentation. Nevertheless, for much more superior instance initiatives, the Moralis weblog and the Moralis YouTube channel must be your go-to retailers. These two locations will help you turn into a Web3 developer totally free.
Moreover, we should always inform you that to go full-time crypto, changing into blockchain licensed may be your entry ticket. That is the place Moralis Academy enters the scene. That is the place you attend top-notch blockchain improvement programs, obtain a customized research path, and expertise professional mentorship. What’s extra, Moralis Academy gives you with a membership in one of the crucial advancing communities within the crypto realm!