Tag Archives: azure

Application Insights Intrumentation Key in Web.config

When using Azure Application Insights in ASP.Net application, by default, Visual Studio insert IntrumentationKey in ApplicationInsights.config.

To allow multiple environments tracking, move IntrumentationKey to Web.config by following this steps:

  1. Remove IntrumentationKey from ApplicationInsights.config. If you have MVC application, don’t forget to modify ApplicationInsights’ script (usually in View\Shared\_Layout.cshtml), replace:

    {instrumentationKey:"your instrumentation key"}


  2. Add new app settings for IntrumentationKey in Web.config under <appSettings>

    <add key="InstrumentationKey" value="your instrumentation key" />
  3. In Global.asax.cs, Application_Start() method, add:

    Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = System.Web.Configuration.WebConfigurationManager.AppSettings["InstrumentationKey"];

That’s it for the configuration changes. Everything else is the same including tracking custom event or page view.
With this configuration, you will be able to define InstrumentationKey in Release management for each environments.

1 Comment

Posted by on May 18, 2018 in General


Tags: , , , , , ,

Configuring Azure Application Insights for Angular 2+ App

1. Obtain InstrumentationKey from Azure.
-Create new Application Insights resource.


-Enter Name, Application Type (choose General for Angular app) and choose Resource Group.

-Wait for Azure to finish creating the resource.
-Go to the resource detail, by clicking name of the resource, select Overview menu and expand Essentials information to get the Instrumentation Key.

2. In Angular app, install applicationinsights-js package.

npm install applicationinsights-js --save --save-dev

3. In Angular component you want to track, add import statement and call AppInsights.downloadAndSetup() method in the constructor.
Normally, we only need to call AppInsights.downloadAndSetup() once in entire application lifecycle so it make sense to put this in app.component.ts.

4. To track each telemetry, call available methods in AppInsights class. In the example below, we track page view after the component initialized.

import {AppInsights} from 'applicationinsights-js';

	selector: 'test-app',
	templateUrl: 'test-app.component.html',
	styleUrls: ['test-app.component.scss']
export class TestAppComponent {
	constructor() {
		// Download and setup Application Insights
		AppInsights.downloadAndSetup({instrumentationKey: 'xxxx-xxxx-xxxx-xxxx'});

	public Init(): void {
		// Example of how to track page view

For more details on what Application Insights can track and methods to call, please see:

After everything is configured, browse your application for few minutes and let Azure Application Insights does its magic. You should be able to go back to Azure portal and see some activities, like this:

Leave a comment

Posted by on May 18, 2018 in General


Tags: , , ,

Conceal Sensitive Information with Azure Role-Based Access Control (RBAC)

Use Role-Based Access Control to Hide Access to Configurations, Connection Strings, Account Keys and Certificates

Access to Azure services can be defined in a more granular level. This is useful when you want to grant access to certain services without revealing sensitive information, such as account keys, connection strings or certificates.

RBAC Custom Roles

This can be achieved by defining Custom Roles in RBAC. Built-in roles is not going to be sufficient.

For example, we could restrict access to Azure Cloud Service ‘s Configurations and Certificates below:


  "Name": "Dev Ops",
  "Id": "<some_guid>",
  "IsCustom": true,
  "Description": "Dev Ops role.",
  "Actions": [
  "NotActions": [
  "AssignableScopes": [

What restrict users access to the configurations and certificates are the resource provider operations in NotActions.

What Resource Provider Needed for Azure Service?

In the example above, I use Azure Cloud Service as an example and the resource provider for Azure Cloud Service is Microsoft.ClassicCompute.

You can find out what resource provider used in an Azure Service from the URL. For example, this is URL for Azure Cloud Service.


The part where it says Microsoft.ClassicCompute is what tells you which resource provider to use.


The challenge is to find resource provider operations to suit your needs.

For more information on how to create custom roles, available built-in roles and list of resource provider operations, see the links in References.


Leave a comment

Posted by on April 24, 2018 in General


Tags: ,

Azure Batch Deployment and ‘msshrtmi’ Assembly

Azure Batch Deployment Issue

During Azure Batch deployment, I got the following error.

[22:32:51][Step 2/2] Unhandled Exception: System.AggregateException: One or more errors occurred. ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "SomeNamespace.SomeTask", name = "(none)".
[22:32:51][Step 2/2] Exception occurred while: Calling constructor SomeNamespace.Data.Repositories.SomeRepository(SomeNamespace.Common.Repositories.IStorageRepository storageRepository, SomeNamespace.Common.ILoggingService logger).
[22:32:51][Step 2/2] Exception is: TypeInitializationException - The type initializer for 'Microsoft.ServiceRuntime.RoleEnvironment' threw an exception.
[22:32:51][Step 2/2] -----------------------------------------------
[22:32:51][Step 2/2] At the time of the exception, the container was:
[22:32:51][Step 2/2]
[22:32:51][Step 2/2]   Resolving SomeNamespace.SomeTask,(none)
[22:32:51][Step 2/2]   Resolving parameter "analyticsService" of constructor SomeNamespace.SomeTask(SomeNamespace.Common.IConfigurationManager config, SomeNamespace.Services.ServiceLayer.ISomeServiceA chainService, SomeNamespace.Services.ServiceLayer.ISomeServiceB siteService, SomeNamespace.Common.ILoggingService logger, SomeNamespace.Services.ServiceLayer.ISomeServiceC analyticsService)
[22:32:51][Step 2/2]     Resolving SomeNamespace.Services.ServiceLayer.SomeServiceC,(none) (mapped from SomeNamespace.Services.ServiceLayer.ISomeServiceC, (none))
[22:32:51][Step 2/2]         Resolving SomeNamespace.Services.ServiceLayer.SomeServiceI,(none) (mapped from SomeNamespace.Services.ServiceLayer.ISomeServiceI, (none))
[22:32:51][Step 2/2]         Resolving parameter "someRepository" of constructor SomeNamespace.Services.ServiceLayer.SomeServiceI(SomeNamespace.Common.Repositories.ISomeRepository someRepository)
[22:32:51][Step 2/2]           Resolving SomeNamespace.Data..Repositories.SomeRepository,(none) (mapped from SomeNamespace.Common.Repositories.ISomeRepository, (none))
[22:32:51][Step 2/2]           Calling constructor SomeNamespace.Data..Repositories.SomeRepository(SomeNamespace.Common.Repositories.ISomeRepositoryA someRepositoryA, SomeNamespace.Common.ISomeServiceH someServiceH)
[22:32:51][Step 2/2]  ---> System.TypeInitializationException: The type initializer for 'Microsoft..ServiceRuntime.RoleEnvironment' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'msshrtmi, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
[22:32:51][Step 2/2]    at Microsoft..ServiceRuntime.RoleEnvironment.InitializeEnvironment()
[22:32:51][Step 2/2]    at Microsoft..ServiceRuntime.RoleEnvironment..cctor()
[22:32:51][Step 2/2]    --- End of inner exception stack trace ---
[22:32:51][Step 2/2]    at Microsoft..ServiceRuntime.RoleEnvironment.get_IsAvailable()
[22:32:51][Step 2/2]    at SomeNamespace.Data..Repositories.SomeRepository..ctor(ISomeRepositoryA someRepositoryA, ISomeServiceH someServiceH, Boolean forceLocal) in F:\TeamCity\buildAgent\work\2b474eac7536416f\SomeNamespace.Data.\Repositories\SomeRepository.cs:line 64
[22:32:51][Step 2/2]    at lambda_method(Closure , IBuilderContext )
[22:32:51][Step 2/2]    at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)
[22:32:51][Step 2/2]    at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
[22:32:51][Step 2/2]    at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
[22:32:51][Step 2/2]    at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
[22:32:51][Step 2/2]    at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)
[22:32:51][Step 2/2]    at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)
[22:32:51][Step 2/2]    at lambda_method(Closure , IBuilderContext )

At a glance, it seems like an issue with Unity IOC container. But when I look closer, it’s actually missing ‘msshrtmi‘ assembly as you can see in the following error message:

System.IO.FileNotFoundException: Could not load file or assembly ‘msshrtmi, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35’

Root Cause

This error happens in the build server, but it’s not happening in local machine.

As a comparision, the build server does have Azure SDK installed, the same version used with the Azure Batch application. It also has .NET Framework installed, but not the SDK.

The local machine, on the other hand, has Visual Studio installed, Azure SDK and .NET Framework, but not .NET Framework SDK.

The exception is caused by build server doesn’t have installed Azure SDK library in its Global Assembly Cache (GAC). In local machine, the installation of Visual Studio put Azure SDK into GAC.


It’s as easy as adding msshrtmi assembly to Global Assembly Cache (GAC) in the build server. See here for adding GAC.

Leave a comment

Posted by on January 30, 2018 in General


Tags: , , ,

Azure Storage Blob Listing Query with Search Pattern

Azure Storage blob is not behaving like files and folders in your local computer, albeit looking like one. So, there’s no support for search pattern, such as *.txt

The solution is to write your own search pattern.


public static String WildCardToRegular(String value) {
    return "^" + Regex.Escape(value).Replace("\\*", ".*") + "$";

Then, using it in Listing the blob:

var blobList = await container.ListBlobsSegmentedAsync(blobFilePath, true, BlobListingDetails.None, 1000, token, null, null);
var items = blobList.Results.Select(x => x as CloudBlockBlob);

// Filter items by search pattern, if specify
if (!string.IsNullOrEmpty(searchPattern))
    items = items.Select(i =>
        var filename = Path.GetFileName(i.Name);
        if (Regex.IsMatch(filename, WildCardToRegular(searchPattern), RegexOptions.IgnoreCase))
            return i;
	return null;

Leave a comment

Posted by on September 9, 2017 in General


Tags: ,

Azure Active Directory IDs

Setting up or accessing Azure Active Directory require many IDs. Below is how to find them:

Tenant ID
Azure Portal > Azure AD > Domain names
Azure Portal > Azure AD > Properties > Directory ID


Client ID / Application ID / Audience
Azure Portal > Azure AD > App registrations > ApplicationId

Client Secret / Application Secret
Azure Portal > Azure AD > App registrations > choose the application > Keys
Keys are hidden and only visible the first time you add them.

Redirect URIs or Reply URLs
Azure Portal > Azure AD > App registrations > choose the application > Settings > Redirect URIs or Reply URLs

Azure Portal > Azure AD > App registrations
SQL database:

Azure AD

Leave a comment

Posted by on August 17, 2017 in General


Tags: ,

Azure Key Vault PowerShell Commands


# List all Key vaults

# Get a key given the secret name.
Get-AzureKeyVaultKey -VaultName "vault-name" -Name "secret-name"

# List all secrets
Get-AzureKeyVaultSecret -VaultName "vault-name"

# Get a secret
Get-AzureKeyVaultSecret -VaultName "vault-name" -Name "secret-name"

Azure Get Started

Leave a comment

Posted by on August 17, 2017 in General


Tags: , ,

%d bloggers like this: