mca assgnm
TRANSCRIPT
-
7/30/2019 MCA ASSGNM
1/13
Q-1
a) Web Form Life CycleThe life cycle begins with a request for the page, which causes the server to load it. When the request is complete,
the page is unloaded. From one end of the life cycle to the other, the goal is to render appropriate HTML output
back to the requesting browser. The life cycle of a page is marked by the following events, each of which we can
handle ourself or leave to default handling by the ASP.NET server:
Initialize: Initialize is the first phase in the life cycle for any page or control. It is here that any settings needed for
the duration of the incoming request are initialized.
Load ViewState: The ViewState property of the control is populated. The ViewState information comes from a
hidden variable on the control, used to persist the state across round trips to the server. The input string from this
hidden variable is parsed by the page framework, and the ViewState property is set. This can be modified via the
LoadViewState( ) method: This allows ASP.NET to manage the state of your control across page loads so that each
control is not reset to its default state each time the page is posted.
Process Postback Data: During this phase, the data sent to the server in the posting is processed. If any of this data
results in a requirement to update the ViewState, that update is performed via the LoadPostData ( ) method.
Load: CreateChildControls ( ) is called, if necessary, to create and initialize server controls in the control tree. State
is restored, and the form controls show client-side data.
Send Postback Change Modifications: If there are any state changes between the current state and the previous
state, change events are raised via the RaisePostDataChangedEvent ( ) method.
Handle Postback Events: The client-side event that caused the postback is handled.
PreRender: This is the phase just before the output is rendered to the browser. It is essentially your last chance to
modify the output prior to rendering using the OnPreRender ( ) method.
Save State: Near the beginning of the life cycle, the persisted view state was loaded from the hidden variable. Now
it is saved back to the hidden variable, persisting as a string object that will complete the round trip to the client.
You can override this using the SaveViewState () method.
Render: This is where the output to be sent back to the client browser is generated.. CreateChildControls ( ) is
called, if necessary, to create and initialize server controls in the control tree.
Dispose: This is the last phase of the life cycle. It does any final cleanup and release references to any expensive
resources, such as database connections. You can modify it using the Dispose ( ) method.
-
7/30/2019 MCA ASSGNM
2/13
b) Creating a Web formWeb 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.
First, we need to add a Web Form to a project by doing the following:
1. Select Project, Add Web Form.2. In the Add New Item dialog box, enter the name Fortune.aspx for the Web Form in the Name text box.3. Click Open.
Now that you've created a Web Form Page, you can add server controls to the page from the Web Forms toolbox.
We'll add a Label control to the page by performing the follow steps:
1. If the Toolbox window is not already open, select View, Toolbox.2. Drag a Label control onto the Designer surface from under the Web Forms tab in the Toolbox.
After you add the Label control, your screen should resemble
Adding a Label control to a Web Form Page.
The next step is to add some application logic to the page. We want to retrieve a random fortune from an ArrayList
of fortunes and display the fortune in the Labelcontrol. To do this, perform the following steps:
-
7/30/2019 MCA ASSGNM
3/13
1. Double-click the Designer surface to switch to the Code Editor.2. Enter the following code for the Page_Load() method:
C#
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
ArrayList colFortunes = new ArrayList();
Random objRan = new Random();
colFortunes.Add("Good things will happen!");
colFortunes.Add("Future looks bright!");
colFortunes.Add("Stay in bed!");
Label1.Text = colFortunes[ objRan.Next( 3 ) ].ToString();
}
Q-2
a)Cookies in ASP.Net
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.
In simple term, cookie is a small text file sent by web server and saved by web browser on client machine.
It is a server side object.
Cookies are used in state management.
HttpCookie class is there for cookie by .NET
Class-> httpSessionState
SessionID is send to the client in the form of cookie by server if the request is generated for the first time.
-
7/30/2019 MCA ASSGNM
4/13
Session Cookie
Name -> ASP.NET_SessionID
Value -> will be alphanumeric value.
Name and value will be sent to client as session cookie (which is stored in cookie header).
b) Session State
ASP.NET allows you to save values by using session statewhich is an instance of the HttpSessionState classfor
each active Web-application session.
Session state is similar to application state, except that is is scoped to the current browser session. If different users
are using your application, each user session will have a different session state. In addition, if a user leaves your
application and then returns later, the second user session will have a different session state from the first.
Session state is structured as a key/value dictionary for storing session-specific information that needs to be
maintained between server round trips and between requests for pages.
You can use session state to accomplish the following tasks:
Uniquely identity browser or client-device requests and map them to an individual session instance on
the server.
Store session-specific data on the server for use across multiple browser or client-device requests within
the same session.
Raise appropriate session management events. In addition, you can write application code leveraging
these events.
Once you add your application-specific information to session state, the server manages this object. Depending onwhich options you specify, session information can be stored in cookies, on an out-of-process server, or an a
computer running Microsoft SQL Server.
c) Application State
ASP.NET allows you to save values using application statewhich is an instance of the HttpApplocationState class
for each active Web application. Application state is a global storage mechanism that is accessible from all pages
in the Web application. Thus, application state is useful for storing information that needs to be maintained between
server round trips and between requests for pages.
Application state is stored in a key/value dictionary that is created during each request to a specific URL. You can
add your application-specific information to this structure to store it between page requests.
Once you add your application-specific information to application state, the server manages it.
Q-3
a)Writing and testing a web service
-
7/30/2019 MCA ASSGNM
5/13
b) Implementing a Web Service ClientTo demonstrate how to create such programs, we'll create two different clients for our TimeUtils Web Service: a
console application and a Web page.
Before creating the client to our TimeUtils application, we need to create a proxy class. A proxy class allows us to
call the GetTime method without worrying about how to connect to the server and how to pass and returnparameters to and from the method. In general, a proxy is an object that wraps up method calls and parameters,
forwards them to a remote host, and returns the results to us. Proxies serve the role as a middleman on the client
computer.
.NET comes with a special utility called WSDL.exe for creating Web Service proxies. As you might imagine from
the utility's name, WSDL.exe takes a Web Service's WSDL description and creates a proxy. Follow these steps to
create the proxy with WSDL.exe:
1. Open a Visual Studio.NET command prompt (from the Start menu, choose Visual Studio.NET, VisualStudio.NET Tools, and then Visual Studio.Net Command Prompt). This DOS command prompt contains all
the paths for the .NET tools.
2. Create a new directory for the client projects, such as C:\src\timeclient.3. Type the following command at the prompt (try to fit the entire command at the second prompt onto one line):
4. C:>cd src\timeclient5. C:\src\timeclient> wsdl /l:CS /n:WebBook6. /out:TimeProxy.cs
http://localhost/TimeService/TimeUtils.asmx?WSDL
This command creates the TimeProxy.cs file, which you can use in your client applications:
/l:CS tells the utility to create the proxy using C#. /n:WebBook specifies a namespace for the WSDL tools to use when it creates the proxy class. You can
use any namespace name you want.
/out:TimeProxy.cs specifies the name of the output file.7. The last parameter is an URL for getting a WSDL description of the Web Service. You can also specify a file
containing WSDL.
8. Compile the new proxy file:
-
7/30/2019 MCA ASSGNM
6/13
C:\src\timeclient> csc /t:libarary TimeProxy.cs
The proxy that the WSDL.exe utility creates should look like the file shown in Listing 3.
Listing 3An Automatically Generated Proxy Class for the TimeUtils Client
1: //-----------------------------------------------------------------------
2: //
3: // This code was generated by a tool.
4: // Runtime Version: 1.0.2914.16
5: //
6: // Changes to this file may cause incorrect behavior and will be lost
7: // if the code is regenerated.
8: //
9: //-----------------------------------------------------------------------
10:
11: //
12: // This source code was auto-generated by wsdl, Version=1.0.2914.16.
13: //
14: namespace WebBook {
15: using System.Diagnostics;
16: using System.Xml.Serialization;
17: using System;
18: using System.Web.Services.Protocols;
19: using System.Web.Services;
20:
21: [System.Web.Services.WebServiceBindingAttribute(
22: Name="TimeUtilitiesSoap",
23: Namespace="http://tempuri.org/webservices")]
24: public class TimeUtilities :
25: System.Web.Services.Protocols.SoapHttpClientProtocol {
26:
27: [System.Diagnostics.DebuggerStepThroughAttribute()]
28: public TimeUtilities() {
29: this.Url = "http://localhost/TimeService/TimeUtils.asmx";
30: }
-
7/30/2019 MCA ASSGNM
7/13
31:
32: [System.Diagnostics.DebuggerStepThroughAttribute()]
33: [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
34: "http://tempuri.org/webservices/GetTime",
35: RequestNamespace="http://tempuri.org/webservices",
36: ResponseNamespace="http://tempuri.org/webservices",
37: Use=System.Web.Services.Description.SoapBindingUse.Literal,
38: ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
39: public string GetTime() {
40: object[] results = this.Invoke("GetTime", new object[0]);
41: return ((string)(results[0]));
42: }
43:
44: [System.Diagnostics.DebuggerStepThroughAttribute()]
45: public System.IasyncResult
46: BeginGetTime(System.AsyncCallback callback,
47: object asyncState) {
48: return this.BeginInvoke("GetTime", new object[0],
49: callback, asyncState);
50: }
51:
52: [System.Diagnostics.DebuggerStepThroughAttribute()]
53: public string EndGetTime(System.IAsyncResult asyncResult) {
54: object[] results = this.EndInvoke(asyncResult);
55: return ((string)(results[0]));
56: }
57: }
58: }
The proxy file contains a new definition for the TimeUtilities class, different from the one created earlier in Listing
1. This new definition is the version that all our client programs will use. Although this new class is defined
differently than the original created for the Web Service, this newTimeUtilities class will work the same way for any
client program that uses it. Rather than execute code directly, this new class will call the Web Service to execute
each method.
-
7/30/2019 MCA ASSGNM
8/13
Notice also the two new methods in the proxy class, BeginGetTime (Lines 4450) and EndGetTime(Lines 5256).
These methods are created so that we can call the Web Service asynchronously. This means that we can call the
Web Service in our client program and then do other work immediately. Then, when the Web Service returns a
result, we can process the results at that time. This asynchronous method for calling remote Web Services might not
be responsive because of a slow network connection.
Now that we've created our proxy class, we can create client programs that use our Web Service. To begin, let's
create a simple console application to call the Web Service. The client is shown in Listing 4.
Listing 4CallService.cs: A Console Application That Calls the TimeUtils Web Service
using System;
using WebBook;
namespace WebBook
{
public class CallService
{
public static void Main()
{
TimeUtilities remoteService = new TimeUtilities();
Console.WriteLine("Calling web service...");
Console.WriteLine(remoteService.GetTime());
}
}
}
If you save the CallService.cs client in the TimeClient project directory that you created, you can compile it by using
this command:
csc CallService.cs /r:TimeProxy.dll
This command creates a file named CallService.exe in the client directory, which you can run now. For this
command to work, you must have compiled the proxy class in Listing 3 according to the instructions in step 4 of this
section.
After running the CallService.exe program you created, you should see output like the following:
-
7/30/2019 MCA ASSGNM
9/13
Calling web service...
The current time on the server is: 7/9/2001 12:19:08 PM
Creating a Web Service Client
Now that you've seen how to create a Web Service proxy and client program manually, let's use Visual Studio.NET
to make a Web page that calls the TimeUtils Web Service. Visual Studio.NET automates the process by creating a
proxy file using a wizard. For the example in this section, we'll create an ASP.NET page that calls the Web Service.
To create the client application, follow these steps:
1. Create a new ASP.NET Web application by selecting New Project on Visual Studio's start page.2. In the New Project dialog, select Visual C# Projects in the Project Types list and then select ASP.NET Web
Application from the Templates list. Name the Web application ASPClient and click OK
3. From the Project menu, choose Add Web Reference. You should see a dialog similar to Figure 4.4. In the Address text box, enterhttp://localhost/TimeService/TimeUtils.asmxand then click the Add
Reference button to create a proxy file for the Web Service and add it to your project.
5. Change the Page_Load method in WebForm1.aspx to use the code in Listing 5.6. Run the project.
Listing 5Page_Load Method for the Visual Studio Client ASP.NET Page
private void Page_Load(object sender, System.EventArgs e)
{
localhost.TimeUtilities remoteService = new localhost.TimeUtilities();
Response.Write(remoteService.GetTime());
}
Q-4
a)Creating Application Pools (IIS 6.0)
With IIS 6.0 running in worker process isolation mode, you can group Web applications into application pools.
Application pools allow specific configuration settings to be applied to groups of applications, and the worker
processes servicing those applications. Any Web directory or virtual directory can be assigned to an application
pool.
http://popup%28%27/content/images/sam_aitkensyme1_intro/elementLinks/sam_aitken-syme1_fig4.gif')http://popup%28%27/content/images/sam_aitkensyme1_intro/elementLinks/sam_aitken-syme1_fig4.gif') -
7/30/2019 MCA ASSGNM
10/13
By creating new application pools and assigning Web sites and applications to them, you can make your server
more efficient and reliable, and your other applications always available, even when the applications in the new
application pool terminate.
Procedures
To create a new application pool
1. In IIS Manager, double-click the local computer, right-click Application Pools, point to New, and then click
Application Pool.
2. In the Application pool ID box, type the name of the new application pool.
3. Under Application pool settings, click either Use default settings for new application pool or Use existing
application pool as template.
4. If you selected Use existing application pool as template, from the Application pool name list box, click the
application pool to be used as a template.
5. Click OK.
b) Deploying ASP.NET ApplicationsDeployment refers to the process of copying an asp.net web application from the development system to the server
on which the application will be run. There are several way we can deploy our web application
We can deploy ASP.NET Application in 3 different ways
xCopy Deployment Precompiled Deployment Web Setup Project
xCopy Deployment
To manually copy the files of an asp.net web site to a server. We can use the xcopy command from a command
prompt. Then we can use IISs (Internet Information Servemanagement console t o create a virtual directory thats
mapped to the directory that you copied the web site to.
Setup Deployment
It uses a web setup project to build a windows setup program used to deploy website onto server. Useful for deployment on multiple servers. Can be used to deploy precompiled assemblies and can be configured to include or omit the source code. The installed application can be removed by using add or remove programs dialog box from control panel
-
7/30/2019 MCA ASSGNM
11/13
Q-6
A class is a construct that enables you to create your own custom types by grouping together variables of other
types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. If the class is not
declared as static, client code can use it by creating objects or instances which are assigned to a variable. The
variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for
garbage collection. If the class is declared as static, then only one copy exists in memory and client code can only
access it through the class itself, not an instance variable. For more information, see Static Classes and Static Class
Members (C# Programming Guide).
Unlike structs, classes support inheritance, a fundamental characteristic of object-oriented programming.
Declaring classes
public class Customer
{
//Fields, properties, methods and events go here...
}
Creating object
Customer object1 = new Customer();
Class Inheritance
public class Manager : Employee
{
// Employee fields, properties, methods and events are inherited
// New Manager fields, properties, methods and events go here...
}
EXAMPLE
-
7/30/2019 MCA ASSGNM
12/13
public class Person
{
// Field
public string name;
// Constructor
public Person()
{
name = "unknown";
}
// Method
public void SetName(string newName)
{
name = newName;
}
}
class TestPerson
{
static void Main()
{
Person person = new Person();
-
7/30/2019 MCA ASSGNM
13/13
Console.WriteLine(person.name);
person.SetName("John Smith");
Console.WriteLine(person.name);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/* Output:
unknown
John Smith
*/