A day with .Net

My day to day experince in .net

Installing Tensorflow in Windows and Visual Studio – Deep learning.

Posted by vivekcek on March 25, 2017

Recently i was working with designing a chat-bot with Microsoft bot framework.
And i was using a combination of Retrieval based model and Intent/Entity based model.

During the research i decided to include some deep learning into the bot with a Generative Model.
Because generative Models will be the future of chat bot’s.

So i decided to play with Google’s tensorflow for deep learning implementation.
As i am a .NET guy, i like to do all my experiments with Visual Studio.
Then i googled and found that we can integrate tensorflow with Visual Studio.

So how can we do that? First we need to have below software requirements.

1. A 64 bit Windows.
2. Visual studio 2015.

Now download 64 bit Python 3.5, You can have it from below link.
https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe

Install python, Please ensure that you selected the “Add Python 3.5 to PATH”

Now open the Command Prompt in Administrator Mode.
Then check the Python Version, by using below commands and ensure that you have 3.5 , 64 bit version.

python --version

Now we need to update pip, Issue the below command.

python -m pip install --upgrade pip

Now install the CPU mode tensorflow by using below command.

pip install tensorflow

Now try this code in your python prompt.

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

Now to do python in visual studio, we need to install Python Tools For Visual Studio.
Have it from this link https://github.com/Microsoft/PTVS

After successful installation create a python application in Visual studio.

Add below code in your file and run.

import tensorflow as tf
hello = tf.constant('Hello, Tensorflow on Windows!')
sess = tf.Session()
print(sess.run(hello))

Posted in Tensorflow | Tagged: , , , | Leave a Comment »

Dependency Injection in Microsoft Bot Framework

Posted by vivekcek on January 15, 2017

In this post i will show you, how you can implement dependency injection in Microsoft Bot application.
Basically Microsoft bot application is a Web API service. So you can implement dependency injection, the same way you done it in Web API.

Here i am using Ninject to implement dependency injection.

1. Add below Nuget Packages

“Ninject.Web.WebApi”

1

“Ninject.Web.Common.WebHost”

2

2. Go to App_Start folder and open NinjectWebCommon.cs

3. Update the CreateKernel() method by adding below line.

RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
return kernel;

4. You may need to add below namespaces in NinjectWebCommon.cs

using System.Web.Http;
using Ninject.Web.WebApi;

5. Register your services in NinjectWebCommon.cs

private static void RegisterServices(IKernel kernel)
{
  kernel.Bind<IMyInterface>().To<MyClass>();
}    

6. Use this class inside your MessageController.

[BotAuthentication]
public class MessagesController : ApiController
{
        
private readonly IMyInterface instance;
public MessagesController(IMyInterface _instance)
{
  instance = _instance;
}

Posted in Bot Framework | Tagged: , , | Leave a Comment »

Moving your app to free service plan in microsoft azure.

Posted by vivekcek on November 11, 2016

I was working on a research project with a developer essentials account in azure.
The first problem i faced was to deploy the app from visual studio ,immediately after creating my subscription.
So i just logged in to the portal and created a Web App, unfortunately i forgot to select the free pricing tier.

After someday’s i found that my credit is going down, from the bill it was found that the app service is consuming all money.

Immediately i had to do something. I tried the below steps.

1. Created a new app in the same resource group with a new service plan and free pricing tier.
2. Then i changed the service plan of my existing app to new free service plan.
3. Then tried to delete my old service plan.
4. Haaaa got the error, i can’t delete this plan due to an app is associated with it.
5. But there was no app associated with that plan.
6. After some reserach i found that it is the resource group is causing the issue.
7. I can’t delete my existing default resource group, as my existing app and free service plan is running above it.
8. Now i created a new Resource group and moved my old service plan to this group.
9. Then i was able to delete my old plan and my app is now running freely.

Moving Service plan to new resource group

1
Deleteing the resource group.

2

Posted in Azure | Tagged: | Leave a Comment »

Amzon AWS Windows Server 2016 hosting -Could not write to output file

Posted by vivekcek on November 2, 2016

I was trying to host an ASP.NET 4.5 application in an Amazon EC2 instance (Windows Server 2016)

Then i got below error

CS0016: Could not write to output file ‘c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\6a5d994b\b5f46f1c\App_global.asax.o34wdr3q.dll’ — ‘Access is denied. ‘

We can solve this by below power shell script

$dir = "C:\Windows\Temp"
$acl = get-acl -path $dir
$new = "IIS_IUSRS","FullControl","ContainerInherit,ObjectInherit","None","Allow"
$accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $new
$acl.SetAccessRule($accessRule)
$acl | Set-Acl $dir

Posted in Amazon AWS | Tagged: , | Leave a Comment »

Angular 2 Routing And Passing Parameters

Posted by vivekcek on October 30, 2016

When we plan to develop an Angaular2 application, the first thing need to be in place is a good routing module.
So the intention of this post is to help developers to design a routing mechanism for their Angular2 App.

Hope the reader have some good understanding of Angular2 basics like Module, Component, Routing etc.

We are going to develop an app that have 2 pages ‘Home’ and ‘Products’.
The Home page contain a textbox and a button. Once we enter some value in the textbox and click the button,
products page will be displayed with the value we entered in the home page.

So this post also demonstrate, how you can pass value from one component to another via routing.

Let us have a look at the UI.

Home Page
1

Products Page

2

We have 3 components in our app.

1. AppComponent (Which is our root component).
2. HomeComponent
3. ProductListComponent

Now follow these steps:

1. First let us create our Routing Module.

import { NgModule } from '@angular/core'
import { RouterModule, Routes} from '@angular/router'

import { HomeComponent } from './home.component'
import {ProductListComponent} from './product-list.component'

const routes:Routes=[
    { path:'', redirectTo:'/home', pathMatch:'full' },
    { path:'home', component:HomeComponent },
    {path:'products/:value', component:ProductListComponent}

];

@NgModule({
    imports:[RouterModule.forRoot(routes)],
    exports:[RouterModule]
})

export class AppRoutingModule{}

2. Integrate our routing module to main AppModule.

import {NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {FormsModule} from '@angular/forms'
import{AppRoutingModule} from './app-routing.module'

import {AppComponent} from './app.component'
import {HomeComponent} from './home.component'
import {ProductListComponent} from './product-list.component'

@NgModule({
    imports:[BrowserModule,FormsModule,AppRoutingModule],
    declarations:[AppComponent,HomeComponent,ProductListComponent],
    providers:[],
    bootstrap:[AppComponent]
})

export class AppModule{}

3. Add as the first child of head.

4

4. Define route links and outlet in our root component (AppComponent)

import { Component} from '@angular/core'

@Component({
    moduleId:module.id,
    selector:'my-app',
    template:`
    <h1>{{Title}}</h1>
    <nav>
    <a routerLink='/home'>Home</a>
    <a [routerLink]="['/products','test']">Products</a>
    </nav>
    <router-outlet></router-outlet>
    `
})

export class AppComponent{
    Title:string='Routing Demo';
}

5. Here is th code for HomeComponent

import {Component} from '@angular/core'
import {Router} from '@angular/router'
@Component({
    moduleId:module.id,
    selector:'my-home',
    templateUrl:'home.html'
})

export class HomeComponent{
    Title:string='Home';
    
    constructor(private router:Router){

    }

    Go(value:string):void{
        let link=['/products',value];
        this.router.navigate(link);
    }
  
}

6. Here is the code for home.html

<h2>{{Title}}</h2>
<input #input/>
<button (click)="Go(input.value)">Next Page</button>

7. Here is the code for ProductListComponent

import {Component,OnInit} from '@angular/core'
import{ActivatedRoute,Params} from '@angular/router'
import{Location} from '@angular/common'
@Component({
    moduleId:module.id,
    selector:'product-list',
    templateUrl:'products.html'
})

export class ProductListComponent implements OnInit{
    Title:string='Product List';
    Input:string;
    constructor(private router:ActivatedRoute,
    private location:Location){}

    ngOnInit():void{
        this.router.params.forEach((params:Params)=>{
            
            this.Input=params['value'];
          
        })
    }

    goBack():void{
        this.location.back();
    }
}

8. Here is the code for products.html

<h2>{{Title}}</h2>
<label>Passed Value is {{Input}}</label>

So you are done.

Download code from https://github.com/vivekcek/AngularCRUD

Posted in Angular2 | Tagged: , , | Leave a Comment »

Desktop application with Angular2 and Electron in One minute

Posted by vivekcek on October 24, 2016

We can use Angular2 and electron to build nice desktop application for Windows/Mac/Linux.
So how to do it in a minute

1. Install visual studio code.
2. Install Node package manager.
3. Clone the code from my git hub.

https://github.com/vivekcek/angular2electron

4. Run command “npm install”
5. Run command “npm start” or debug with VS Code.
6. Please set the path in launch.json, and point to electron module.

capture

capture1

Posted in Angular2 | Tagged: | Leave a Comment »

Angular2 Development With Visual Studio Code

Posted by vivekcek on October 24, 2016

In this post, i will show you how we can use VS Code for Angular2 development.
The main thing is how to configure VS Code to transpile typescript to js files and lunch the app.

1. Install NodeJs
2. Install visual studio code.
3. Install TypeScript via NPM (Visual studio code may normally install TypeScript).
4. Create a folder in E, E:\Angular2
5. Open this folder through VS Code.
6. Go to https://angular.io/docs/ts/latest/quickstart.html
7. Create 3 files in VS Code , package.json, tsconfig.json, systemjs.config.js.
1
8. From angular site copy the codes for these files and save.
2
9. Go to NPM and change your directory to E:\Angular2, and issue below command

npm install

3

10. Now create a folder named app, and create below files (Refer angular2 quick start).

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }   from './app.component';
@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
  selector: 'my-app',
  template: '<h1>My First Angular App</h1>'
})
export class AppComponent { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
<html>
  <head>
    <title>Angular QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">
    <!-- 1. Load libraries -->
     <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
  </head>
  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>
</html>

4

11. So everything is ready, now important step to configure TypeScript compiler.
12. Ctrl+Shift+P will open command pallette.
13. Type task and select Configure Task Runner
5
14. Selecet tsconfig.json
6
15. This will create a file named tasks.json, update args section as shown in image (args:[])
7
16. Now click on build and configure launch.json file.
8
17. Update program section as shown.
9
18. Run the app now

Posted in Uncategorized | Tagged: , | Leave a Comment »

Write ECMAScript 6 (JavaScript 6) in VS Code.

Posted by vivekcek on October 9, 2016

In this post i will show you how to write and debug EcmaScript6 in VS Code.

1. Install NodeJs (Check the configure path option during install).
2. Install VS Code.
3. Restart your machine/Inspect path has been set for NodeJs.
4. Start VS Code.
5. From menu select Open Folder(File->Open Folder).
1

6. Create a New Folder.
2

7. Create a new file named “jsconfig.json” and paste below code.

{
    "compilerOptions": {
        "target": "ES6"
    }
}

3

8. Now create a javascript file Hello.js and paste below code.

class Car {
    constructor(make) { //constructors!
        this.make = make;
      this.currentSpeed = 25;
    }

    printCurrentSpeed(){
          console.log(this.make + ' is going ' + this.currentSpeed + ' mph.');
    }
}

class RaceCar extends Car { //inheritance
    constructor(make, topSpeed) {
        super(make); //call the parent constructor with super
        this.topSpeed = topSpeed;
    }

    goFast(){
          this.currentSpeed = this.topSpeed;
    }
}

let stang = new RaceCar('Mustang', 150);

stang.printCurrentSpeed();
stang.goFast();
stang.printCurrentSpeed();

4

9. From menu select Debug (View->Debug).

5

10. Click on green start debug button, which will ask to configure the settings.
Select NodeJs

6

11. When we select NodeJs,VS Code will open launch.json. Update it with our file name Hello.js

7

12 Agin click on that Green Play button and enjoy.

8

Posted in javascript | Tagged: , , , | Leave a Comment »

Restart Amazon AWS EC2 instance from PowerShell

Posted by vivekcek on July 25, 2016

In this post i will show you how can we restart a windows ec2 instance by using powershell.

1. Go to your account, select Security Credentials and create an AWS access key and secret key.

2

3

2. Open Power-Shell for AWS, which will ask your AWS Access key and Secret Key.

1

3. If Power-Shell not prompted for keys go to “C:\Users\Administrator\AppData\Local\AWSToolkit” and delete the Registered Account Jsonfile and open Power-Shell again.

4. Now get your instance id by using this url from EC2 instance

http://169.254.169.254/latest/meta-data/instance-id

5. Now from powershell give below command

Restart-EC2Instance -InstanceId i-12345678

Posted in Amazon AWS | Tagged: , , , | Leave a Comment »

C# Different Types Of Heap Memory

Posted by vivekcek on July 10, 2016

You might be heard that, reference objects are stored in heap:).
Do you know how many types of heaps are available in CLR.
Do you know that, you can pass data to another app domain with out marshaling:)

These all are .NET CLR implementation details, know to Bill Gates:)

You can explore, if you want to know more, Later i will post about memory management in CLR.

For the time being have a look at the types of heap’s below.

Loader Heap: contains CLR structures and the type system
High Frequency Heap: statics, MethodTables, FieldDescs, interface map
Low Frequency Heap: EEClass, ClassLoader and lookup tables
Stub Heap: stubs for CAS, COM wrappers, PInvoke
Large Object Heap: memory allocations that require more than 85k bytes
GC Heap: user allocated heap memory private to the app
JIT Code Heap: memory allocated by mscoreee (Execution Engine) and the JIT compiler for managed code
Process/Base Heap: interop/unmanaged allocations, native memory, etc

Usually, the “heaps” that most people refer to or know about are the “GC Heap” and the LOH.
GC Heap is shared among app-domains and GC will work only on that.

Capture

Posted in c#.net | Tagged: , , , , , , , , | Leave a Comment »