Nov 122019
 

To do that we need the executable MSbuild.exe to build our project. Normally its part of the environment variables so if you open command window and type MSBuild.exe and hit enter. If you see any error saying that the command is not found then it means that its not part of the environment variable.

Normally its located  at this path C:\Program Files (x86)\MSBuild\14.0\Bin so either you open this path and write the command there or you add that to the environment variables.

After that its really easy to run the build all you have to do is write the command

 

MSBuild.exe solutionname.sln

 

This command will build the solution. This command could come really handy if you plan to make your own power tools.

 Posted by at 10:31 pm
Nov 122019
 

Lets start by thinking what we want to do here. Now there could be some requirement where we want to log all the web calls that user is making so that we can log some audit trail.

Audit trail are good to keep accountability in the application so that problem and breach detection is easy and trackable.

If you want to add audit trail on a request level its really easy. You can make a filter that can intercept each web request and log that action. You can either log the action name or decorate each action name and use that to log.

Lets start by writing some code. So first we need to write that attribute or filter

audit1

If you notice this is a very basic class that is driving from the action filter attribute. By doing that we also get access to the overrideable methods of ActionFilterAttribute. One of which is OnActionExecuted. This method will be invoked when the action method is finished execution.

This method will be called on each web request and you can log/validate the action call here.

Lets say there are some action that you don’t want to log like api calls. In those cases you can write exception classes which are simple action attribute.

audit2

To use this we will go to our LogThisAttribute and add a exclusion so that we can skip logging if he action method is decorated with DoNotLogThis attribute.

audit3

Now that we have all the building blocks setup lets start using it.

I am going to use it in my basic MVC Dotnet Core web application. To use this I need to configure it in the startup.cs class

audit4

If you notice on line 38 I added the filter to the global Filters so it should be called on every action call. This is added in the MVC middleware options.

NOTE: IN the action executed method one could say that I only want to login stuff on successful execution else I want to skip the logging. There is no directly way of handling that except you throw a validation exception indicating that the method execution was unsuccessful. You have to process that exception in the context and make decisions on it.

You can find the code at

https://github.com/alineutron/Lab/tree/master/dotnet/HttpLogIntercepter

 

 Posted by at 10:28 pm
Nov 112019
 

Ok so this is something that I was not really aware on how to do. The assignment was to read the data from the log analytics in azure to show it in one of our application by using c#

The challenge was to understand how to do it. So I started searching google and I found these three very interesting links

https://dev.loganalytics.io/documentation/Tools/CSharp-Sdk

this link is mentioned on these SDKs but it limited to limitations in the OpenAPISpecification. I am going to try that out to see if it works or not. I couldn’t quite understand the domain in this part may be its super easy and obvious but as I said I am not fully aware I think I will skip that,

The other way that I found is mentioned on this link

https://docs.microsoft.com/en-us/rest/api/loganalytics/query/get

this is the more direct way of accessing it but its more to access via power shell or python. There is still no way mentioned here to access it via c# so I found these two links

https://stackoverflow.com/questions/53915236/querying-azure-log-analytics-from-c-sharp-application

https://blogs.technet.microsoft.com/livedevopsinjapan/2017/08/23/log-analytics-log-search-rest-api-for-c/

from the second link I was able to get the bearer token so I use this link to get the token and from the first link I checked how to use the api call.

Initially I was still getting the 443 forbidden so what I did is I created a new app and allowed the log analytics access to that app. I did another run and it still didn’t work. So then I realized that my log analytics workspace should also that app to access the logs. So I have to add the role assignment in the workspace for that app. I did that and after that I got a 404 because the log table that I accessed doesn’t exists so I added another table name ‘Usage’ in the query and it worked fine.

 Posted by at 10:22 pm
Nov 102017
 

 

 

 

 

You can say that the bot framework can be classified in to the following concepts.

keyconcepts

 

Connector: The bot framework is basically a REST full API that is used to communicate between the Bot and the channel. Now here the channel could be anything like skype, slack kik etc. Basically the connector library is used to perform an interaction between the Bot and the user.

Activity: The means to facilitate communication between the bot and the user is called activity. This is used by the connector. Now an Activity is an object which could be a message or any other type.

Dialog: Dialog in bot framework is used to model a conversation and conversation flow. You can have a dialog within a dialog to model a meaning full conversation. The main purpose of having a nested is model like is because of the re usability. The Dialog in the Dialog Context is maintained in dialog stack that are in a conversation.

FormFlow: Sometime the input from a user is more complex than just a message in those cases a form flow is used. For example if you want to take multiple input from the user like customer review or some general information regarding a complex question. The formflow can automatically generate dialog to maintain a guided conversation.

State: As obvious by its name the state is associated with a user. The bot frame maintain the state of the user that is communication with the bot at any given time, If the user leaves you can always reload the state of the user and see where he left and continue the conversation from there.

 Posted by at 4:29 am
Nov 062017
 

Whenever i talk Bot i always think of those Sifi movies where you can have those super complex and intelligent digital beings. When I heard that the bot framework is out I wanted to try it and it wasn’t that easy to learn and develop. I know I am a bit late in this blog so the words might be new but the technology is not that recent. So lets start coding then.

So first off you need to follow this post to set up the environment and the template for the bot. So when you create a new bot project you will given some template files on which you can work directly and that is what we are going to do as well. I will directly use those templates and change them to more meaningful and understandable code.

If you see the template generate files you will observe that the code is divided into two main parts, Controller and Dialog. The Controller will send the message that will come from the user via the bot interface and it will send it to the dialog. The dialog will process the message and will reply accordingly. We can have multiple dialogs you can consider them as routes in a website.

How will we work

MessageController: No change here so we will use the same file as the template generate it.

RootDialog: Here we will instrust the dialog to get user profile information. Do remember that this class needs to be serializable.

EnsureProfileDialog: This dialog will get the user profile from the user. You can also notice that this class is serializable

UserProfile: This serializable datastrcuture will hold the actual profile information

This is how the bot will look like

bot hello world

 Posted by at 10:35 am
Jul 242017
 

I wanted to create a self hosting console application without going to the hassle  of creating a new web application project. So all i had to do was to create a console application and then do the nuget and OWIN do the rest.

Create a new console project and paste this code in the main procedure.

 

So the first thing that you will see if that you will get error as it is unable to identify the WebApp class. So the first thing that you need to do is to include the nuget package. Include the nuget package called ‘Microsoft.AspNet.WebApi.OwinSelfHost’ to the project and then try to compile and run the project. After that you will still get more error that Startup class is missing. So include this class.

Now compile the project and you will be able to compile. Lets see the code now

The WebApp.Start is taking a class called Statup as configuration. The configuration defines multiple configuartion paramatere  but here we are only defining the route though which the we will access the REST full API. Now when you run the application you will see a 404 as its still unable to find the values controller. So lets include that.

Snippet

Now run the app and you will see an output. Wohoo a successful self hosting app.

 Posted by at 10:22 am
Jul 132017
 

To create your first c# Bot you need to setup your environment you the Bot project template and the Bot framework channel emulator. They are not included in the standard VS installation package (at least not yet) so we have to fix it ourselves. Lets fix the project template first. You can download the project template from this link. After you download the template unzip the template and open this path “C:\Users\usernmae\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#” and paste you unzipped folder here. Once you have that you will be able to see the new Bot project template in your ‘Create New Project’ dialog.

bot new project

 

Once you have that you can just hit F5 to run but you will notice that it will only open your selected browser nothing else will happen. You need to communicate with your bot and for that you need an emulator. You can download the emulator from here. Once you have that you can run it and along with the hosted application. We will see the emulator in more detail in the next slide.

Bot Framework Channel Emulator

bot emulator empty

 

 Posted by at 2:59 pm
Jul 122017
 

Though the concept is really simple but there is always an edge confusion between the two. Lets discuss them one by one using example. To understand the concept of Abstract lets consider one of this easy case domain problem. Lets discuss the characteristics of an animal. An animal can have 4 legs if its a horse and two legs if its a Kangaroo (ok you can argue that the arms are legs as well but lets assume that its only two legs and two arms). When i define a object of such characteristics i cant really say directly that an animal has 4 legs. I mean in real life it could have 4 but not when its a Kangaroo. Let me define the two processes separately

If we have a domain case where we say that we cannot haven’t an object of Animal class because we don’t know if it would have 4 or 6 legs. but we could have Horse or Kangaroo objects. Then we define the animal class abstract. What it will do it one it could restrict the type animal to be instantiated and secondly it would force all the child classes to implement the base class abstract method.

If we have a domain case where we have a Passenger class which could travel with ticket. But if its European passenger then we need to check the passport only and if its Non European passenger then we need to check the Passport and visa. To implement that check the example below where the base class(Passenger) method is overridden by the child class based on their nature

 

 

 

 

 

 Posted by at 4:38 pm
Jul 112017
 

Structs are value types so their memory allocation is stored in Stack in the memory on your computer and the class is stored on heap which is a different location the logical memory. The reason behind that is structs are value types and classes are reference type. So if you pass a struct to a method and change the value of that the object you will see that the value is changed within that scope of the method. On the other hand if you do the same with a class and pass it to a method. In that method if you update the value of the object. You will see that the value of the actual object is also changed. see the example below

 

The output from that code is

values before passing Asad Mirza
values updated Ali Mirza
values after passing Asad Mirza
Class Object
values before passing Asad Mirza
values updated Ali Mirza
values after passing Ali Mirza

So you see for the class object the actual value is updated

 Posted by at 11:29 am
Jul 102017
 

In this example i will printing out the list of movies that we get in the form of Json and print them out.

Step1: Create a console project

Step2: Add newtonsoft nuget package

Step3: I will be using a open source api that i will call to pint the title of the movie along with the release year. In this example i will print the movies which have the keyword beast in the title. So here is the code.

Snippet

In the above code the variable text have the resulting json text. which looks like this

[ { “id”: “5628302”, “title”: “Beast (III)”, “year”: “2017”, “poster”: “N/A”, “type”: “movie” }, { “id”: “6414796”, “title”: “Beast (V)”, “year”: “2017”, “poster”: “N/A”, “type”: “movie” }, { “id”: “4261746”, “title”: “Beast (VIII)”, “year”: “2017”, “poster”: “N/A”, “type”: “movie” }, { “id”: “1365050”, “title”: “Beasts of No Nation”, “year”: “2015”, “poster”: “N/A”, “type”: “movie” }, { “id”: “1152398”, “title”: “Beastly”, “year”: “2011”, “poster”: “N/A”, “type”: “movie” } ]

To deserialize it I will cal the newtonsoft deserializer that will map the json values to my object MovieInfo and provide me with the converted list.

 Posted by at 10:38 am