<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dhiraj Khodade on Medium]]></title>
        <description><![CDATA[Stories by Dhiraj Khodade on Medium]]></description>
        <link>https://medium.com/@dhirajkhodade?source=rss-2dba3e4b9f7f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bHzpmFXxCd8JskLJUQYdcg.jpeg</url>
            <title>Stories by Dhiraj Khodade on Medium</title>
            <link>https://medium.com/@dhirajkhodade?source=rss-2dba3e4b9f7f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 05:34:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhirajkhodade/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AWS CDK: Running Containers on AWS Lambda]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://dhirajkhodade.medium.com/aws-cdk-running-containers-on-aws-lambda-e6f24a81ac1?source=rss-2dba3e4b9f7f------2"><img src="https://cdn-images-1.medium.com/max/800/1*TxjkKk0oFDXqAlwdp0h_DA.png" width="800"></a></p><p class="medium-feed-snippet">In this post we will focus on how to deploy same&#x2026;</p><p class="medium-feed-link"><a href="https://dhirajkhodade.medium.com/aws-cdk-running-containers-on-aws-lambda-e6f24a81ac1?source=rss-2dba3e4b9f7f------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://dhirajkhodade.medium.com/aws-cdk-running-containers-on-aws-lambda-e6f24a81ac1?source=rss-2dba3e4b9f7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6f24a81ac1</guid>
            <category><![CDATA[lambda-container-image]]></category>
            <category><![CDATA[aws-cdk]]></category>
            <category><![CDATA[serverless-architecture]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[lambda]]></category>
            <dc:creator><![CDATA[Dhiraj Khodade]]></dc:creator>
            <pubDate>Mon, 24 Jul 2023 13:34:01 GMT</pubDate>
            <atom:updated>2023-10-05T23:16:56.335Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[AWS CDK: Serverless private API with AWS Lambda Powertools]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://dhirajkhodade.medium.com/aws-cdk-serverless-api-with-aws-lambda-powertools-1eec97af2a32?source=rss-2dba3e4b9f7f------2"><img src="https://cdn-images-1.medium.com/max/1492/1*bBZxAnj5uaqHPrNJ-COMOg.png" width="1492"></a></p><p class="medium-feed-link"><a href="https://dhirajkhodade.medium.com/aws-cdk-serverless-api-with-aws-lambda-powertools-1eec97af2a32?source=rss-2dba3e4b9f7f------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://dhirajkhodade.medium.com/aws-cdk-serverless-api-with-aws-lambda-powertools-1eec97af2a32?source=rss-2dba3e4b9f7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1eec97af2a32</guid>
            <category><![CDATA[power-tools]]></category>
            <category><![CDATA[aws-private-api-gateway]]></category>
            <category><![CDATA[aws-cdk]]></category>
            <category><![CDATA[iac]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <dc:creator><![CDATA[Dhiraj Khodade]]></dc:creator>
            <pubDate>Sun, 23 Jul 2023 10:24:34 GMT</pubDate>
            <atom:updated>2023-10-05T23:16:27.885Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Blockchain explained using C# implementation]]></title>
            <link>https://dhirajkhodade.medium.com/blockchain-explained-using-c-implementation-5482dc980c47?source=rss-2dba3e4b9f7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/5482dc980c47</guid>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[blockchain-technology]]></category>
            <category><![CDATA[aspnetcore]]></category>
            <category><![CDATA[blockchain-development]]></category>
            <dc:creator><![CDATA[Dhiraj Khodade]]></dc:creator>
            <pubDate>Mon, 01 Mar 2021 12:29:05 GMT</pubDate>
            <atom:updated>2021-03-04T14:17:52.091Z</atom:updated>
            <content:encoded><![CDATA[<p>In earlier <a href="https://dhirajkhodade.medium.com/how-does-bitcoin-mining-work-b1ffb0ac3142">post</a> we saw what Blockchain and <a href="https://dhirajkhodade.medium.com/how-does-bitcoin-mining-work-b1ffb0ac3142">Bitcoin Mining</a> is and how it works. In this post we will be implementing our own simple crypto currency using blockchain algorithm which will have following features. (Find the link to complete source code at the end of this article.)</p><ul><li>Create Blockchain</li><li>Mining Rewards &amp; Transactions</li><li>Validate and Sign Transactions (Elliptic curve signature algorithm)</li><li>Proof-of-Work</li><li>Blockchain Explorer UI front-end in aspnet core for easy understanding of Blockchain functionality</li></ul><p>I have added this project’s docker image to <a href="https://hub.docker.com/r/dhirajkhodade/mudracoin-blockchain-app">DockerHub</a>. You can directly run this image as container on you machine using below docker command and access it from browser — <a href="http://localhost:8080/">http://localhost:8080</a></p><blockquote>docker run -p 8080:80 dhirajkhodade/mudracoin-blockchain-app</blockquote><h3>Block</h3><p>As we saw blockchain consists of multiple blocks and each block has 3 main elements</p><ol><li>The <strong>Data</strong> in block which will be list of transactions in our case.</li><li><strong>Nonce. </strong>It&#39;s a random integer number adjusted by miners to get the desired hash value of the block. Once the valid nonce is found it gets added to block along with generated hash for that block. Generated hash is compared with targeted hash. In below code observe the method MineBlock(int difficulty) this will keep calculating the hash of the block until it matches the targeted value. For Example if difficulty is 4 then it will keep adjusting nonce value till it gets the hash value which has 4 zeros at the start of the hash similar to this <strong>0000</strong>eb6f460221dd465688be5d7a1e955088e46ada2f083c3d801c3b7c8aac34</li><li>The <strong>Hash </strong>is 256 bit number with lots of zeros at the start of it as shown above.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47b620aec4fc060e0308f685bc02be40/href">https://medium.com/media/47b620aec4fc060e0308f685bc02be40/href</a></iframe><h3>Transaction</h3><p>As we know blocks are formed by grouping pending transactions over certain period of time. To make sure transactions are valid we are going to sign transactions in our blockchain with ECC – Elliptic Curve Cryptography – <a href="https://www.nuget.org/packages/Secp256k1.Net/">secp256k1</a>. Signing transactions will make sure only you can spend coins in your wallet. We also have validate transaction method which will make sure transactions are signed by owner of the wallet. So if anyone else tries to make a transaction/spend your coins on your behalf that will be marked as invalid and will not be recorded in blockchain system.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a40579e8f74ebc5b93e5fc9584fc933c/href">https://medium.com/media/a40579e8f74ebc5b93e5fc9584fc933c/href</a></iframe><h3>Signing Transactions</h3><p>So how does this signing work? this is pretty simple every user on network has their own pair of public and private key. In fact the address of you wallet is your public key which you share with everyone to receive coins and there is a private key associated with this public key which you use to sign the transactions. Look at the <strong>SignTransaction(string secretKey)</strong> method this extracts the public key from your secretKey and then compares it with your wallet address i.e. your public key. If it matches that means transaction is coming from owner of the wallet and is valid but that is not enough you need to protect your transaction even after it is created and put into transaction pool so for this we are going to sign it with our private key for this we calculate the hash of the transaction and sign it with secret key and then the signature is attached with transaction to validate it anytime you want.</p><p>To validate transaction we have <strong>IsValid()</strong> method where we check if the signature that transaction hold is really a valid signature for the transaction for that we will recalculate the hash of the transaction and verify if that signature belongs the same hash. If somehow some hacker changed your transaction the hash of the transaction will also change and this will invalidate the signature, as that hash is not what we signed for it will fail the signature verification and will me marked as invalid transaction.</p><h3>Blockchain</h3><p>Now that we have all the parts of blockchain ready lets create blockchain. We will need a method to add newly created transactions to pending transaction’s pool. Observe AddTransactionToPool(Transaction transaction) method below we are checking if the transaction is valid or not using IsValid() method that we created earlier in Transaction class. Also observe the Blockchain constructor which adds a first block to the Blockchain. This is a special block which does not have any data or transactions and it does not refer to the previous block as it is the first block of blockchain. Its called <strong>Genesis Block</strong>.</p><p>We will need mining functionality in our blockchain. Remember miner of the block gets paid a reward in BTC. So if you observe the method MinePendingTransactions() it accepts miner’s wallet address to receive his/her mining reward which I have set to 12.5 BTC by default and mining difficulty to 4. When MinePendingTransactions() method is called it will create one more transaction which will not have FromAddress as that is reward transaction and ToAddress will be the miner’s address and we will add this reward transaction to the pool and create a block of all the pending transactions along with the reward transaction and call MineBlock() method of the Block class that we created earlier by passing the mining difficulty as parameter. Once the block is mined we will add it to the our blockchain as it has passed all the checks on its way to the blockchain making it the tamper proof, valid block of verified transactions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1be770214ce93058b19452fc18ebaaf9/href">https://medium.com/media/1be770214ce93058b19452fc18ebaaf9/href</a></iframe><h3>UI Dashboard - Blockchain Explorer</h3><p>Let&#39;s use all the core functionality that we have created for our blockchain and create UI dashboard. Let&#39;s call it Blockchain Explorer. This explorer will help us create and sign transactions, see them being added to transaction pool and then mine those into a block, explore the blockchain block by block and have a look at transactions inside it, set mining reward and mining difficulty, check the balance of users who made transactions; of-course we can do that after all its a public ledger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CQglMDGqAGRbcNoAdamw1Q.png" /></figure><p>This is how our Bitcoin Explorer UI looks like. You can find complete source code on GitHub -</p><p><a href="https://github.com/dhirajkhodade/MudraCoin-Blockchain">dhirajkhodade/MudraCoin-Blockchain</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5482dc980c47" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How does bitcoin mining work?]]></title>
            <link>https://dhirajkhodade.medium.com/how-does-bitcoin-mining-work-b1ffb0ac3142?source=rss-2dba3e4b9f7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/b1ffb0ac3142</guid>
            <category><![CDATA[blockchain-technology]]></category>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[bitcoin-mining]]></category>
            <category><![CDATA[proof-of-work]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Dhiraj Khodade]]></dc:creator>
            <pubDate>Sun, 28 Feb 2021 07:26:34 GMT</pubDate>
            <atom:updated>2021-12-03T12:43:09.333Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Blockchain</strong> is the core technology behind bitcoin.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*62rlhdAwvzWFDFHi0n84eQ.jpeg" /></figure><h3>What is Blockchain?</h3><p>Blockchain is digital ledger of transactions and it is based on peer-to-peer (P2P) topology, so it allows data/transactions to be stored globally on thousands of servers. Allows anyone on the network to see everyone else’s data/transactions. That makes it difficult for any one user to gain control and tamper the transactions. Basically it is shared, immutable ledger which makes it impossible to hack or change the system.</p><h3>What is Bitcoin mining?</h3><p>As we know Blockchain is decentralized P2P network which has no central authority or middleman instead it is powered by it’s users. So Bitcoin transactions are verified in decentralized system where people contribute computing resources to verify these transactions.</p><blockquote>The process of verifying transactions is called mining.</blockquote><p>This is called mining because it is similar to mining of commodities like gold, where it takes lots of efforts and lots of resources to mine gold and there is only so much gold in earth that makes its supply limited and the amount of gold mined every year is roughly same. Similarly huge computing power is required in process of mining bitcoin and number of bitcoins generated decreases over the time. Also there is limited supply, <strong>only 21 million bitcoins will ever be created. </strong>So bitcoin being a currency with limited supply makes it deflationary currency.</p><h3>Why do we need Mining process?</h3><p>Bitcoin mining is done to make the bitcoin blocks secure and tamper proof. And people who contribute computing power and resources to mine blocks are rewarded with transaction fees and newly created bitcoins.</p><h3>How Miners get Rewarded?</h3><p>Miners verify transaction using software called bitcoin miner. This program basically puts all the transactions happened in particular time period (approx. 10 mins for bitcoin can be different for different crypto currencies) together which is called as block. Miners try to guess the hash of this new block which is expensive operation in terms of computing and the miner who guesses this hash first gets the mining reward and the block is added to blockchain with link to previous block using hash of previous block. So each block has hash of previous block along with its own hash. If anyone has to tamper the block he/she has to recalculate the hash of all the blocks in the blockchain which is very very time consuming and costly operation and also remember blockchain is decentralized ledger and thousands of users have the copy of blockchain so it is impossible for someone to hack or tamper the blockchain.</p><h3>Proof-of-Work</h3><p>Proof of work is just a piece of data that is hard to produce also called as mining difficulty. In bitcoin this difficulty self-adjusts every 2016 block which is approx 2 weeks and it also self adjusts in such a way that it should take approx 10 minutes to mine the block. If it takes less than 2 weeks to get to 2016 blocks then difficulty increases and if it takes more than 2 weeks then difficulty decreases.</p><p>When a valid block is mined the miner gets reward of few bitcoins currently (as of 2021) this reward is 6.5 BTC. In early days of bitcoin this reward was 50 BTC per block and this reward goes down by half every four years. Miners also get transaction fees which is paid by senders of transactions.</p><p>As you can guess mining is kind of gamble. Only first miner who mines the block only he/she gets the reward while others who were trying parallely do not get anything, instead all the computing resource they spent goes in vain. So miner started to organize themselves in groups to combine computing power with everyone in the group to increase chances of mining block and then share mining reward with everyone in group fairly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b1ffb0ac3142" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Self-Mutating CDK pipeline which has ability to update the very infrastructure that it runs on!]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://dhirajkhodade.medium.com/self-mutating-cdk-pipeline-which-has-ability-to-update-the-very-infrastructure-that-it-runs-on-cd116e3240e3?source=rss-2dba3e4b9f7f------2"><img src="https://cdn-images-1.medium.com/max/600/0*lJwRKhMoi1iLRRb7.png" width="600"></a></p><p class="medium-feed-snippet">As we all know importance of automation in IT, we are supposed to automate everything.</p><p class="medium-feed-link"><a href="https://dhirajkhodade.medium.com/self-mutating-cdk-pipeline-which-has-ability-to-update-the-very-infrastructure-that-it-runs-on-cd116e3240e3?source=rss-2dba3e4b9f7f------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://dhirajkhodade.medium.com/self-mutating-cdk-pipeline-which-has-ability-to-update-the-very-infrastructure-that-it-runs-on-cd116e3240e3?source=rss-2dba3e4b9f7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd116e3240e3</guid>
            <category><![CDATA[aws-cdk]]></category>
            <category><![CDATA[inception-pipeline]]></category>
            <category><![CDATA[awscdkpipeline]]></category>
            <category><![CDATA[ci-cd-pipeline]]></category>
            <category><![CDATA[self-mutating-pipeline]]></category>
            <dc:creator><![CDATA[Dhiraj Khodade]]></dc:creator>
            <pubDate>Sun, 01 Nov 2020 12:27:13 GMT</pubDate>
            <atom:updated>2023-10-05T23:17:13.105Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[What actually Docker is?]]></title>
            <link>https://dhirajkhodade.medium.com/what-actually-docker-is-3ecb9ef54d5f?source=rss-2dba3e4b9f7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ecb9ef54d5f</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[what-is-docker]]></category>
            <category><![CDATA[containerization]]></category>
            <category><![CDATA[how-it-works]]></category>
            <category><![CDATA[containers]]></category>
            <dc:creator><![CDATA[Dhiraj Khodade]]></dc:creator>
            <pubDate>Sun, 01 Nov 2020 12:11:43 GMT</pubDate>
            <atom:updated>2020-11-01T12:11:43.358Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X1ukuqvYib9eadD8sD5pdQ.png" /></figure><blockquote>“Docker” — something that everybody is fascinated about.</blockquote><p>I have seen while discussing Docker many can’t quickly understand “what actually docker is”. This lack of understanding leads to containerization being treated or thought of as VMs. Containers are abstraction at application layer whereas VMs are abstraction at physical hardware.</p><p>I have read many good articles on the internet about docker and it’s working. So I will try to put my knowledge and readings about docker here in this article.</p><p>First of all,</p><blockquote>Containers are not a new thing!</blockquote><p>Docker is nothing but a <strong>“Packaging Format” </strong>and Runtime for your applications and/or processes.</p><p>We used to run exe files with shared DLLs and few of us liked to use their own private DLLs to avoid famous DLL-hell problem. We use many different tools like Chef/Puppet/Ansible to start our applications. Docker is packaging format and runtime that allows you to specify how to package your application and how to run it, and to define what all things are required for it to run(i.e. Runtime).</p><p>As I said earlier “ <strong>containers are not a new thing!” </strong>Docker “containers” are nothing but processes. NEVER forget what they are — <strong>processes!</strong> They (containers) were available long before Docker in Linux. Docker introduced simple Daemon process and a powerful REST-API at right time in market which led this technology to grow rapidly.</p><p>Running “fork()” in C code on *nix machine is similar to or I would rather say you have created your own “Docker container”. (this is just a ENORMOUS simplification — as Windows guys will disagree as they have their hypervisor-based containers but at the end they run inside linux VM and also latest version of windows supports virtualization natively, anyway “Docker Windows vs Linux” is different topic to discuss altogether) Over the time they added many new features to docker such as sandboxing processes better i.e. controlling what they can or can’t call etc but we could do that long before docker in Linux using process isolation. You can apply logical network policies in docker but again we did that using IP Tables since 90&#39;s.</p><p>Are you getting what I am trying to say??</p><p>None of things that docker offers are “new” or “magical”.</p><p>Docker won’t make your app secure, or scalable or whatever it is you’re missing with current non-docker technology. Technology wise Docker won’t give you something that didn’t exist before.</p><p>But hell yeah Docker will make your life easier in using all these tools and technologies under one roof. That is why we need to understand those tools what they do, how they do and why they do it!</p><p>The reason docker got famous all of a sudden is because of dev-ops world where we deploy and destroy multiple times a day with all network and resource limitations and what not. Docker gave us a simple and really efficient way to define, run and manage our processes. Docker is devops tool and it is really good at what it does. Docker makes it easy to run processes in isolation with restricted permissions and resources under specified network policies and more.</p><blockquote>But at the end they are <strong>PROCESSES!</strong></blockquote><p>Note:- <em>Don’t be afraid of spinning up hundreds of containers and destroy them (b’coz they are efficient and cheap). </em><br><em>Everything at google runs in containers (that too from long before arrival of Docker). Google spin up billions of containers every week. Check it </em><a href="https://cloud.google.com/containers"><em>Here</em></a><em>.</em></p><p><em>Originally published at </em><a href="http://www.dontworrygeek.com/2020/03/understanding-what-is-docker.html"><em>http://www.dontworrygeek.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ecb9ef54d5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CI/CD pipeline for .NET]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://dhirajkhodade.medium.com/ci-cd-pipeline-for-net-8752dac2e21a?source=rss-2dba3e4b9f7f------2"><img src="https://cdn-images-1.medium.com/max/960/1*C2NevReUrClbE5fpN_Isbw.png" width="960"></a></p><p class="medium-feed-snippet">While working on CDK and&#xA0;.Net framework I couldn&#x2019;t find much help on the internet to deploy&#xA0;.Net framework applications to IIS using CDK&#x2026;</p><p class="medium-feed-link"><a href="https://dhirajkhodade.medium.com/ci-cd-pipeline-for-net-8752dac2e21a?source=rss-2dba3e4b9f7f------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://dhirajkhodade.medium.com/ci-cd-pipeline-for-net-8752dac2e21a?source=rss-2dba3e4b9f7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8752dac2e21a</guid>
            <category><![CDATA[dot-net-framework]]></category>
            <category><![CDATA[ci-cd-pipeline]]></category>
            <category><![CDATA[aspnet-mvc]]></category>
            <category><![CDATA[aws-cdk]]></category>
            <category><![CDATA[aws-codepipeline]]></category>
            <dc:creator><![CDATA[Dhiraj Khodade]]></dc:creator>
            <pubDate>Sun, 01 Nov 2020 11:47:53 GMT</pubDate>
            <atom:updated>2023-10-05T23:17:29.419Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Easier way to make C# (Server side) enums automatically available at client side-javascript]]></title>
            <link>https://dhirajkhodade.medium.com/easier-way-to-make-c-i-e-e81296b8ec12?source=rss-2dba3e4b9f7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e81296b8ec12</guid>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[enum]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Dhiraj Khodade]]></dc:creator>
            <pubDate>Sun, 01 Nov 2020 10:52:26 GMT</pubDate>
            <atom:updated>2021-12-03T12:40:22.070Z</atom:updated>
            <content:encoded><![CDATA[<h3>Easier way to make C# (i.e. Server side) enums automatically available within javascript (i.e. Client side)</h3><p>There are too many human errors and work involved in creating a new JavaScript object for the client side every time there is a new C# Enum on the server side that we want to access at client side in javascript and We are about to take care of this lengthy process here.</p><p>We are going to achieve this by creating our own custom HtmlHelper, which will spit out the required JavaScript Enum for us.</p><p>Most of our code is going to be at server side. :)</p><p>Let’s start with the code (We are working with .Net MVC code samples)</p><p><strong>SERVER SIDE :</strong></p><p>1. Creating custom HtmlHelper</p><p>To create our own HtmlHelper we need to add extension method to HtmlHelper class.</p><pre>namespace MyProject.Common { <br>public static class ExtensionMathods { <br>public static MvcHtmlString GetEnums &lt; T &gt; (this HtmlHelper helper)  <br>{ <br> System.Text.StringBuilder jsEnum = new System.Text.StringBuilder(); <br> jsEnum.AppendLine(&quot;&lt;script type=\&quot;text/javascript\&quot;&gt;&quot;); <br> jsEnum.AppendLine(&quot;if(!window.Enum) Enum = {};&quot;); <br> <br>var enumeration = Activator.CreateInstance(typeof(T));  <br>// var enums = typeof(T).GetFields().ToDictionary(x =&gt; x.Name, x =&gt; x.GetValue(enumeration));</pre><pre>var enums = Enum.GetValues(typeof(T)).Cast &lt; int &gt; ().ToDictionary(x = &gt;Enum.GetName(typeof(T), x), y = &gt;y); <br>   <br>jsEnum.AppendLine(&quot;Enum.&quot; + typeof(T).Name + &quot; = &quot; + System.Web.Helpers.Json.Encode(enums) + &quot; ;&quot;); <br>jsEnum.AppendLine(&quot;&lt;/script&gt;&quot;); <br>   <br>return new MvcHtmlString(jsEnum.ToString()); <br>}}}</pre><p>This extension method will create instance of the passed Enum ‘’T’’ from client side and will read its fields and values and convert it to JSON and final output will be the Javascript containing required Enum ready to use.</p><p>2. Adding namespace to web.config so that the GetEnums() method will be accessible in all views.</p><p>Note: You are supposed to add below given namespace entry in web.config situated in <strong>Views folder</strong></p><pre>&lt;pages pageBaseType=”System.Web.Mvc.WebViewPage”&gt;<br> &lt;namespaces&gt;<br> &lt;add namespace=”System.Web.Mvc” /&gt;<br> &lt;add namespace=”System.Web.Mvc.Ajax” /&gt;<br> &lt;add namespace=”System.Web.Mvc.Html” /&gt;<br> &lt;add namespace=”System.Web.Optimization”/&gt;<br> &lt;add namespace=”System.Web.Routing” /&gt;<br> &lt;/namespaces&gt;<br>&lt;/pages&gt;</pre><p>Now compile the project.<br> Sometimes adding this namespace in web.config will not give the access to custom HTML helper in views , in that case try unloading and loading the project from solution or you may restart visual studio.</p><p>3. Lets create one sample enumeration</p><pre>namespace MyProject.Common<br>{<br>  public enum AccessRole<br>  {<br>    ApplicationView = 1,<br>    ApplicationEdit = 2,<br>    GlobalAdmin = 3<br>  }<br>}</pre><p>Now we are all set to use our custom HtmlHelper helper in client side javascript.</p><p><strong>CLIENT SIDE :</strong></p><p>1. In view we can now use HtmlHelper as follows</p><pre>@(Html.GetEnums&lt;MyProject.Common.AccessRole&gt;())</pre><p>Above line will call the Extension method created in step 1 at run time and it will spit out the javascript code for ‘’MyProject.Common.AccessRole’’ Enum as shown below</p><pre>&lt;script type=”text/javascript”&gt;</pre><pre>if(!window.Enum)<br>Enum = {};<br>Enum.AccessRole = {“ApplicationView”:1,”ApplicationEdit”:2,”GlobalAdmin”:3} ;</pre><pre>&lt;/script&gt;</pre><p>lets use the Enums in javascript</p><pre>&lt;script&gt;<br> var permissions=1;<br> if (permissions == Enum.AccessRole.ApplicationView)<br> {<br>   alert(“You have Application View permissions”);<br> }<br>&lt;/script&gt;</pre><p>As value of permissions variable is set to 1 it will show alert message showing message “You have Application View permissions”</p><p>And We Are Done.</p><p><em>Originally published at </em><a href="http://www.dontworrygeek.com/2015/06/easier-way-to-make-c-ie-server-side.html"><em>http://www.dontworrygeek.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e81296b8ec12" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>