pages

Thursday, 23 April 2020

Override ToString in Csharp

First question that comes to mind is why should I do that. My answer is “for fun”. ;)
The code is pretty simple to write. Here is a sample.

There are two return statements. One return name and salary, while the other returns json string.

It is mostly recommended to override ToString for debugging purposes.

I was able to find a good stackoverflow answer written in 2012, talks about why we should do that.

There is an important note on msdn article on how to override ToString which is as below.

"When you decide what information to provide through this method, consider whether your class or struct will ever be used by untrusted code. Be careful to ensure that you do not provide any information that could be exploited by malicious code."

You can find the complete program here.





Saturday, 11 April 2020

Using yield return in CSharp


I was not very sure on the use of yield return in C#, until I read completely about it in very simple manner. I will try my best to share all I learned in as simple words I can.

I know it’s a very old thing and many people online use it, but I believe there, still, are many developers like me who are avoiding it because all the books and articles explain it in very few words at very high level view.

So, let us just start with a simple premise, we need a function to return list of numbers from a very large sequence. An example can be to return the list of first 100 even numbers, assume that later on in the program we are processing our list of 100 even numbers. In your case it can be to pull the record of customers from database on certain condition.

Well there are many efficient ways to write the same program, I intentionally chose this, as this post is about keeping it simple.

If you keep the input as 100, there is no major memory allocation, but if the input is changed to 1million or more, the memory allocated for execution of the program increases drastically. Same can be checked using diagnostic tools in visual studio.


We live in the world where memory cost is always ignored against CPU, but that should not be our reason to fill the stack when we can do better.

yield return is a solution. I wrote the same program as above, the only difference is the return type. Instead of returning the List we are here returning IEnumberable.

Don’t even think of counting the elements in the sequence. This is IEnumerable, not IQueryable. IEnumberable<T> exist in System.Collections.Generic.
IQueryable exists in System.Linq. This article talks about the difference in detail.
You can find the complete program on github.

I hope this was easy. Please share your thoughts in the comments below.

Wednesday, 30 October 2019

Debugging Azure functions locally

Actually the title of this post should be "Debugging your serverless apps locally".
I found a utility while working on Azure functions which helps you debug the function locally(if you have the source code, obviously). It is called ngrok. They have a pretty awesome website and very simple docs.


Steps to proceed are as below.
  1. Make sure your Azure function is up and running in your machine and you are able to hit the breakpoint from your local.
  2. Signup on the ngrok website.
  3. Download the ngrok utility(its an exe file). double click to execute. It opens a command prompt.
  4. Connect to your account. Upon login to ngrok website you will get an auth token which you have to use to connect to your account. command to execute "ngrok authtoken <your_token_here>". This step will create a .yml file in your users directory.
  5. Create a tunnel. Use command "ngrok http 7071". 7071 is the port you want to expose. I used it because its the default port for Azure function apps.
  6. Use the url provided by the ngrok utility to hit the breakpoint on your function from the internet.
I hope this was easy. Please share your thoughts in the comments below.

Wednesday, 23 October 2019

Getting started with Azure Event Grid

As a human being, every new thing scares us. Same was the case with me when I tried started working on Azure Event Grid. No article I was able to find that works out steps by step. The learning curve looked steep almost vertical ;)
Well in this post I try to reduce those efforts for you. I will try to list out the details required to get you started with Event Grid.

Below image is taken from Microsoft Event Grid documentation website.




Azure Event Grid as its mentioned everywhere, its a way to publish/subscribe your events in an easy way. If your subscriber is down It tries in a Exponential back-off way to deliver the message for 24 hours.

First thing you should know is 'Topic'. What is a topic?
Topic is an endpoint where the source sends the event. To respond to certain types of events, subscribers decide which topics to subscribe to.


Event Grid provides below-mentioned event sources: (Topics are already available, you can subscribe to them easily)
  • Azure Subscriptions
  • Container Registry
  • Custom Topics
  • Event Hubs
  • IoT Hub
  • Media Services
  • Resource Groups (management operations)
  • Service Bus
  • Storage Blob
  • Azure Maps


Below mentioned Event Handlers are supported: (You can receive the events using any of the below mechanism)
  • Azure Automation
  • Azure Functions
  • Event Hubs
  • Hybrid Connections
  • Logic Apps
  • Microsoft Flow
  • Queue Storage
  • Service Bus (Preview)
  • WebHooks

The good thing is that you can publish an event of your own using a schema (Custom topic). what it means in real world terms is you call an api with the data in specified format and it will work as event generated and event grid will push it to all the subscribers of that event.

There are three types of schema available.
In case of inbuilt event providers you can even apply filters to the event, so you will get the event you wish to.

Retry policies can be configured. You can also expire the event after certain time.

Please share your thoughts in the comments below.

Tuesday, 8 October 2019

Azure functions, A server less compute

First thing I thought about was "What does it mean when someone says its serverless?"

Well I have found my answer serverless doesn't mean it will not run on server first of all. 😉 It means that you don't have to worry about the server resources and other stuff. You just pay for the time your function is executing. If your function executes for 20 seconds per day well then you mostly will pay for that. You  can explore more about the pricing using the links given at the end of this post.

How to get started? Usually that is the most difficult thing to do.

Create a function is as simple as working on a console app. Open Visual Studio. Create new project of Azure functions type and you are done. :)

Make sure you select:
  1. Http Trigger
  2. Authorization: Anonymous
  3. Function Runtime (2.x)
  4. Storage Account (Storage Emulator) standalone installer

You are ready to go. Press F5 to run your solution, you can see a command window firing up (which has lot of node.js work behind the scenes). Wait till it gives you the URL which mostly is http://localhost:7071/api/function1if you have not done any changes in the default out of the box app.


There is a pretty comprehensive documentation available.

Azure Functions pricing

 Please share your thoughts in the comments below.

Wednesday, 2 October 2019

Connect to your broadband using win32 api using RasDial

So yesterday my router stopped working and new one will come in mail by today. Its one day that I was like without internet (not actually). I tried putting LAN directly to my laptop and connect to broadband. It worked like charm,  but the only problem I am facing now is connecting and disconnecting the connection. So, as a person who knows how to Google I tried finding a command to connect to internet by dialing up my broadband (PPPoE connection) and make it as a batch, just double click and it should work.

I found a command line utility called RasDial which is (RemoteAccessService). If you have already a network connection setup in your computer. you can directly open command prompt and type rasdial /? which gives you all the option to connect. If you have not done the setup yet you can check this link and do it.



Usage:
  1. Connection: rasdial <YourConnectionName>
  2. Status: rasdial
  3. Disconnection: rasdial <YourConnectionName> /disconnect
Pretty simple and straight forward. Please share what you think about it in the comments below.

Tuesday, 10 September 2019

New to KQL? No problem its just like SQL

Well KQL is a query language used to query on Kusto, which is covered in the post Getting started with Kusto.




One Cluster can have multiple Databases and one Database can have multiple Tables. Before going forward let us assume we have a cluster named 'cluster1' and it has database with name 'database1' and database has tables with names 'table1', 'table2', 'table3','table4', 'table5'.

A kusto query usually comprises of  multiple operators and table or cluster name. Unlike SQL table name is not the first thing to be written in KQL. Below is a sample kusto equivalent of select top 5* from table1.
table1 | top 5

Since we are going to query on a big data source lets just add count or where clause to all our queries.

List of  5 basic operators to get started:

1. Project: To select the list of columns to show from the table.
example: table2 | project col1, col2 | take 10.

2. Count: To count the number of rows.
example: table1 | count.

3. Where: It is similar to where clause in SQL server. In KQL we use double equal, unlike SQL, for conditional check.
example: table2 | where col1 = = 'anyvalue'.

4. Take: It picks the number or rows mentioned. It takes rows in no specific order. It is suggested to sort the rows before using take. Limit can also be used for the same purpose.
example: table1 | take 5.

5. Sort (asc/desc): It is used for sorting the data in ascending or descending order, just like SQL.
example: table3 | take 5 col1 asc or table3 | top 5 col1 desc.


You can check out all the available operators/functions/entities at the Microsoft docs website.

Please share your thoughts in the comments below.