Wednesday, October 30, 2013

Debugging a Windows Services

We cannot run Windows Services, directly like running a Windows or Web application; we need to install the Windows Service in the service console for it to operate. Hence we cannot debug windows services directly, use the following procedure to debug a windows service. 

Using ServiceController to Manage a Windows Service

We can use the ServiceController component to Start/Stop a service from an application without having to open the services panel. This can be used in Admin / Dashboard applications which needs to monitor and manage a set of services in a server.

The ServiceController is part of the “ System.ServiceProcess” assembly and has methods to start/stop or perform other operations on a Windows Service. In this post we shall see on how to use the ServiceController to manage the state of our Windows Service “NotificationServices”

Windows Services with custom User account

A Windows service runs under a specific account, the Account property needs to be set in the ServiceProcessInstaller. The property can take any of the below 4 values.

LocalService
NetworkService
LocalSystem
User

Creating a Deployment Project to Install a Windows Service

In the post Installing and Uninstalling a Windows Service, we have seen on how to Install and Un-Install a windows service using the InstallUtil tool, however when you want your service to be packaged for deployment into end user computer this approach might not work as the end user will not be aware of how to run the commands to install the service, in these cases we can create a deployment project which will automatically deploy the service to the computer.
 
In this post we shall see on how to create a Setup Project which will create a setup file to deploy the service. 
1.    Open the Windows Service Project Solution in VS.Net
2.    Add a new project of type “Setup Project” to the solution.
3.    I will name the setup project as NotificationServiceSetup. 
4.    You can set the Properties like Company, Author etc in the Set up project Properties.
5.    Once the Setup Project is created, right click on the project and select Add -> Project Output 
6.    Select the NotificationService project in the Projects dropdown and select, “Primary output” in the list box below. Click OK 
7.    Once again right click on the Setup Project and select View -> Custom Actions
8.    In the Custom Action panel, right click on Custom Actions and select “Add Custom Action” 
9.    Select “Application Folder” click OK 
10. Select “primary output from NotificationService (Active)”, click OK
11. Notice that the “Primary output from NotificationService” is added under Install , Commit, Rollback and Uninstall options.
12. Right click on the NotificationServiceSetup project and build the project
13. Once the build is completed, the setup file to install the service is created in the /Bin folder of the project. 
14. That’s it the setup project is ready, just double click on the .msi file and follow the instructions of the setup Wizard to install the Project.
15. Once the installation completes the NotificationService will get added to the list of services on the computer.

Installing and Uninstalling a Windows Service

Windows services can be installed and un-installed from the list of services, using the Utility installutil.exe, this utility comes with the framework and can be used to configure services. 
Follow the below instructions to use the installutil.exe utility to Install / UnInstall a windows service.

Adding Service Installers to the Windows Service

We have created the windows service and implemented the Start and Stop methods of the service, but for the service to start working it needs to be installed to the list of Window services in the server, to do that we need to create a Service Installer which can install / uninstall the service.

In this post we shall create an installer for a Windows Service.

Logging errors to Windows Event Log

A windows service, gets added to the list of services and gets executed automatically, hence we need a good set of logs to identify issues with the service. We shall see on how to write appropriate logs into the Windows event log for each action performed in the Windows service.

We shall add a new utility class LogHelper which will log the events to the Windows Event Log and call the methods in this helper class to log the status of the windows service.

Add a new class called LogHelper.cs and add the following logging methods to the class.

Setting Timer for a Windows Service

A windows service is supposed to perform a set of operation repeated after a pre-defined interval of time. For the service to trigger the operation after a pre-defined interval we need to define a timer and call the implementation method when the timer elapses.

We will create a times and define the interval when the service gets started, there after whenever a time elapses the timers Elapsed event gets triggered, we can bind our implementation method to this event so that the method gets called every time the time elapses.

We will use the same windows service which we created in the post Creating a Basic Windows Service
 and add a timer to the service.

Creating a Basic Windows Service

Microsoft Windows Services, enable us to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface.


Visual Studio.Net provides us pre-defined templates which can be used to create Windows Services, in the following example we shall see on how to create a basic windows service.

Windows Service Events

A Windows Services class should inherit from the System.ServiceProcess.ServiceBase class. We should override methods of this class and define functionality for them to determine how your service behaves.

The following are the main methods which should be overwritten to implement the functionality of a Windows service.

What is a Windows Service?

Microsoft Windows Services, formerly known as NT services, enable you to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer.

The compiled executable file that a service application project creates must be installed on the server before the project can function in a meaningful way

Windows Service applications are based on a class that inherits from

Sunday, October 20, 2013

Distributed Caching

Assembly Overview

PerfView Overview

Performance Monitor Overview

CLR Profiler Overview

Garbage Collection Overview

.Net Memory Management Overview

Object Oriented Concepts in C#

Interface Overview

Class Modifiers in C#

Access Modifiers

Properties Overview

OOPs Overview

Tuesday, July 2, 2013

AppFabric Cache – Installation Modes

The AppFabric Caching service installation can be done in 3 different modes, each mode has its own function, together the 3 modes form a complete distributed caching system which can effectively handle caching in a distributed environment. AppFabric takes care of replicating the cache defined in one cache server to the other servers, so that the cache data placed in one of the servers in the cluster can be accessed from the other servers in the cache cluster.

The following are the modes in which the AppFabric caching can be installed.

AppFabric – Pre Requisites

AppFabric is available for free and can be downloaded from the following Microsoft’s site


The following are the pre requisites for installing AppFabric caching.

AppFabric Caching Cluster Configuration

A AppFabric cache cluster includes a number of cache host servers which jointly form a cache cluster, the configuration details of the cache host servers should be placed in a common location, each time a cache host starts it gets the basic configuration information and starts operating.

AppFabric provides the following options to store configuration information.

AppFabric Caching Architecture

AppFabric Caching can handle multiple cache servers running parallel, the individual cache servers form a cluster, the client web servers see the cache as a single cache cluster and not as individual cache servers. AppFabric caching takes care of synchronizing the cache handling between the various cache servers.

Each cache server contains a Cache Host windows service, which takes care of the managing the cache and replicating cache data across the various servers in the cache cluster.


The following diagram shows the AppFabric caching architecture in a cache cluster environment.

Monday, June 24, 2013

What is AppFabric Caching

AppFabric provides a distributed caching platform, used to develop highly scalable applications which require a cache to provide performance optimization. The caching platform can be scaled out by adding multiple cache servers to the architecture.

AppFabric caching platform takes care of sharing the cached objects across the cache servers automatically, and makes sure that the cache objects added to one of the instances is made available in all other instances.

Advantages of Distributed Caching

In general Caching improves performance of an application by storing frequently used information in the cache memory which is easily accessible when compared to the standard memory. Simple caching techniques can be used for smaller applications which get executed in a single server, however when it comes to larger applications which cannot run in a single server the simple caching techniques are not sufficient enough.

Larger applications required multiple servers, running in parallel to handle the huge volume of requests, hence we need Distributed Caching for large applications. The following are the advantages of using Distributed Caching.


Distributed Cache

Data is an important part of any application, in a conventions web application architecture which does not use any cache, data can be stored in 2 ways, using the database or using sessions, but both these options have their own limitations.

The database can store any amount of data, but fetching large amount of data from the database and displaying in the UI will bring performance of the application. Sessions are quick but there is a limitation on the amount of data that can be stored in session, adding more data to the session, consumes more memory and hence brings down the application performance.

Limitations of Asp.Net Cache

Asp.Net cache is a good approach to improve the performance of an application, frequently used data can be stored in the cache and re-used to avoid unwanted database hits. However Asp.Net cache has its limitations.

The following are the limitation of Asp.Net Cache.

Limitations of Asp.Net Cache

Asp.Net cache is a good approach to improve the performance of an application, frequently used data can be stored in the cache and re-used to avoid unwanted database hits. However Asp.Net cache has its limitations.

The following are the limitation of Asp.Net Cache.

Asp.Net Cache

Asp.Net provides a caching mechanism which can be used to store data in a key value format. The data stored in the Asp.Net Cache can be retrieved at any time by using the key against which the value is stored.

The following example stores the list of countries is a Cache variable “cacheCountryList”.

What is a Cache?

A Cache refers to an intermediate storage location which can respond quickly to requests, thereby helping in improving the performance of the applications which makes use of the Cache.

In Hardware we refer to Cache memory as an intermediate memory which stores frequently used data and responds quickly when the processor requests the data. For example RAM (Random Access memory) is a intermediate cache memory used to store and respond to frequently used data. Data stored in the cache memory is retrieved quickly as compared to the data stored in the hard disk. Similarly a cache in the software world in an intermediate storage location used to provide quick response to users requests. 

Monday, June 17, 2013

Assemblies and Side-by-Side Execution

Let us consider that we have 2 applications A & B in a system, where A is an old application using an older version (say x1) of an assembly, not B is a new application and wants to use a new version (say x2) of the same assembly, how can this be achieved?

If the Assemblies are private assemblies then we don’t have an issue as the applications A & B will contain their own version of the assembly in their application folder and use the assemblies independently. But what is the assembly is a shared assembly, how can be maintain and use 2 different versions of the same assembly? This is where side-by-side execution comes into play.

What is a Global Assembly Cache (GAC)

The Global Assembly Cache (GAC) is a common folder where the shared assemblies are placed. The GAC folder is placed in one of the following folders.

X:/Windows/assembly
(or)
X:/Winnt/assembly

Where X: is the driver where the .Net framework in installed, it might vary from machine to machine based on where the .Net Framework is installed.

For an Assembly to be placed in the GAC, it should be signed with a strong name key, generated using the sn.exe utility. Once the assembly is signed with the key and is ready for deployment, it can be deployed in the GAC in one of the following ways.

Delay signing an Assembly

Delay signing is the process of delaying the actual signing process, if we use the normal signing process then the details of the private and public key will be available to all the developers who are involved in developing the Assembly.

The organization might want to restrict access to the private key only to a set of key individuals and not to all, delay signing process can be used to achieve this.

Delay signing can be achieved by specifying the AssemblyDelaySignAttribute attribute to true in the assemblyInfo file, on doing so the runtime will not sign the Assembly, but will reserve space in the PE file for the strong name which can be specified later.

Signing an Assembly

Signing an Assembly requires generating a key file and mapping it to a Shared assembly. There are 2 types of assemblies, private and shared assemblies. Private assemblies are private to the application and are placed in the same folder as the application files. Shared assemblies are those which are shared across multiple applications in the same machine.

Before we could sign an Assembly we need to generate a key file which will be used for signing. To sign the assembly we can use the .Net Utility sn.exe

sn.exe -k MyPublicPrivateKeyFile.snk

The key file contains a combination of private and public keys which will later be used by the publisher and the consumer to the assembly to establish a secured relationship.

Once the assembly key file is generated, it can be linked to the assembly in 2 ways.

Assembly Versioning

Assembly version details are stored in a file called AssemblyInfo, this file gets automatically created when you create a new project in Visual Studio, the version information of the assembly is specified in this file.

A .Net Assembly version is a four part name, and contains the following 

Major Version
Minor Version
Build Version
Release Version.

The four parts of the Assembly version are organized as follows.

Dynamic Assemblies

As the name suggests, Dynamic Assemblies are those which are created dynamically at runtime. They are not stored in any file before execution, after execution we can optionally store them in files. The System.Reflection.Emit namespace contains classes that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) and optionally generate a PE file on disk.

The Reflection.Emit namespace provides builder classes which can be used to create assemblies at runtime, these classes allow you to define classes and methods at runtime. The AssemblyBuilder is the core class which allows you to create assemblies at runtime.

AssemblyBuilder assemblyBuilder = curAppDomain.DefineDynamicAssembly(
assemblyName,AssemblyBuilderAccess.Save);

Static Assembly

Static Assemblies are the normal assemblies which we create in .Net, they have physical class files and other related resource files like, images, sound files etc. These physical files are compiled into a single assembly file.

Static Assemblies can be private or public based on the type of target applications which use these assemblies. Private assemblies are used by individual applications, the assembly files are placed in the same folder/sub-folder of the application files. Shared assemblies are placed in the GAC and are shared across multiple applications in the system.

Satellite Assembly

Satellite Assemblies are used to create multilingual applications which needs to display information to the end users in multiple languages. Satellite assemblies do not contain any executable code, they contain only culture specific resource files which can be used by the main assembly to display details to the end user in their preferred language.

Language specific resources should be created and compiled into Satellite Assemblies using the Assembly Linker tool Al.exe.

Shared Assembly

As the name suggests a Public Assembly or Shared Assembly is one which is shared with various applications in the same system, hence these assembly files cannot be placed in the individual application folders, and these assembly files are placed in a common location in the machine called the Global Assembly Cache.

Public Assembly or Shared Assembly should have specific version information. All public / shared assemblies are placed in a common folder in the machine called the Global Assembly Cache. The GAC is usually located in the path C:\Windows\Assembly

Public Assembly

As the name suggests a Public Assembly or Shared Assembly is one which is shared with various applications in the same system, hence these assembly files cannot be placed in the individual application folders, and these assembly files are placed in a common location in the machine called the Global Assembly Cache.

Public Assembly or Shared Assembly should have specific version information. All public / shared assemblies are placed in a common folder in the machine called the Global Assembly Cache. The GAC is usually located in the path C:\Windows\Assembly

Private Assembly

A Private assembly is one which is used by an individual application; it is private to that application and is not shared with any other application in the system. Private assemblies are usually placed in the same folder or in a sub-folder where the application files reside.

Private Assemblies do not have specific version information, since the parent application is just going to refer the latest assembly file in its folder/sub-folder.

What are the types of Assemblies in .Net?

Assemblies can be categorized based on various factors like, based on how they are shared between applications, based on their physically state, based on how they are packed into files etc.

The following are the various types of assemblies based on the different classifications.

What does an Assembly contain?

An assembly is the core building block of .Net, assemblies are self-contained, they contain all the information required to process the assembly. Any .Net assembly contains the following details.

1. The MSIL code of the assembly
2. Assembly Manifest
3. Type Metadata
4. Resources

Now let us see these in detail.

What is an Assembly in .Net

An assembly is a collection of classes, namespaces and related information which together form a complete application. Every application which is developed in .Net is stored in an assembly.

Assembly can be either a self-executable file, with an extension of .EXE, or a library file with an extension of .DLL. Let us see more in details about both these types of assemblies.

Sunday, June 16, 2013

Performance Monitor – Detailed Report View

The Windows Performance Monitor is a performance monitoring tool, which comes along with the windows operating system. The Performance Monitor tool provides various performance counters and factors which can be used to analyze and determine the performance of a system on a whole or that of a specific process over a specific period of me.

The Performance Monitor displays the details captured by the counter is different formats like Line Graph, Histogram and a Detailed Report view.

To change the output view to detailed report view, click on the report menu and select the Report option as below.

Performance Monitor - Histogram Report View

The Windows Performance Monitor is a performance monitoring tool, which comes along with the windows operating system. The Performance Monitor tool provides various performance counters and factors which can be used to analyze and determine the performance of a system on a whole or that of a specific process over a specific period of me.

The Performance Monitor displays the details captured by the counter is different formats like Line Graph, Histogram and a Detailed Report view.

To change the output view to Histogram view, click on the report menu and select the Histogram bar as below.


Performance Monitor – Windows Application

The Windows Performance Monitor is a general purpose performance monitoring tool which can be used to monitor performance of windows and web applications. The tool can be customized to monitor specific parameters like Memory Utilization, CPU Utilization, Disk I/O etc of the system at any given point of time or over a specific time interval.

In this post we shall see on how to use the Performance Monitor tool monitor performance of a Windows application based on memory utilization of the application. This is just one of the counters, there a hundreds of such counters which can be used to measure various parameters of the application.

To measure the memory utilization of the application, use the following steps to configure the Performance Counter.

Performance Monitor – Asp.Net Requests profiling

The Windows Performance Monitor is a general purpose performance monitoring tool which can be used to monitor performance of windows and web applications. The tool can be customized to monitor specific parameters like Memory Utilization, CPU Utilization, Disk I/O etc of the system at any given point of time or over a specific time interval.

In this post we shall see on how to use the Performance Monitor tool monitor performance of an Asp.Net application based on the number of requests processed per second. This is just one of the counters, there a hundreds of such counters which can be used to measure various parameters of an Asp.Net application.

To measure the number of request processed by Asp.Net per second, use the following steps to configure the Performance Counter.

Advantages of Performance Monitor

The Windows Performance Monitor is the first and basic Performance Monitoring tool used by any developer or system admin, since is it part of the Windows operating system, needs to download or installation it is per-build when you install Windows.

The following are the advantages of the Performance Monitor Tool

What is a Performance Monitor?

The Windows Performance Monitor is a performance monitoring tool, which come along with the windows operating system. The Performance Monitor tool provides various performance counters and factors which can be used to analyze and determine the performance of a system on a whole or that of a specific process over a specific period of me.

Performance Monitor can be started by typing the command PerfMon in the Run window. The tool can also be opened from the control panel as follows.

PerfView – JIT Status Report

The PerfView is a performance analysis tool from Microsoft, the tool can be used to analyze application performance of both Windows and Web based applications. In this post we shall see on how to analyze the data about the runtime JIT compiler, to determine the code and utilization of the application. This report provides details about the size of the JIT and Native code for every function / method in the application. This will help us to determine any oversized methods and fix it by breaking them appropriately.

To analyze the JIT Status of an application follow the below steps.

PerfView – GC Heap Allocation Report

The PerfView is a performance analysis tool from Microsoft, the tool can be used to analyze application performance of both Windows and Web based applications. In this post we shall see on how to analyze the data about Heap, to determine the GC Heap Allocation of the application.

To analyze the GC Heap Allocation of an application follow the below steps.

PerfView – DISK I/O Stack Report

The PerfView is a performance analysis tool from Microsoft, the tool can be used to analyze application performance of both Windows and Web based applications. In this post we shall see on how to analyze the data about DISK I/O, to determine the Disk Utilization of the application.

To analyze the DISK I/O Utilization of an application follow the below steps.

PerfView – CPU Stack Report

The PerfView is a performance analysis tool from Microsoft, the tool can be used to analyze application performance of both Windows and Web based applications. In this post we shall see on how to analyze the data about CPU usage, to determine the CPU Utilization of the application.

To analyze the CPU Utilization of an application follow the below steps.

PerfView - Managed Stack Snapshot Report

The PerfView is a performance analysis tool from Microsoft, the tool can be used to analyze application performance of both Windows and Web based applications. In this post we shall see on how to analyze the data from the Managed Stack, to determine the memory utilization of the application.

To analyze the Memory utilization and Stack details of an application follow the below steps.

PerfView - Managed Heap Snapshot Report

The PerfView is a performance analysis tool from Microsoft, the tool can be used to analyze application performance of both Windows and Web based applications. In this post we shall see on how to analyze the data from the Managed Heap, to determine the memory utilization of the application.

To analyze the Memory Utilization and Heap details of an application follow the below steps.

Advantages of PerfView

PerfView is a performance analysis tool, developed by the Microsoft Performance Team to debug and analyze memory and CPU related performance issues.

PerfView has the following advantages.

What is PerfView

PerfView is a performance analysis tool, developed by the Microsoft Performance Team to debug and analyze memory and CPU related performance issues.  It is a light weight component that makes sure that running the tool is not an overhead to the server or workstation.

PerfView tool is very handy, it does not need any installation and configurations to be done, you can just download and start working on it with a single click.

PerfView uses the following sources of information to do its analysis.

Friday, June 14, 2013

CLR Profiler – Object Allocation Report for a Web Application

The CLR Profiler provides various types of reports which help in determining the performance of the application and tuning it. The “Object Allocation” report shows the structure of the Managed heap, when the application was executing.

To view the “Object Allocation” report, use the following steps.

CLR Profiler – Object Aging report for an Asp.Net Web Application

The CLR Profiler provides various types of reports which help in determining the performance of the application and tuning it. The “Histogram by Age” report shows the object allocating aging report which shows the object allocation across time intervals.

To view the “Histogram by Age” report, use the following steps.

CLR Profiler – Analyzing Object Memory Allocation for a Web Application

The CLR Profiler provides various types of reports which help in determining the performance of the application and tuning it. The “Histogram Allocated Types” report shows the various types of objects which were created and allocated during the execution of the application. This report shows you on the share of object allocation to the Small Object Head and the Large Object Heap.

To view the “Histogram Allocated Types” report, use the following steps.

Using CLR profiler to Profile an Asp.Net Web Application

The CLR Profiler can be used to profiler your windows / web application. The CLR Profiler provides useful inputs in how the application is executed in the underlying runtime environment.

Before you profile your application using the CLR Profiler make sure that you are using the correct version of the CLR Profiler which matches with the version of the framework which was used to develop the application. Also make sure to select the format of the CLP Profiler 32/64 bit based on how you developed your application.

Once you selected the correct CLR Profiler, use the following steps to profile your web application.

CLR Profiler – Call Tree Report for a Windows Application

The CLR Profiler provides various types of reports which help in determining the performance of the application and tuning it. The “Call Tree” report shows the object allocations done to the Heap over a specific time period.

To view the “Call Tree” report, use the following steps.

CLR Profiler – Timeline Graph Report for a Windows Application

The CLR Profiler provides various types of reports which help in determining the performance of the application and tuning it. The “Timeline Graph” report shows the object allocations done to the Heap over a specific time period.

To view the “Timeline Graph” report, use the following steps.

CLR Profiler – Managed Heap Report for a Windows Application

The CLR Profiler provides various types of reports which help in determining the performance of the application and tuning it. The “Object by Address” report shows the structure of the Managed heap, when the application was executing.

To view the “Object by Address” report, use the following steps.

CLR Profiler – Object Aging report for a Windows Application

The CLR Profiler provides various types of reports which help in determining the performance of the application and tuning it. The “Histogram by Age” report shows the object allocating aging report which shows the object allocation across time intervals.

To view the “Histogram by Age” report, use the following steps.

CLR Profiler – Analyzing Object Memory Allocation for a Windows Application

The CLR Profiler provides various types of reports which help in determining the performance of the application and tuning it. The “Histogram Allocated Types” report shows the various types of objects which were created and allocated during the execution of the application. This report shows you on the share of object allocation to the Small Object Head and the Large Object Heap.

To view the “Histogram Allocated Types” report, use the following steps.

CLR Profiler error - Waiting for application to start common language runtime

Error:
When you try to analyze your application using the CLR Profiler, you might get the following error at times.

Waiting for application to start common language runtime

Cause:
The reason for this error could be one of the following.
1. The CLR Profiler is not compatible with the version of .Net Framework used by your application.
2. The CPR Profiler you choose 32/64 bit is not compatible with your Application

Resolution:

Using CLR profiler to Profile a Windows Application

The CLR Profiler can be used to profiler your windows / web application. The CLR Profiler provides useful inputs in how the application is executed in the underlying runtime environment.

Before you profile your application using the CLR Profiler make sure that you are using the correct version of the CLR Profiler which matches with the version of the framework which was used to develop the application. Also make sure to select the format of the CLP Profiler 32/64 bit based on how you developed your application.

Once you selected the correct CLR Profiler, use the following steps to profile your windows application.

Advantages of CLR Profiler

The CLR Profiler is a very handy tool especially when you are analyzing your application for performance issues. The CLR Profiler shows a clear picture of what is happening behind the scene when your application is running, it shows the state of the Managed Heap, the cycles run by Garbage Collection. This information will be very useful in identifying performance bottlenecks and memory related issues in your application.

The CLR Profiler is a free tool and is developed and distributed by the Microsoft Performance team, hence it integrates seamless with .Net Applications

The following are some of the advantages of using the CLR Profiler.

CLR Profiler for .Net 4.0

We have seen about the memory allocation in Stacks, Heaps, Garbage collections etc. in order to examine these things and find out memory leakages and other memory related issues, we need some kind of tools to see the status of Stacks, Heaps etc.

There are a number of free and paid tools available in the market which can analyze the state of the application, state of Stacks, Heaps, Garbage Collection etc. Microsoft provides a free tool “
CLR Profiler”, which allows developers to see the allocations made their managed application.

The
CLR provides the following information which will be very useful in analyzing the state of your applications, and investigation the causes behind issues like Memory Leakages etc.