Monday, November 28, 2011

How does object pooling and connection pooling differ?

In Object pooling, you can control the number of connections.

In connection pooling, you can control the maximum number reached.

When using connection pooling, if there is nothing in the pool, a connection is created since the creation is on the same thread.

In object pooling, the pool decides the creation of an object depending on whether the maximum is reached which in case if it is, the next available object is returned. However, this could increase the time complexity if the object is heavy

What is an object pool in .NET?

An object pool is a container of objects that holds a list of other objects that are ready to be used.

It keeps track of:

•Objects that are currently in use
•The number of objects the pool holds
•Whether this number should be increased
The request for the creation of an object is served by allocating an object from the pool.

This reduces the overhead of creating and re-creating objects each time an object creation is required.

What is the JIT? What is NGEN? What are limitations and benefits of each?

Just In Time Compiler compiles the code just before it is run.
Due to this the code takes longer time to start however it is more efficient compilation since the compiler has more information about the target system.

NGen is used to pre-just in time code which yields faster startup time but the compiler produces less efficient code because it has less information.

Explain the use of static members with example using C#.NET.

Static members are not associated with a particular instance of any class.
They need to be qualified with the class name to be called.
Since they are not associated with object instances, they do not have access to non-static members.
i.e.: "this" cannot be used, which represents the current object instance.

What are generics in C#.NET?

Generic types to maximize code reuse, type safety, and performance.
They can be used to create collection classes.
Generic collection classes in the System.Collections.Generic namespace should be used instead of classes such as ArrayList in the System.Collections namespace.

The classes and the methods can treat the values of different types uniformly with the use if generics.

The usage of generics is advantageous as:

•They facilitate type safety
•They facilitate improved performance
•They facilitate reduced code
•They promote the usage of parameterized types
•The CLR compiles and stores information related to the generic types when they are instantiated. (The generic type instance refers to the location in memory of the reference type to which it is bound for all the instances of the generic type.)

Define most commonly used interfaces in .Net Framework

IComparable: It is implemented by types for ordering and sorting. The implementation types must implement one single method i.e. CompareTo.

IDisposable:This is implemented to manage release of unmanaged resources from memory. The garbage collector acts on its own and hence the Dispose method is used to call the garbage collector to free unmanaged resources through this interface.

IConvertible: It is implemented to convert value of the implementing type into another CLR compatible type. If the conversion fails then an invalidcastexception is thrown.

ICloneable: Allows creating objects of a class having same values as another instance using the Clone method.

IEquatable:Implemented to create type specific methods to know the equality between various objects.

IFormattable:Implemented to convert object into string representations. Objects can define their own specific string representation through this interface’s ToString() method.

How do you create a resource-only assembly?

Resources are nonexecutable data in an application and the data can be updated without recompiling application. Resource assemblies can be created as follows:
Add resource files to an empty project.
Built the project.
The resource will get compiled into assembly.

What does an assembly contain?

An assembly contains following information:
Assembly manifest: Information about the assembly.
Type metadata: Information about the types.
IL Code
Resource files.

An assembly manifest contains the following information:

Identity of the assembly
Types and resources
Files
Security permissions

Define .Net Assembly.

It is a primary unit of deployment in a Microsoft .NET Framework application. It is called as building block of an application which provides all required execution information to common language runtime.

An assembly perform following functions:
It contains IL code that gets executed by common language runtime.
It forms a security boundary.
An assembly is the unit at which permissions are requested and granted.
It ensures type safety by establishing name scope for types at the runtime.
It contains version information.
It allows side-by-side execution of multiple versions of same assembly.

Assemblies can be static or dynamic.
Static assemblies are created when the program is compiled using .Net compiler. It exists as PE file either in .exe or .dll. However, dynamic assemblies are created at runtime and run from the memory without getting saved on the disk.

Which .exe performs conversion of .txt / .restext files to .resources / .resx files and vice-versa ?

Resgen.Exe

Which .exe performs conversion of .txt / .restext files to .resources / .resx files and vice-versa ?

Resgen.Exe

Thursday, November 24, 2011

Page.AsyncTimeout Property

'<'asp:Page AsyncTimeout="TimeSpan" '/>'
A TimeSpan that contains the allowed time interval for completion of the asynchronous task. The default time interval is 45 seconds.

page.ClientQueryString

The ClientQueryString property contains the query string portion of the URL requested by the browser.
For example, if the requested URL is "http://www.xyz.com/default.aspx?id=100", the ClientQueryString property will contain "id=100".

supprted in following versions:
4, 3.5, 3.0, 2.0

How do I use the aspnet_merge.exe command line to merge precompiled assemblies?

aspnet_merge.exe d:\PrecompiledWebSiteDemo -w YourAssemblyName.dll

What is the difference between the updatable and without non-updatable option while precompiling ASP.NET website?

When precompiling the ASP.NET website, there is an option "Allow this precompiled site to be updatable" in Publish Utility using Visual Web Developer or the –u option while using aspnet_compiler command line. If we enable "Allow this precompiled site to be updatable" option, it doesn’t mean that we can change everything on the precompiled website. Let’s see what the difference is while the "Allow this precompiled site to be updatable" option is enabled or not:
Precompiling without the updatable option checked:

If this option is disabled, the compiler will not only compile source code into assembly, but also produces assemblies from markup of page, resource files, etc. In the output, compiler removes all the source code and markup from page which will leave a placeholder with empty content. To change the website except for its configuration, we need to recompile the website and redeploy the layout.

Precompiling with updatable option checked:

With this option enabled, the compiler will not remove the markup of page and so we can make limited changes, such as change the arrangement of controls, colors, fonts, and other appearance aspects of pages. Additionally, when the page is requested, ASP.NET will perform further compilation in order to create output from the markup.

What is smart navigation?

Smart Navigation is obsolete in .NET 2.0. It works with 1.1 & versions before it. The SetFocus and MaintainScrollPositionOnPostBack are used instead

Smart Navigation basically enhances a web pages' performance by doing the following:

* It eliminates the flash caused during navigation
* It persists element focus during postbacks
* It persists scroll position during postbacks between pages
* It retains the lasts page information in the history of the browser

I suggest not to use SmartNavigation because it does not work with many browsers.
From .Net2.0 versions Page.MaintainScrollPositionOnPostBack is used in the place of smartnavigation

Wednesday, November 23, 2011

How to use more than one web.config in asp.net

By default when a webspplication in created we get root folder web.config
set the appsettings tag as follows
(Iam removing the start and end tag symbols due to publishing problems,instead i use '(' and ')' in place of '<' and '>'.Please replace while you test this code)
(appsettings)
(add key="rootfolder" value="Root Folder web.config"/)
(add key="test1" value="Testing the application for root folder web.config"/)
(/appSettings )

in Default.aspx
(html xmlns="http://www.w3.org/1999/xhtml")
(head runat="server")
(title)RootFolder web.config(/title)
(/head)
(body)
(form id="form1" runat="server")
(div)
(b)
Root Folder values
(/b)
(/div)
(br /)
(div)
AppKey value:
(b)
(asp:Label ID="lbltxt" runat="server" Text="(%$appSettings:rootfolder %)"/)
(/b)
(br /)
Appkey Test value:
(b)
(asp:Label ID="Label1" runat="server" Text="(%$appSettings:test1 %)"/)
(/b)
(/div)
(/form)
(/body)
(/html)

Run your application
Output:
AppKey value:Root Folder web.config
AppKey Test value:Testing the application for root folder web.config

Create SubRoot Folder and add web.config and one aspx page and name it as test.aspx

add settings in web.config in subroot folder
(appSettings)
(add key="subrootfolder" value="Sub Root Folder web.config"/)
(add key="test1" value="Testing the application for sub root folder web.config"/)
(/appSettings )

in Test.aspx
(html xmlns="http://www.w3.org/1999/xhtml")
(head id="Head1" runat="server")
(title)SubRoot Folder web.config(/title)
(/head)
(body)
(form id="form1" runat="server")
(div)
(b)
SubRoot Folder values
(/b)
(/div)
(br /)
(div)
AppKey value:
(b)
(asp:Label ID="lbltxt" runat="server" Text="(%$appSettings:subrootfolder %)"/)
(/b)
(br /)
Appkey Test value:
(b)
(asp:Label ID="Label1" runat="server" Text="(%$appSettings:test1 %)"/)
(/b)
(/div)
(/form)
(/body)
(/html)

Run application and test.aspx as start page
Output
AppKey value:Sub Root Folder web.config
AppKey Test value:Testing the application for sub root folder web.config

If you observe above outputs Default.aspx page getting values from root folder web.config file and ChildPage.aspx page getting values from subroot folder web.config file.

Modifying Configuration Settings at Runtime

Adding New Key-Value Pairs
// Adds a key and value to the App.config
public void AddKey(string strKey, string strValue)
{
XmlNode appSettingsNode =
xmlDoc.SelectSingleNode("configuration/appSettings");
try
{
if (KeyExists(strKey))
throw new ArgumentException("Key name: <" + strKey + "> already exists in the configuration.");
XmlNode newChild = appSettingsNode.FirstChild.Clone();
newChild.Attributes["key"].Value = strKey;
newChild.Attributes["value"].Value = strValue;
appSettingsNode.AppendChild(newChild);
//We have to save the configuration in two places,
//because while we have a root App.config,
//we also have an ApplicationName.exe.config.
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory +
"..\\..\\App.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
catch (Exception ex)
{
throw ex;
}
}

Updating Key-Value Pairs
// Updates a key within the App.config
public void UpdateKey(string strKey, string newValue)
{
if (!KeyExists(strKey))
throw new ArgumentNullException("Key", "<" + strKey + "> does not exist in the configuration. Update failed.");
XmlNode appSettingsNode =
xmlDoc.SelectSingleNode("configuration/appSettings");
// Attempt to locate the requested setting.
foreach (XmlNode childNode in appSettingsNode)
{
if (childNode.Attributes["key"].Value == strKey)
childNode.Attributes["value"].Value = newValue;
}
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory +
"..\\..\\App.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}

Deleting Key-Value Pairs
// Deletes a key from the App.config
public void DeleteKey(string strKey)
{
if (!KeyExists(strKey))
throw new ArgumentNullException("Key", "<" + strKey + "> does not exist in the configuration. Update failed.");
XmlNode appSettingsNode =
xmlDoc.SelectSingleNode("configuration/appSettings");
// Attempt to locate the requested setting.
foreach (XmlNode childNode in appSettingsNode)
{
if (childNode.Attributes["key"].Value == strKey)
appSettingsNode.RemoveChild(childNode);
}
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}

Helper Method
// Determines if a key exists within the App.config
public bool KeyExists(string strKey)
{
XmlNode appSettingsNode =
xmlDoc.SelectSingleNode("configuration/appSettings");
// Attempt to locate the requested setting.
foreach (XmlNode childNode in appSettingsNode)
{
if (childNode.Attributes["key"].Value == strKey)
return true;
}
return false;
}

Tuesday, November 22, 2011

Side-by-side Execution

Side-by-side execution is the ability to install multiple versions of code so that an application can choose which version of the common language runtime or of a component it uses. Subsequent installations of other versions of the runtime, an application, or a component will not affect applications already installed.

Benefits of Side-by-Side Execution
Prior to Microsoft Windows XP and the .NET Framework, DLL conflicts occurred because applications were unable to distinguish between incompatible versions of the same code. Type information contained in a DLL was bound only to a file name. An application had no way of knowing if the types contained in a DLL were the same types that the application was built with. As a result, a new version of a component could overwrite an older version and break applications.

Side-by-side execution and the .NET Framework provide the following features to eliminate DLL conflicts:

•Strong-named assemblies.Side-by-side execution uses strong-named assemblies to bind type information to a specific version of an assembly. This prevents an application or component from binding to an invalid version of an assembly. Strong-named assemblies also allow multiple versions of a file to exist on the same computer and to be used by applications.
•Version-aware code storage.The .NET Framework provides version-aware code storage in the global assembly cache. The global assembly cache is a computer-wide code cache present on all computers with the .NET Framework installed. It stores assemblies based on version, culture, and publisher information, and supports multiple versions of components and applications.
•Isolation. Using the .NET Framework, you can create applications and components that execute in isolation, an essential component of side-by-side execution. Isolation involves being aware of the resources you are using and sharing resources with confidence among multiple versions of an application or component. Isolation also includes storing files in a version-specific way.

What is the Common Language Specification?

To fully interact with other objects regardless of the language they were implemented in, objects must expose to callers only those features that are common to all the languages they must interoperate with. For this reason, the Common Language Specification (CLS), which is a set of basic language features needed by many applications, has been defined. The CLS rules define a subset of the common type system; that is, all the rules that apply to the common type system apply to the CLS, except where stricter rules are defined in the CLS. The CLS helps enhance and ensure language interoperability by defining a set of features that developers can rely on to be available in a wide variety of languages. The CLS also establishes requirements for CLS compliance; these help you determine whether your managed code conforms to the CLS and to what extent a given tool supports the development of managed code that uses CLS features.

If your component uses only CLS features in the API that it exposes to other code (including derived classes), the component is guaranteed to be accessible from any programming language that supports the CLS. Components that adhere to the CLS rules and use only the features included in the CLS are said to be CLS-compliant components.

Most of the members defined by types in the .NET Framework class library are CLS-compliant. However, some types in the class library have one or more members that are not CLS-compliant. These members enable support for language features that are not in the CLS. The types and members that are not CLS-compliant are identified as such in the reference documentation, and in all cases a CLS-compliant alternative is available.

The CLS was designed to be large enough to include the language constructs that are commonly needed by developers, yet small enough that most languages are able to support it. In addition, any language construct that makes it impossible to rapidly verify the type safety of code was excluded from the CLS so that all CLS-compliant languages can produce verifiable code if they choose to do so.