Wednesday, March 18, 2009

Writing RSS Feeds in .net

This article will cover following topics

• About RSS feeds
• Various ways to generate RSS Feed
o Using HTTP handlers and Cache mechanism.
o Building an HTTP Handler
• RSS Library
• RSS XML Format
• Advantages
• Limitation and support
 About RSS Feeds:
RSS stands for really simple syndication. It’s one of the medium to deliver information to users’ offline directly in their mailbox/ offline RSS reader application on subscription. It mostly consist the frequently updated data in a website such as blog entries, web feeds, news & alerts, audio & video links etc. RSS feeds are updated on defined frequency depends on the new contents available on the site. It might be hourly, weekly, monthly etc. The users subscribe to the feed on which they like to get timely updates or to aggregate feeds from many sources to one application


An RSS document includes full text or summary based on content plus it’s related metadata such as date of publishing, author, web links etc. it can aggregated using an RSS reader application sometimes called aggregator also. A user provides a link to RSS document and thereafter RSS reader application checks for timely update on each feed subscribed. The RSS icon ("
 ") is very much popular among many web2.0 based websites.

RSS formats are specified using XML, a generic specification for the creation of data formats

Various ways to generate RSS Feed:
RSS Feeds can be generated in following three ways.

• Using HTTP handlers and Cache mechanism.
• Using a scheduler (Window services).
• Using default RSS provided by the MOSS Lists.

1.1 HTTP handlers and Cache mechanism:
1.1.1 When user clicks on the RSS icon attached to the web page/data list, the client browser or RSS reader requests an RSS document from the site.
1.1.2 IIS checks the .rss file extension and invokes .NET library to process it. It will be configured in IIS. The details steps are discussed later in the document
1.1.3 Based on information in the Web.Config file, .NET knows to invoke RSS library class (HTTP handler) to process the request.
1.1.4 RSS library class checks for a cached copy of the RSS document. If it exists, RSS library sends the document back to the browser and terminates. If the response has not been cached (or if it expired), then RSS library continues.
1.1.5 RSS library merges information from the datatable/list/xml with item data from the appropriate list to build the RSS document.
1.1.6 RSS library caches the RSS document and sends it back to the browser.

1.2 Building an HTTP Handler

Whenever a page request comes to IIS it checks the extension of the page requested which is in the Web server configuration that which file to serve on that particular request, since IIS initially handles the request.

An HTTP handler can be configured so that the browser request can ask for a file that doesn't physically exist under the Web site. For example, you won't find a file under the Web root called "example.rss." Instead, IIS sends all requests for .rss files to the .net assembly which is directed from the web configuration file, and .net assembly writes the response from memory.

An http handler be configured in the following way


• Open ISS, Click start, run and enter inetmgr
• Click on configuration button on home directory tab, Application configuration dialog popup
• click on add button in mapping tab, Add/edit application extension mapping dialog open up
• Select the path of the DLL in executable text box and set the extension to .rss
• Close all the dialog boxes and now IIS is ready to serve any request that comes from .rss extension
To configure handler to a file extension through .NET, add a section to the Web.Config file in the root folder of Web application. Here's an example that shows how to associat RSS library with rss file requests .

The httpHandlers "add" tag tells .NET to send all rss file requests (verb="*" path="*.rss") to the RssHandler class in the RSS library assembly (type="Forum3.RSS library.RssHandler, RSS library").




Fig: Sample Web.Config

An HTTP handler is nothing more than a simple class library with at least one class that implements the IHttpHandler interface. The interface has just two members
• ProcessRequest method
• IsReusable property.
IsReusable tells .NET whether or not instances of your class can be safely pooled and reused. RssHandler returns true because it doesn't maintain any state that would interfere with its reusability.

The ProcessRequest method is where all the fun stuff happens. The .NET worker process passes you a reference to the request's HTTP context, which gives you access to the Request and Response objects as well as the other standard elements of the context.

2. RSS Library:

RSS library (RSS library) is .net class file which defines all the methods related to fetch/search and cache the resulted output to RSS format. it can be use any data source to find the result as databases/ xml/MOSS list etc. RSS profile contains all the settings related to the particular RSS that has to served like connection setting, Web link to RSS, title of feed etc.

FIG: Format of well defined RSS
3. RSS Format:

RSS format contain the data according to RSS 2.0 specification.
Here each item depicts a new feed in the document with its metadata. The properties of an RSS feed is classified under channel node. It can title of RSS feed, short description, web link, publish date etc.
Channel node contains many item nodes which translate into specific information available for the user in RSS feed. Each item node contains similar properties like title, description, web link, date of publish etc



References: www.msdn.microsoft.com

Friday, December 5, 2008

Arrays in C# : Value type or Ref type

Array Class is the abstract class provides methods for manipulation of array....
int [] intArray;
intArray = new int[5]

string [] names;
names = new string[10];

now from above which one is value type and which one is refrence type (Int is value type and String is refrence type)
 
ok the topic is interesting lets's play with it
 
 
as we all know the difference in readonly and const keyword in C#
readonly can be applied with only ref type and const can be only with value type except string
 
and if you try to make the above said array to const it will show error while it will work fine with readonly
try using this line of code in your program
 
you may also try this see the error
public const int[] intArray = new int[5];

now lets do another test on these arrays
declare one readonly array and try to change it svalue

like readonly int [] intarray = {1,2,3}

intArray[1] = 4

it will execute fine

because array is ref type so ref remains same when readonly but value can be changed
 
 
so we can finally conclude that all array are refernce type whether it contains value type or reference type

Thursday, October 16, 2008

Introduction to Nullable types in C# 2.0

Content:
• Introduction
• System requirement
• Nullable type’s use
• Creating Nullable types
• The ?? Operator
• Sending Nullable types to the Database
• Nullable objects and the relational and logical operators
Introduction:
Prior to C# 2.0 there was an inability to assign a null value to certain types e.g. Int, DateTime, Float, Decimal. Microsoft C# 2.0 provides nullable type that allows you to assign a null to "value types" that have previously not allowed such an assignment. This article provides a walkthrough how to use nullable types in c# and working of ?? Operator with nullable types.


System requirement
The following software required to be installed
• .net framework & Visual studio 2005

Nullable type’s Use:
Databases use null as being distinct from values such as 0 and false. Without nullable types, mapping of c# data type to database null field depends on arbitrary decision taken by user. In different situations a null in a Boolean field may be equivalent to false or it may be equally valid to map it to true. Nullable types overcome this problem and allow any database value - including the “no value” of null - to be mapped directly to value types in C#. Databases are not the only example either. Any situation where something may not be defined, and only has a meaningful value if it is defined is benefit of nullable types. Optional parameters in XML or HTML also can be set equivalent to null which was not directly possible earlier. Likewise the square root of -1 in real number scenarios, the age of a customer who declined to say how old they are, or the time to completion of some batch process that’s not yet started are some perfect uses of nullable types. So Nullable types can save development time and ease program maintenance. In the past, handling the possibility of unused fields required the use of either placeholder values but this could only work if there was a value that would otherwise be invalid or an extra field that simply indicated whether a field was in use, but having to manually create and manage such a field is annoyance. The nullable type solves both the problems.

Creating Nullable types:
A nullable type is a special version of a value type that is represented by a structure. Nullable types are objects of System. Nullable, where T must be a non nullable type. Nullabe types can be declared in two different ways. First it can be explicitly declared as object of System.Nullable. For example, this creates int and bool nullable types –

System.Nullable intCount;
System.Nullable boolDone;


Secondly it can be declared using short hand method as follows:

int? intCount;
bool? boolDone;


There are two ways to determine if a variable of nullable type is null or contains a value. Nullable variable can be compared with null value or HasValue read-only property can be used.

int? intCount = null;
if (intCount != null )
Console.WriteLine(intCount);
if (intCount.HasValue)
Console.WriteLine(intCount);

If a nullable variable is null and its value is tried to obtain then System.InvalidOperationException will be thrown. Nullable variable’s value can be obtained by using the Value read-only property or by casting it into its underlying type.
Following code sample demonstrates this:-

using System;
using System.Collections.Generic;
using System.Text;

namespace NullableTypeBOK
{
class NullableType
{
static void Main(string[] args)
{
int? intCount = null;
if (intCount.HasValue)
Console.WriteLine(intCount.Value);
else
Console.WriteLine("Count has no value");

intCount = 100;
if (intCount.HasValue)
Console.WriteLine(intCount.Value);
else
Console.WriteLine("Count has no value");

Console.Read();

}
}
}


A nullable object can be used in expressions in the same way as its underlying type. When non-nullable and nullable types are mixed in an operation, the outcome is a nullable value.

The ?? Operator:
The ?? Operator is also called the null coalescing operator, which lets us specify a default value that will be used when nullable object contains null. If without using ?? Operator nullable object is tried to cast to its underlying type then System.InvalidOperationException will be thrown if the nullable object contains a null value. It can be declared as follows:-

Nullable-object ?? Default-value
If nullable object contains a value, then the value of ?? Operator is that value. Otherwise the value of the ?? Operator is default value. For example, in the following code amount is null. This causes Interest to be assigned the value 0.0 and no exception will be thrown and if amount is set to some value then interest will be accordingly.
static void Main(string[] args)
{
float Interest;
System.Nullable pAmount = null;
// if pAmount=10.0F then Interest=40 otherwise 0
System.Nullable rate = 0.8F;
System.Nullable time = 5;
Interest = (pAmount * rate * time) ?? 0.0F;
Console.WriteLine(Interest);
Console.Read();
}

The right hand expression of ?? Operator is evaluated only if left hand side of the operator does not contain a value.

Sending Nullable types to the Database:
Nullable types can be sent to the database and have DBNull inserted for types that have a null value. To do this the ??Operator is used and DBNull is casted to an object. The cast is required because the objects on either side of the ??Operator must be compatible.

Nullable objects and the relational and logical operators:
Nullable objects can be used in relational expressions as same as their corresponding Non-Nullable types. However, when two nullable objects are compared using the <, >, <= or >= operators, the result is false if either of the object is null.
When a logical expression involves two bool? Objects, outcome will be true, false or null. Following truth table shows the entries for the & and operators that apply to bool?.



References:
http://www.blogger.com/www.msdn.com
http://www.blogger.com/www.techrepublic.com