Sunday, December 6, 2009

ASP.Net Web.Config File

ASP.Net Web.Config File

What is Web.Config File?
Web.config file, as it sounds like is a configuration file for the Asp .net web application. An Asp .net application has one web.config file which keeps the configurations required for the corresponding application. Web.config file is written in XML with specific tags having specific meanings.
What is Machine.config File?
As web.config file is used to configure one asp .net web application, same way Machine.config file is used to configure the application according to a particular machine. That is, configuration done in machine.config file is affected on any application that runs on a particular machine. Usually, this file is not altered and only web.config is used which configuring applications.
What can be stored in Web.config file?
There are number of important settings that can be stored in the configuration file. Here are some of the most frequently used configurations, stored conveniently inside Web.config file.
  • Database connections

  • Session States

  • Error Handling

  • Security
Database Connections:
The most important configuration data that can be stored inside the web.config file is the database connection string. Storing the connection string in the web.config file makes sense, since any modifications to the database configurations can be maintained at a single location. As otherwise we'll have to keep it either as a class level variable in all the associated source files or probably keep it in another class as a public static variable.
But if this is stored in the Web.config file, it can be read and used anywhere in the program. This will certainly save us a lot of alteration in different files where we used the old connection.
Lets see a small example of the connection string which is stored in the web.config file.





As you can see it is really simple to store the connection string in the web.config file. The connection string is referenced by a key which in this case is "ConnectionString". The value attribute of the configuration file denotes the information about the database. Here we can see that if has database name, userid and password. You can define more options if you want.
There is a very good website that deals with all sorts of connection strings. Its called http://www.connectionstrings.com/ , in the website you will find the connection strings for most of the databases.

Lets see how we access the connection string from our Asp .net web application.
using System.Configuration;
string connectionString = (string )ConfigurationSettings.AppSettings["ConnectionString"];

The small code snippet above is all that is needed to access the value stored inside the Web.config file.

ASP.Net Web Services

ASP.Net Web Services

  • A programming model that provides the ability to exchange messages in a scalable, loosely coupled and platform neutral environment using standard protocols such as HTTP,XML,XSD,SOAP and WSDL.

  • The SOAP based XML messages exchanged between a XML web service and its clients can be structured and typed or loosely defined.

  • The flexibility of using a text format such as XML enables the message exchange to evolve over time in a loosely coupled way.

Web Services - are a group of closely related, emerging technologies that describe a service oriented, component based application architecture that is based on an open, internet centric infrastructure.

XML,SOAP - a simple XML based protocol for exchanging structured and typed information as the web. The protocol contains no application or transport semantics which makes it highly modular and extensible.

WSDL - Web services Definition Language (similar to method signature). It is an XML based language used to define web services and describe how to access them. It is SOAP's interface definition language.

DISCO - (Discovery file) Holds reference to all the web services hosted on a server.

UDDI (Universal Description, Discovery and Integration)

Client Machine ---- URI of web service ----> IIS (port 80) ------> ASP.Net run time-----

----> Web services Handler ------> creates an instance of MyWebService and invokes the called method.Any parameters required by method are deserialized from SOAP packet and passed to method as .Net objects.


MyWebService ---------returns---> Computed value -----serializes---> data to XML document wrapped in SOAP packet ------>ASP.Net runtime ----response---->IIS----respond --->Client

ASP.NET Session State

ASP.NET Session State

ASP.NET session state enables you to store and retrieve values for a user as the user navigates to different ASP.NET pages that make up a Web application. HTTP is a stateless protocol, meaning that your Web server treats each HTTP request for a page as an independent request; by default, the server retains no knowledge of variable values used during previous requests. As a result, building Web applications that need to maintain some cross-request state information (applications that implement shopping carts, data scrolling, and so on) can be a challenge. ASP.NET session state identifies requests received from the same browser during a limited period of time as a session, and provides the ability to persist variable values for the duration of that session.
ASP.NET session state is enabled by default for all ASP.NET applications. ASP.NET session-state variables are easily set and retrieved using the Session property, which stores session variable values as a collection indexed by name. For example, you can create create the session variables FirstName and LastName to represent the first name and last name of a user, and sets them to values retrieved from TextBox controls.
Basic use of Session in ASP.NET (C#):
STORE: Session["mydataset")=ds;
RETRIEVE: DataSet ds = (DataSet)Session["mydataset"];
Storage location
InProc - session kept as live objects in web server (aspnet_wp.exe).
StateServer - session serialized and stored in memory in a separate process (aspnet_state.exe). State Server can run on another machine
SQLServer - session serialized and stored in SQL server
Performance
InProc
- Fastest, but the more session data, the more memory is consumed on the web server, and that can affect performance.
StateServer - When storing data of basic types (e.g. string, integer, etc), in one test environment it's 15% slower than InProc. However, the cost of serialization/deserialization can affect performance if you're storing lots of objects. You have to do performance testing for your own scenario.
SQLServer - When storing data of basic types (e.g. string, integer, etc), in one test environment it's 25% slower than InProc. Same warning about serialization as in StateServer.

ASP.Net Application state, difference between application, viewstate & session state

What is ASP.NET Application State?

Application state is a data repository available to all classes in an ASP.NET application. Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Unlike session state, which is specific to a single user session, application state applies to all users and all sessions. Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another.
What is Postback?
When an action occurs (like button click), the page containing all the controls within the tag performs an HTTP POST, while having itself as the target URL. This is called Postback.
ASP.NET View State
ASP.NET Web pages provide view state, which is a way for you to store information directly in the page that you want to persist between postbacks.
ASP.NET Cookies
A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site. Creating CookiesResponse.Cookies["userName"].Value = "patrick";Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1); HttpCookie aCookie = new HttpCookie("lastVisit");aCookie.Value = DateTime.Now.ToString();aCookie.Expires = DateTime.Now.AddDays(1);Response.Cookies.Add(aCookie);
Getting Cookiesif(Request.Cookies["userInfo"] != null){ System.Collections.Specialized.NameValueCollection UserInfoCookieCollection; UserInfoCookieCollection = Request.Cookies["userInfo"].Values; Label1.Text = Server.HtmlEncode(UserInfoCookieCollection["userName"]); Label2.Text = Server.HtmlEncode(UserInfoCookieCollection["lastVisit"]);}

Differences between Session and ViewStates
"Session" is data stored per user session. "Viewstate" is an ASP.NET thing. It's a way for the application to maintain state about a particular form and its child controls. Your ASP.NET application might consist of several pages/forms. There might be a piece of data that you want to access across all pages. You could place that in a Session variable. However, you rarely need to worry about ViewState, ASP.NET manages that itself.

Viewstate variables are stored in the HTML of your page. They are encoded so they are not easily readable, but they are not encrypted. This is generally the best scope to use when you have a variable that is valid for a particular page and you want to store the value between postbacks. Once theuser navigates to another page the value is lost.Session variables are stored on the server (in RAM by default) and are persisted between all pages.

Different between application and session state
The difference is in the length of their existance.An application variable exists from when the application is first 'run' (The first time a page in the app is used) and exists until the application is stopped on the server, either through IIS or the actual machine being turned off or rebooted.A session variable is created for the instance of the browser accessing the site and will only be available until that browser is closed or the session times out.

ISS Diploma Black and White