Unit 2.intro To
Unit 2.intro To
NET Framework
2.1. DOT NET Framework
Important Points:
Visual Studio is the development tool that is used to design and develop .NET
applications. For using Visual Studio, the user has to first install the .NET
framework on the system.
In the older version of Windows OS like XP SP1, SP2, or SP3, the .NET
framework was integrated with the installation media.
Windows 8, 8.1, or 10 do not provide a pre-installed version 3.5 or later of
.NET Framework. Therefore, a version higher than 3.5 must be installed either
from a Windows installation media or from the Internet on demand. Windows
update will give recommendations to install the .NET framework.
Garbage collection:-
Garbage Collection is a technique introduced in Microsoft .NET that manages
memory automatically. This article discusses the concepts of Garbage Collection
and the strategies adopted by Microsoft .NET for handling managed memory
efficiently. It also discusses the methods and properties of the System. GC class,
the class that is responsible for controlling the garbage collector in the .NET
environment.
What is Garbage Collection?
The Common Language Runtime (CLR) requires that you create objects in the
managed heap, but you do not have to bother with cleaning up the memory once
the object goes out of the scope or is no longer needed. This is unlike the
strategies adopted in programming languages like C and C++ where you needed to
cleanup the heap memory explicitly using a free function of C and delete operator
of C++. Garbage collection refers to the strategy adapted by Microsoft .NET to
free unused objects or objects that go out of the scope automatically.
A "garbage" object is one that is no longer needed, is unreachable from the root or
goes out of the scope in which it is created. Microsoft .NET uses the information
in the metadata to trace the object graph and detect the objects that need to be
garbage collected. Objects that are not reachable from the root are referred to as
garbage objects and are marked for garbage collection. It is to be noted here that
there is a time gap between the time when an object is identified as garbage and the
time when the object is actually collected. It is also to be noted that objects in the
managed heap are stored in sequential memory locations. This is unlike C and
C++ and makes allocation and de-allocation of objects faster.
Strong and Weak References
The garbage collector can reclaim only objects that have no references. An object
that is reachable cannot be garbage collected by the garbage collector. Such a
reference is known as a strong reference. An object can also be referred to as a
weak reference; another term for a weak reference is the target. An object is
eligible for garbage collection if it does not contain any strong references,
irrespective of the number of weak references it contains. Weak references are of
the following types:
· Short Weak Reference
· Long Weak Reference
A short weak reference does not track resurrection while a long weak reference
tracks resurrection. The primary advantage of maintaining weak references to an
object is that it allows the garbage collector to collect or reclaim memory of the
object if it runs out of memory in the managed heap.
The System.GC class
The System. GC class represents the garbage collector and contains many of
methods and properties that are described in this section.
GC.Collect Method
This method is used to force a garbage collection of all the generations. It can also
force a garbage collection of a particular generation passed to it as a
parameter. The signatures of the overloaded Collect methods are:
A web service is a web-based functionality accessed using the protocols of the web
to be used by the web applications. There are three aspects of web service
development:
Creating the web service
Creating a proxy
Consuming the web service
Creating a Web Service
A web service is a web application which is basically a class consisting of methods
that could be used by other applications. It also follows a code-behind architecture
such as the ASP.NET web pages, although it does not have a user interface.
To understand the concept let us create a web service to provide stock price
information. The clients can query about the name and price of a stock based on
the stock symbol. To keep this example simple, the values are hardcoded in a two-
dimensional array. This web service has three methods:
A default HelloWorld method
A GetName Method
A GetPrice Method
Take the following steps to create the web service:
Step (1) : Select File -> New -> Web Site in Visual Studio, and then select
ASP.NET Web Service.
Step (2) : A web service file called Service.asmx and its code behind file,
Service.cs is created in the App_Code directory of the project.
Step (3) : Change the names of the files to StockService.asmx and StockService.cs.
Step (4) : The .asmx file has simply a WebService directive on it:
<%@ WebService Language="C#" CodeBehind="~/App_Code/StockService.cs"
Class="StockService" %>
Step (5) : Open the StockService.cs file, the code generated in it is the basic Hello
World service. The default web service code behind file looks like the following:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace StockService
{
// <summary>
// Summary description for Service1
// <summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
string[,] stocks =
{
{"RELIND", "Reliance Industries", "1060.15"},
{"ICICI", "ICICI Bank", "911.55"},
{"JSW", "JSW Steel", "1201.25"},
{"WIPRO", "Wipro Limited", "1194.65"},
{"SATYAM", "Satyam Computers", "91.10"}
};
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public double GetPrice(string symbol)
{
//it takes the symbol as parameter and returns price
for (int i = 0; i < stocks.GetLength(0); i++)
{
if (String.Compare(symbol, stocks[i, 0], true) == 0)
return Convert.ToDouble(stocks[i, 2]);
}
return 0;
}
[WebMethod]
public string GetName(string symbol)
{
// It takes the symbol as parameter and
// returns name of the stock
for (int i = 0; i < stocks.GetLength(0); i++)
{
if (String.Compare(symbol, stocks[i, 0], true) == 0)
return stocks[i, 1];
}
Step (8) : Click on a method name, and check whether it runs properly.
Step (9) : For testing the GetName method, provide one of the stock symbols,
which are hard coded, it returns the name of the stock
Consuming the Web Service
For using the web service, create a web site under the same solution. This could be
done by right clicking on the Solution name in the Solution Explorer. The web
page calling the web service should have a label control to display the returned
results and two button controls one for post back and another for calling the
service.
The content file for the web application is as follows:
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="wsclient._Default" %>
<head runat="server">
<title>
Untitled Page
</title>
</head>
<body>
</div>
</form>
</body>
</html>
The code behind file for the web application is as follows:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace wsclient
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblmessage.Text = "First Loading Time: " +
DateTime.Now.ToLongTimeString
}
else
{
lblmessage.Text = "PostBack at: " + DateTime.Now.ToLongTimeString();
}
}
Step (2) : Select 'Web Services in this solution'. It returns the StockService
reference.
Step (3) : Clicking on the service opens the test web page. By default the proxy
created is called 'localhost', you can rename it. Click on 'Add Reference' to add the
proxy to the client application.
Include the proxy in the code behind file by adding:
using localhost;
ASP.NET
ASP.NET is a part of Microsoft .NET Framework. The following image shows the
component stack.
Fig: .NET framework components
The following table shows the versions and features included in ASP.NET.
46.5M
823
April 12, 4.0 The two new properties added in the Page
2010 class are MetaKeyword and MetaDescription.
1. Web Forms
2. ASP.NET MVC
3. ASP.NET Web Pages
Web Forms
ASP.NET MVC
It is used to create dynamic web pages. It provides fast and lightweight way to
combine server code with HTML. It helps to add video, link to the social sites. It
also provides other features like you can create beautiful sites that conform to the
latest web standards.
All these are stable and well equipped frameworks. We can create web applications
with any of them. These are also based on the .NET Framework and share core
functionalities of .NET and ASP.NET.
We can use any development style to create application. The selection of style is
depends on the skills and experience of the programmer.
Although each framework is independent to other, we can combine and use any of
that at any level of our application. For example, to develop client interaction
module, we can use MVC and for data control, we can use Web Forms.
The following table contains the lifecycle stages of ASP.NET web page.
Stage Description
Page request This stage occurs before the lifecycle begins. When a
page is requested by the user, ASP.NET parses and
compiles that page.
Rendering Before rendering, view state is saved for the page and all
controls. During the rendering stage, the page calls the
Render method for each control, providing a text writer
that writes its output to the OutputStream object of the
page's Response property.
Unload At this stage the requested page has been fully rendered
and is ready to terminate.at this stage all properties are
unloaded and cleanup is performed.
A requested page first loaded into the server memory after that processes and sent
to the bowser. At last it is unloaded from the server memory. ASP.NET provides
methods and events at each stage of the page lifecycle that we can use in our
application. In the following table, we are tabled events.
PreRender This event occurs after the page object has created
all controls that are required in order to render the
page.
Unload This event raised for each control and then for the
page.
We can use Visual Studio to create ASP.NET Web Forms. It is an IDE (Integrated
Development Environment) that allows us to drag and drop server controls to the
web forms. It also allows us to set properties, events and methods for the controls.
To write business logic, we can choose any .NET language like: Visual Basic or
Visual C#.
Web Forms are made up of two components: the visual portion (the ASPX file),
and the code behind the form, which resides in a separate class file.
44.1MHistory of Java
The main purpose of Web Forms is to overcome the limitations of ASP and
separate view from the application logic.
ASP.NET provides various controls like: server controls and HTML controls for
the Web Forms. We have tables all these controls below.
Server Controls
The following table contains the server-side controls for the Web Forms.
HTML Controls
These controls render by the browser. We can also make HTML controls as server
control. we will discuss about this in further our tutorial.
Controls Description
Name
File Field Places a text field and a Browse button on a form and allows
the user to select a file name from their local machine when
the Browse button is clicked
CheckBox Gives the user a check box that they can select or clear
Radio Used two or more to a form, and allows the user to choose
Button one of the controls
ListBox Displays a list of items to the user. You can set the size from
two or more to specify how many items you wish show. If
there are more items than will fit within this limit, a scroll
bar is automatically added to this control.
Dropdown Displays a list of items to the user, but only one item at a
time will appear. The user can click a down arrow from the
side of this control and a list of items will be displayed.
Events:
Event is an action or occurrence such as a mouse click, key press and mouse
movements, or any system generated notifications.
Event in asp.net raised at the client machine and handled at the server machine
The server has a subroutine describing what to do when the event is raised; it is
called the event handler.
In ASP.NET Web Forms pages, however, events associated with server controls
originate on the client but are handled on the Web server by the ASP.NET.
ASP.NET Web Forms follow a standard .NET Framework pattern for event-
handler methods. All events pass two arguments: an object representing the object
that raised the event, and an event object containing any event-specific
information.
Here, we are creating an event handler for click event. In this example, when user
click on the button, an event fires and handler code executes at server side.
// EventHandling.aspx
// EventHandling.aspx.cs
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Web;
5. using System.Web.UI;
6. using System.Web.UI.WebControls;
7. namespace asp.netexample
8. {
9. public partial class EnventHandling : System.Web.UI.Page
10. {
11.protected void Button1_Click(object sender, EventArgs e)
12. {
13.int a = Convert.ToInt32(firstvalue.Text);
14.int b = Convert.ToInt32(secondvalue.Text);
15. total.Text = (a + b).ToString();
16. }
17. }
18.}
Output: