Search This Blog

Thursday, 28 April 2011

IIS Express 7.5

For those who don't have or don't want to install IIS on their machines....

IIS 7.5 Express enhances your ability to develop and test web applications on Windows by combining the power of IIS 7.5 with the convenience of a lightweight web server like the ASP.NET Development Server (also known as Cassini). IIS 7.5 Express is included withMicrosoft WebMatrix, an integrated suite of tools designed to make developing web applications on Windows simple and seamless. IIS 7.5 Express can also be used with Visual Studio 2010 as a powerful alternative to Cassini. The benefits of using IIS 7.5 Express include:
  • The same web server that runs on your production server is now available on your development computer.
  • Most tasks can be done without the need for administrative privileges.
  • IIS 7.5 Express runs on Windows XP and all later versions of Windows.
  • Many users can work independently on the same computer.
This package installs only IIS 7.5 Express. For an integrated development experience, also install Microsoft WebMatrix or Visual Studio 2010.

Download link
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=abc59783-89de-4adc-b770-0a720bb21deb

Get started with Cloud Computing-- The latest buzz word


What is Cloud Computing?

As per wikipedia

The phrase “cloud computing” originated from the cloud symbol that is usually used by flow charts and diagrams to symbolize the internet. The principle behind the cloud is that any computer connected to the internet is connected to the same pool of computing power, applications, and files. Users can store and access personal files such as music, pictures, videos, and bookmarks or play games or use productivity applications on a remote server rather than physically carrying around a storage medium such as a DVD or thumb drive. Almost all users of the internet may be using a form of cloud computing though few realize it. Those who use web-based email such as Gmail, Hotmail, Yahoo, a Company owned email, or even an e-mail client program such as Outlook, Evolution, Mozilla Thunderbird or Entourage that connects to a cloud email server. Hence, utilizing desktop applications to connect to your cloud email, is still considered a cloud application.
So Cloud computing refers to the provision of computational resources on demand via a computer network, such as applications, databases, file services, email, etc. In the traditional model of computing, both data and software are fully contained on the user's computer; in cloud computing, the user's computer may contain almost no software or data (perhaps a minimal operating system and web browser only), serving as little more than a display terminal for processes occurring on a network of computers far away. Common shorthand for a provided cloud computing service (or even an aggregation of all existing cloud services) is "The Cloud".

What are the services available in Cloud?

There are broadly 3 types of services that are available via cloud
a.)     IaaS (Infrastructure as a Service, like Amazon Web Services)
b.)     PaaS (Platform as a Service, like Microsoft Windows Azure)
c.)     SaaS (Software as a Service).

What are the various types of Cloud?

There are 2 types of cloud:-
a.)     Public Cloud
b.)     Private Cloud

I will try to explain more about Iaas, PaaS and SaaS and also more about various types of cloud in future blogs..Keep posted...

How to Use Extension Methods in .Net 2.0

Extension Methods in .NET Framework 2.0 Apps
Extension methods are not doing anything in the underlying IL code that could not be done using Visual Basic 2005; they are simply making a shared method call. What has changed is the compiler's ability to identify specific methods and allow the change in the calling method syntax, which gives the appearance that the type being used has new functionality and makes the extensions work just like the existing instance methods.
Another new feature in Visual Basic 2008 is multi-targeting, which lets you use Visual Basic 2008 to write code that targets a specific version of the Framework. Since the same compiler is used for multi-targeting, it is possible to use extension methods for applications targeted, for example, to version 2.0 of the Framework. This does take a little workaround, as the IDE prevents adding the required references to a .NET Framework 2.0 targeted application.
If you create a 3.5 application and change the target Framework to version 2.0 (using Project Properties | Compiler | Advanced | Target Framework), you'll see some of the references highlighted as missing, including those containing most of the out-of-the-box LINQ functionality. Trying to add these references to a 2.0 project results in a dialog informing you that they require a different version of the targeted Framework (see Figure 1).

 References for Changed Target to the .NET Framework 2.0 

Extension methods can be created and used for 2.0 applications after you have created your own System.Runtime.CompilerServices.Extension attribute. You must first create a new project targeting version 2.0. The default references will not include System.Core, which contains the extension attribute. In the Applications Project Properties, go to the Application Tab, clear the Root Namespace, and then create your own extension attribute in the correct System.Runtime.CompilerServices namespace. The code in Figure 2 shows the creation of the extension attribute and then using this to declare and use extension methods. Once you compile the application, you can call the extension methods.

Here are some important considerations regarding 2.0-targeted applications. First, type inference is off by default. To turn it back on, use Option Infer On. You'll probably want to do when you migrate .NET Framework 2.0 applications to the version 3.5.
Second, to use extension methods, you need to create your own extension attribute that mimics the one in System.Core. The IDE will prevent you from using the System.Core reference on a 2.0-targeted project.
Finally, this technique will not work for ASP.NET applications targeting the .NET Framework 2.0 because they have a runtime dependency on the 2.0 command-line compiler. When those apps are deployed to Web servers that only have the .NET Framework 2.0 installed, they will not work because the 2.0 CSC.EXE command-line compiler does not understand extension methods.
Even if you don't want to start writing applications for the .NET Framework 3.5 or you have existing applications in version 2.0 that you have to maintain, you can still benefit from upgrading to Visual Basic 2008. The extension method functionality is built into the compiler and can be used for any version of the Framework that the Visual Basic 2008 compiler is able to target.


using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.CompilerServices;

static class Module1
{
 public static void Main()
 {
  int i_x = 1;
  string i_s = "Test";

  dynamic x = i_x.IntegerExtension;
  Interaction.MsgBox(x);

  dynamic y = i_s.StringExtension;
  Interaction.MsgBox(y.ToString());
 }
}

//CREATE A USER-DEFINED ATTRIBUTE CALLED EXTENSION IN THE CORRECT NS
namespace System.Runtime.CompilerServices
{
 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
 class ExtensionAttribute : Attribute
 {
 }
}

//DEFINED EXTENSION METHODS WITH USER-DEFINED ATTRIBUTE ON THEM
static class ExtMethods
{
 [Extension()]
 public static string StringExtension(string a)
 {
  return "Success";
 }

 [Extension()]
 public static int IntegerExtension(int a)
 {
  return 100;
 }
}

New Decompiler From Telerik

Telerik has just released a new decompiler.

JustDecompile is a new, free developer productivity tool designed to enable easy .NET assembly browsing and decompiling. Currently available as a BETA, JustDecompile builds on years of experience in code analysis and development productivity originally created for JustCode, Telerik's Visual Studio productivity add-in. JustDecompile lets you effortlessly explore and analyze compiled .NET assemblies, decompiling code with the simple click of a button. Download the BETA and provide feedback in the Forums to help shape the official version, targeted for the Q2 2011 release this summer.

Major Features are :-
  • Innovative Code Navigation and Analysis
  • Side-by-side Assembly Loading
  • Better Decompiling Accuracy
  • Powerful Free Tool by a Leading Commercial Vendor
  • Auto-updating and Regular Updates
  • Professional Support
If you are looking for a free .NET Decompiler, you can download the beta now!

Tuesday, 26 April 2011

Alert user before session timeout

Another very good article on a very commonly faced problem..

http://www.vijaykodali.com/Blog/post/2010/05/20/Alert-user-before-session-timeout.aspx

Keep yourself updated before it bites you :)

Best Practices for Using ADO.NET

I Found this very good article on msdn site..Worth reading...

http://msdn.microsoft.com/en-us/library/ms971481.aspx

Hidden Treasure of Memory Mangement in Windows OS

Windows Operating Systems (OS) allocate Random Access Memory (RAM) for running processes. This physical RAM allocated to the process is called the Working Set. Access to data in the Working Set is extremely fast. If a process requires more memory than is allocated by the operating system, it can use the hard disk as Virtual Memory. In order to use the data in Virtual Memory, the OS must transfer the data from disk to RAM. This process is called Paging. Access to data in the Working Set is extremely fast. Access to data in Virtual Memory is relatively slow, because the disk operates slower than RAM and because Paging takes time.

When an application minimizes its top-level window, the OS reduces the Working Set for the application. This is done to free RAM for foreground processes. The performance of the minimized process may be affected if it must access Virtual Memory more often. You can read about how this is implemented in Microsoft KnowledgeBase 293215: The working set of an application is trimmed when its top-level window is minimized.

This is expected behavior of the Windows OS. You will notice the same behavior in other Windows applications such as Microsoft Word or the Calculator application. After maximizing the same application, you will see that the memory allocated to the application does not return to it's original value. This is again due to Windows allocating more than enough memory when starting the application but upon return to the maximized running window after being minimized it will have a better idea of how much memory is needed to run. If this behavior is a problem for some reason, the Microsoft KB explains how to prevent the Working Set from being trimmed when the window is minimized. You may also adjust this behavior by changing the Processor Scheduling option from Programs to Background Services in Control Panel»System»Advanced»Performance»Advanced»Processor Scheduling.

Sunday, 24 April 2011

ASP.Net: How to send a web page as HTML body

One of the common problems that I have seen developer facing is sending a web page as attachment.I have come across this solution that will embed complete web page into your mail as body.Sending attachment is a very easy task with mail

First of all we will download a webpage using code.There are classes exposed in System.Net namespace through which yo can upload and download entire webpages without opening internet explorer or any other browser.One of such classes is WebRequest class.

Create an object of WebRequest class passing it the URL pf page to attach.This will make a request for the webpage via code.

WebRequest objRequest = System.Net.HttpWebRequest.Create(url);

To get the response from the request call GetResponse() method on it which returns a WebResponse class object but here instead of assigning it to an object of WebResponse class we will call GetResponseStream() method on it to get the entire page stream.

StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());

Why StreamReader?
Because it exposes methods to read entire content as string.With other streams we need to make conversions from byte to string.

string result = sr.ReadToEnd();

Once we have the entire page content as string we will create a MailMessage as
MailMessage mail = new MailMessage("From", "TO");
mail.Subject = "this is a test email.";
string url = "http://www.microsoft.com";
mail.Body = HttpContent(url);
mail.IsBodyHtml = true;

here we have set the IsBodyHtml property to true to indicate that we will passing html as body of the message.Rest is simple create an object of SmtpClient and call send method on in passing the message just created.

SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.Send(mail);
don’t forget to set EnableSsl property to true in case your smtp server needs SSL.In this particular sample we are making use of gmail’s SMTP server which requires SSL.
Here is the complete code

private void Page_Load(object sender, System.EventArgs e)
{
MailMessage mail = new MailMessage("From", "TO");
mail.Subject = "this is a test email.";
string url = "http://www.microsoft.com";
mail.Body = HttpContent(url);
mail.IsBodyHtml = true;

SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.Send(mail);
}
//screen scrape a page here
private string HttpContent(string url)
{
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
string result = sr.ReadToEnd();
sr.Close();
return result;
}


And here goes the web.config settings

<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" port="587" userName="yourgmailId" password=" yourgmailPassword "/>
</smtp>
</mailSettings>
</system.net>

Testing Email Settings in Development Environment without SMTP server

When you are sending mail using SMTP Here's what the App.config settings look like :
<system.net>
<mailSettings>
<smtp>
<network host="smtp.domain.com"/>
</smtp>
</mailSettings>
</system.net>


Now just change the configuration as
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\temp\DropFolderName\"/>
</smtp>
</mailSettings>
</system.net>

What this setting will do is create the email file and drop it into the c:\temp\ DropFolderName\ folder. This setting can be used if you have your SMTP server watch a directory for new mail and it will then send it.
Now, when I send an email, like this:


public void SendMail()
{
SmtpClient smtpClient = new SmtpClient();
MailMessage mailMessage = new MailMessage("from@example.com", "to@example.com");
mailMessage.Subject = "This is the subject";
mailMessage.Body = "This is the body of message";
smtpClient.Send(mailMessage);
}

The mail message will get generated to c:\temp\DropFolderName\

Dynamic AdRotator file through database using SQLXML

Many a times I have come across this problem which developers face when they want the XML file to be created dynamically from database.
For catering this problem first of all create a table in database which have at least five columns with names ImageUrl,NavigateUrl,AlternateText,Keyword,Impressions.
Don’t worry if you have already created a database table and given your custom names to columns and are already using the database and creating XML file by lot of coding efforts.
With SQL server 7.0 and 2000 Microsoft started supporting XML input and xml output from the database using ADO.Net classes. This branch of ADO.Net is also known as SQL XML.
SQLXML primarily make use of three modes to fetch data from database along with FOR clause, which are: -

RAW
AUTO
EXPLICIT

We are going to discuss RAW and Auto and then carry on with our sample.
1.) FOR XML RAW Clause
RAW mode queries result in a flat table-like XML format. The format does not preserve any information about the origin of the data or hierarchical relationships. SQL Server simply transforms each row of the result set into an XML element with the name row, very similar to the output format of the persist-to-XML option of classic ADO Recordsets. Every column that is not NULL is mapped to an attribute of the column’s name. The example SQL statement below illustrates using XML RAW:
select ImageUrl,NavigateUrl,AlternateText,Keyword,Impressions from AdRotator1 as ad for xml RAW
A row returned from an FOR XML RAW query could look like this element:
<row>
    <ImageUrl>image/asp.gif</ImageUrl>
    <NavigateUrl>http://www.sam.net </NavigateUrl>
    <AlternateText>test</AlternateText>
    <Keyword>te</Keyword>
    <Impressions>60</Impressions>
  </row>
The element names do not reflect the tables names the column are coming from. It only lists the columns we selected.
2.) FOR XML AUTO Clause
The XML format returned via AUTO clause is more descriptive. Each selected row results in an element named after the table from which it was selected. The selected columns result in attributes of the elements by default. If the SELECT statement joins multiple tables the results from the joined table are represented as child elements. AUTO mode also recognizes the ELEMENTS option. If we append “, ELEMENTS” to the query statement columns in the returned rowset are also mapped to child elements rather than attributes. The nesting of elements and their children is determined by the order of the tables in the SELECT clause. Thus the order of the columns in the SELECT clause is significant. We can see the difference if we replace RAW with AUTO in the above query:
select ImageUrl,NavigateUrl,AlternateText,Keyword,Impressions from AdRotator1 as Ad for xml auto,elements,ROOT('Advertisements')
<Ad>
    <ImageUrl>image/asp.gif</ImageUrl>
    <NavigateUrl>http://www.sam.net </NavigateUrl>
    <AlternateText>test</AlternateText>
    <Keyword>te</Keyword>
    <Impressions>60</Impressions>
  </Ad>
Now lets carry on with the sample.

Just run this piece of code and bingo.
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\shakti\My Documents\Visual Studio 2005\Projects\Spider\WindowsApplication1\Database1.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand("select ImageUrl,NavigateUrl,AlternateText,Keyword,Impressions from AdRotator1 as ad for xml auto,elements,ROOT('Advertisements')", con);
con.Open();
System.Xml.XmlReader reader = cmd.ExecuteXmlReader();

XmlDocument doc = new XmlDocument();
doc.Load(reader);
doc.Save("C:\\testAd.xml");
reader.Close();