Most Viewed

Most Viewed

Saturday 11 August 2012

C#4.0 Dynamic Vs Var keyword


C#4.0 Dynamic Vs Var keyword


In C # 4.0, There is a new keyword called dynamic, We can use ia as any datatype like float,int,string, double etc.

Actually we can implement the samething using Var keyword also, But there is a different behavior.

If you want to declare a variable of type with the var keyword, var implicitly static, I'll explain its meaning.

When we declare a string of some data (initialization that is) must be of type String and then the compiler  will use reflection to get all info about the type at the compile time.
See the example given Below:


Now Here we declared a string 

 Var someStr = "Example Some String ";
Response.Write(SomeStr.Length);
Var Keyword Implementation
 Similarly if we use dynamic keyword, It behaves like a Object
Dynamic Keyword Implementation













 We can access methods,properties,operators using Dynamic Keyword

According to the MSDN blog “Variables declared with var are implicitly but statically typed. Variables declared with dynamic are dynamically typed. This capability was added to the CLR in order to support dynamiclanguages like Ruby and Python.”







Read more ...

Generate Random password using C#.Net

 Generating Random password in C#.Net

I create a lot of code to C # with a random password to see. NET. But I feel the following code is very simple to create. Maybe I'm wrong, but I have my solution to this problem.



If you want a better way go through the given below code:

static string CreateRandPass(int numberOfChar)
{
           if (
numberOfChar <= 0 || numberOfChar > 32)
           {
               throw new ArgumentException(“A password of only length 1 to 32 can be generated.”);
           }
           string guidWithoutDashes = Guid.NewGuid().ToString(“n”);
           //Console.WriteLine(“Guid without  :- ” + guidWithoutDashes);
           var chars1 = new char[numberOfChar
];
           var random = new Random();
           for (int i = 0; i < chars.Length; i++)
           {
               chars[i] = guidWithoutDashes[random.Next(guidWithoutDashes.Length)];
           }
           //Console.WriteLine(“Random password is:- ” + new string(chars));
           return new string(chars);
}


xcgvdfgfg
Read more ...

Thursday 2 August 2012

OutProcess Sessions



Enhancement of session state in ASP.NET
1.           ASP.NET supports out process session, this is also called as process independent sessions.
2.           ASP.NET supports cookie less session.


 

When the session are maintained within the process of application domain, then it will not provide reliability because session will be destroyed when application domain is destroyed. The application domain will be recycled in the following cases.
a.            When you make changes to web.config, application domain will be recreated with new settings.
b.           When the web server is restarted the application domain will be recreated.

When the session are maintained outside the application domain process (i.e. ASP.NET worker Process)
This out process session provides more reliability and a solution for web forming.
The external process for maintaining sessions can be state server or SQL server.
This is a service of the Operating system that will be installed with .net framework. This comes as a file ASPNET_State.Exe. This will maintain session data in the form of inmemory storage.



Read more ...

State Management in ASP.Net



State Management

The connection between the client and the server will be closed once the response is returned to the client.
This is called as stateless nature of the web.
When the web server maintains the information about the client, then it is called as state management concept.
1.                       Submission of data using post method.
2.                       Cookies
3.                       Session memory
4.                       Application memory
5.                       Cache data (new in asp.net)

Submission of data

Http context is the memory area in which the instances of the classes will be executed. By getting the reference to this memory area. We can access the handler class instance.

Cookie

Cookie can be defined as small amount of memory used by the web server within client machine.
Basically cookies are used to maintain personal information of the client
The cookies can be classified in two types.
1.                       Inmemory cookie
2.                       Persistent cookie

When the cookie is stored within the process memory of the browser then it is called as Inmemory cookie.
Inmemory cookie is a temporary cookie because when the browser will be closed the data will be lost.
When the browser is storing cookie on to the hard disk memory, then it is called as persistent cookie.
It is provided with particular lifetime.

Creating a cookie:
Dim obj as new httpCookie (“name”, value)
Obj.expires=#mm/dd/yyyy/hh:mm:ss#(requires incase of Persistent cookie)
Response.appendcookie (obj)àthis will write cookie information to the client machine

Reading the cookie:
Dim obj as httpcookie
Obj=request. Cookies (“name”)àit returns an object of httpcookie class, if the obj is nothing then cookie is not available.

Each request from the client will carry all the cookies information to web server.

The cookies will be stored into the hard disk memory of the client machine, with respect to network login folder.
(C:\document and settings\administrator (login folder)\cookies\administrator@localhost.txt)

The cookie information is maintained in the client machine, so it is called as client side state management.
It does not provide any security because it is available in the client machine. So the client can modify the value of the cookie as well as delete the text file.
A cookie can represent maximum of 4KB data.
A website can be used with maximum of 20 cookies because a browser can accommodate only 20 cookies towards single website.
A browser can accommodate maximum of 400 cookies with respect to different websites.

Session Memory
When the first request from the client goes to the web server, the web server will create a unique memory to the client on the server machine.
This session memory provides server side state management.
A cookie can represent only ordinary text, where as a session can represent ordinary text, arrays, objects etc.
The session will be maintained on the web server with the time out of 20 minutes (by default), but this can be changed as per the requirements.
Using session object we can access the session memory.
Session properties are,
1. Session id
2. Time out (To close session implicitly)
Session methods are,
1. Add (varname, value)
2. Remove (varname)
3. Abandon (to close session explicitly)

When it comes to ASP 3.0, session variable can’t be removed, i.e. erased until session is closed. When it comes to ASP.NET, session variable can be removed by using remove method.

Session Tracking
It is a concept of identifying the session by the web server belonging to a particular user.
When user sends the 2nd request to the server, the session ID will be carried with the request, so the server can trap the session memory assigned for the particular user.
The session id will be 120-bit number. (i.e. 15 bytes) in the hexadecimal format.

Application memory
When the first client’s first request comes to the web server towards a particular application, then web server will allocate memory for the application. This memory is called as application memory.
This memory will be common to all the users.
The application memory does not have timeout, this will be maintained till the web server is active. (i.e. executing)
E.g. At the time of chatting we require a common field i.e. application memory not the session memory.

By using application object, we can access the application memory from the web page.
Like session, application objects are having some methods.
1. Add (var, value)
2. Remove (var)
3. Lock ()
4. Unlock ()

Each client request to the web server will start a new thread, when one thread is processing the data in the application memory before the processing is finished, if the time slice is completed the processor will go to next client request, if this thread is processing data in the application memory, then the result will not be proper. To avoid this it is advisable to allow only one thread at a time to work with application memory. This can be achieved through lock () and Unlock () methods.
This process is called as synchronization of threads.

SESSION EVENTS AND APPLICATION EVENTS
1. Session OnStart ()
2. Session OnEnd ()

When a session memory is created OnStart () event will be executed.
Before releasing the session memory OnEnd () event will be executed.

Application Events are,
1. Application OnStart ()
2. Application OnEnd ()
3. Application BeginRequest ()
4. Application EndRequest ()
   (Newly provided in ASP.NET)

When a request comes for a particular web page then begin request will be executed, then the particular requested page will be processed and then the end request will be executed.

GLOBAL.ASAX

Session and application events have to be placed within a special file called as global.asax.
a. Global.asax file has to be stored within the root directory of the application.
b.                       An application can have only one Global.asax file.


When the first client’s first request comes to the asp.net runtime, i.e. web server towards a particular application, then it will create application memory and an instance of global class within global.asax file. This instance will be maintained through out the application for making calls to the session events and application events.






Read more ...