Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning ASP.NET 2

.0.pdf
Скачиваний:
23
Добавлен:
17.08.2013
Размер:
24.67 Mб
Скачать

Deployment, Builds, and Finishing Up

Figure 16-12

Because failed logins are written to the event log by default, take a look at how you could also log successful logins to your application in the Event Log. In this Try It Out you track when a user logs in to Wrox United successfully, and you write details about it to the Event Log.

Try It Out

Adding Health Monitoring to Wrox United

1.Open Web.config and in <system.web>, add a <healthMonitoring>element as follows:

<healthMonitoring enabled=”true”>

</healthMonitoring>

2.Create another child element inside <healthMonitoring> called <rules>:

<rules>

<add provider=”EventLogProvider” name=”Success Audits” eventName=” LoginSuccess” /> </rules>

3.Create another child element inside <healthMonitoring> underneath <rules> called

<eventMappings>:

<eventMappings>

<add name=”LoginSuccess” type=”System.Web.Management.WebAuthenticationSuccessAuditEvent,System.Web,Version=2

.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a” startEventCode=”0” endEventCode=”2147483647”/> </eventMappings>

4.Create a <providers> child element in the <healthMonitoring> element. The provider specifies where you want the event information to be sent; in this case you are specifying the Event Log:

619

Chapter 16

<providers>

<add name=”EventLogProvider”

type=”System.Web.Management.EventLogWebEventProvider,System.Web,Version=2.0.0.0, Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a” /></providers>

5.Run the application and login as Dave, password dave@123.

6.Go to the Control Panel and select Administrative Tools Event Log, and you’ll see the screen shown in Figure 16-13.

Figure 16-13

How It Works

The <health Monitoring> element in the Web.config file does all of the work for you. You start by enabling <healthMonitoring> and then placing three elements inside it to make it work.

The <rules> element is used to map an event onto a provider. The <rules> element has three compulsory attributes: provider, name, and eventName. The provider attribute matches the name attribute specified in the <providers> element. You specify the Event Log in this element and a name for your rule, “Success Audits.”

<rules>

<add provider=”EventLogProvider” name=”Success Audits” eventName=”LoginSuccess” /> </rules>

The <eventMappings> element specifies a user-friendly name for the event and the specific type of the event you want to monitor. The type has to contain the system assembly details as well as information about that assembly such as the Version number, Culture, and PublicKeyToken:

620

Deployment, Builds, and Finishing Up

<eventMappings>

<add name=”LoginSuccess” type=”System.Web.Management.WebAuthenticationSuccessAuditEvent,System.Web,Version=2

.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a” startEventCode=”0” endEventCode=”2147483647”/> </eventMappings>

The <providers> element specifies which source to write the error information to, such as Event Log or SQL Server database. Providers always have two compulsory attributes: name and type. The <provider> must use a name attribute corresponding with a name specified in <eventMappings>. Providers may have other compulsory attributes, but these will vary depending on the type of provider. The EventLogProvider only requires the name and type attributes:

<providers>

<add name=”EventLogProvider” type=”System.Web.Management.EventLogWebEventProvider,System.Web,Version=2.0.0.0, Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a” /></providers>

If you scroll down the Event Log entry you will see a whole stack of useful information about the login. This information includes the event message, the time the error occurred, the location of the error, the error message, the user’s IP address, whether or not they were authenticated, and much more.

Mailing an Administrator

Wrox United now writes to an Event Log, but this is only of use if the administrator is on hand to check the Event Log. A more proactive approach might involve e-mailing the administrator each time the monitored event takes place. Doing this requires only minimal alteration of the <healthMonitoring> element. You only need to change the rules of which provider you are indicating and the provider itself, and also add a SMTP section to the web.config file (if there isn’t an existing one). You can change the details of the <rules> element and the <providers> element and add an SMTP section very simply.

Please note that this Try It Out also requires a working SMTP server. Only if you have IIS installed with the SMTP service (done via the Add or Remove Programs section of Control Panel) will you be able to do this.

Try It Out

Mailing the Administrator

1.Open Web.config and alter the <rules> element so that it now reads as follows:

<add provider=”EmailProvider” name=”My Failure Audit” eventName=”Failure Audits” />

2.Go to the <providers> element and change it so that it now reads as follows:

<add name=”EmailProvider” type=”System.Web.Management.SimpleMailWebEventProvider” from=”myemail@myserver.co.uk” to=”myemail@myserver.co.uk

subjectPrefix=”Failed Login Attempt:” buffer=”true” bufferMode=”Notification” />

Replace myemail@myserver.co.uk with your own SMTP server’s domain name. If your machine is TESTONE, replace it with myemail@testone.com.

621

Chapter 16

3.You’ll also need to add a section to web.config to configure the SMTP section. An example setting would look like this, of course you’d need to substitute in your own mail server’s settings:

4.To send an email successfully, you might also have to enable relaying on IIS. To do this open the Internet Information Services management console in the Administrative Tools window in the Control Panel. Set the properties for the Default SMTP Virtual Server as follows, on the Access tab, click Relay. Select Only the list below and then click Add and then under IP address type 127.0.0.1 and click OK. Click Apply, click OK, and then exit the Internet Information Services management console.

<system.net>

<mailSettings>

<!-- these settings define the mail server settings from: the user name from which the email is sent - this is the application that is sending the message host: the name of your mail server

userName: the name the application will use to log into the mail server password: the password for the above user name

-->

<smtp from=”admin@your-domain.com”> <network host=”your-mail-server-name

userName=”your-user-name password=”your-password” />

</smtp>

</mailSettings>

</system.net>

5.Run the application and wait. You should receive an e-mail within the minute.

How It Works

Instead of placing something in the Event Log, you are now e-mailed the details that you previously viewed in the Application section of the Event Log. To do this you changed the rules and provider elements to specify a different provider.

The health monitoring features of ASP.NET 2.0 are a real godsend for those of us trying to maintain an application and for either tracking down security breaches or hard-to-find bugs.

Scalability

The last scenario you might have to consider is what happens if the web site takes off. What happens if instead of the anticipated hundreds or thousands, you get millions of visitors? On one level this might hit the performance of your application, it might exceed limits within your application, and it also might exceed the limits of your hardware.

For the former you should always consider when you create arrays and databases not to impose small limits on them. For the latter you might end up having to move your site onto a web farm (a collection of servers, any one of which can receive a request and deal with it). However, when creating your first site you’re unlikely to have to worry about these considerations, but it never hurts to make sure.

622

Deployment, Builds, and Finishing Up

Where To Now?

Perhaps the most difficult question to answer in this entire book is what to do next. You’ve built an application, and if you’ve managed to do the exercises as well you will have a fairly good grounding in ASP.NET 2.0 already, but probably not one that will let you walk into a well-paid job, so how can you improve on it? If you follow some philosophies you can find yourself signed up for thousand-dollar courses, or with a booklist the size of a wardrobe next, but while possibly helpful I don’t think these are essential or advised courses of action.

I’m often asked by readers what is the most important part of becoming a developer or programmer, and the answer I give is hands-on experience. You read about coding and coding techniques until you’re blue in the face but until you’re faced with a problem that you have to solve, you can’t really get a feel for coding. I think the first tentative steps on most coders’ route are taking an existing application and modifying it. Improving it. Adding to it. That’s what you should start now. Go back to the Wrox United application and start amending it. Think how you could improve it. Would you change the design, the way it works? Add some news articles, some new players, some new graphics, and get comfortable with the way it works. Then go away and think of an application that would be useful to you, but not too ambitious, and start creating that. If you can’t think of anything then e-mail your friends to see if they’ve got anything that needs doing.

Developing is a very proactive job. The idea of a coder sitting in a room with no contact to the outside world is an outdated one. If your code is being used by thousands of people, you should at least be talking to a fraction of them. If you talk to people, they will undoubtedly have a million ideas you’d never have considered.

References

A lot of a developer’s time will be spent on the Web looking up new concepts or error messages. Here’s a list of some good places to start, if you get stuck:

http://p2p.wrox.com: The Wrox forums and first place to ask with trouble on the book.

http://www.developerfusion.co.uk: The UK developer community for ASP.NET.

http://www.asp.net: The official Microsoft ASP.NET web site.

http://msdn.microsoft.com/asp.net/: The MSDN reference for all things ASP.NETrelated.

http://www.15seconds.com: Great magazine-like site with loads of free articles on ASP.NET 2.0.

http://beta.asp.net: Microsoft’s coverage of the latest version of ASP.NET.

http://www.dotnet247.com: A compendium, online cross-reference of all the Usenet newsgroups, including all of the ASP.NET 2.0 ones.

http://msdn.microsoft.com/coding4fun/default.aspx: MSDN’s site on fun coding topics.

623

Chapter 16

Summar y

This hasn’t really been a chapter about a particular subject, but rather an extended wrapping up and welcome to the real world. To those ends, this chapter discussed the following:

How you can go about deploying the Wrox United site with both Visual Web Developer and the command-line prompt tool.

The importance of testing your application once it has been deployed and getting your potential users or your customer to test it as well.

Maintenance, and introduced very briefly ASP.NET 2.0’s new instrumenting features, the <healthMonitoring> element, and how you might go about using it.

A quick recheck of the Wrox United application and what you learned from it.

Some suggestions about what you should do next after finishing this book.

You’re now ready to start developing without our help. Please begin.

Exercises

1.You are now developer and maintainer of the Wrox United web site. What else would you add to the Wrox United site? Make a list of possible add-ons, and prioritize them with the simplest to do first. Implement some of these add-ons. Just to get you thinking, you could add a facility to post pictures or video clips from Wrox United matches, or you could add player biographies. The possibilities are limitless here, so be creative!

624

A

Exercise Answers

Chapter 1

Exercise 1

Explain the differences among the .NET 2.0 Framework, ASP.NET 2.0, VWD, and IIS.

Solution

The .NET 2.0 Framework is a very broad set of code (contained in classes) that is used by many Microsoft products, including servers, database managers, and web servers.

ASP.NET 2.0 is a subset of the .NET 2.0 Framework that holds the classes used to create dynamic web pages.

VWD is a tool used to create ASP.NET 2.0 web pages.

IIS is a web server that can execute the ASP.NET 2.0 code to create dynamic web pages.

Exercise 2

List some differences between Cassini and IIS.

Solution

IIS is a powerful web server designed to support publicly deployed sites. IIS scales to heavy loads, multiple servers, and multi-processor machines. It has a robust security model. The user of IIS is a special account named ASPNET. IIS does not expect pages to be in development while it is serving them, so it does not lock pages.

Cassini is a lightweight web server designed to give developers a quick and easy way to serve a page on their development machine. Cassini cannot handle more than one of visitors at once. The user of Cassini is whoever is logged in to Windows at the time, and thus the security model is weak. Cassini locks a page in VWD while it is being served.

Appendix A

Exercise 3

When you drag the title bar of the toolbar it will only go to certain locations and certain sizes. How can you put the title bar where you want it?

Solution

Select the toolbar and choose Menu Windows Floating. You can now place the toolbar in the size and location you desire.

Exercise 4

How can you copy a .jpg file in C:\MyPhotos into your site for display on a page?

Solution

From VWD Solution Explorer you can right-click the receiving folder and then select Add Existing Item. Navigate to C:\Photos and select the photo of interest. Click OK.

A less powerful technique is to start from Windows Explorer and you can copy the .jpg file from its original folder to C:\Website\MyWebName. However, the file will be not immediately visible in VWD Solution Explorer.

Exercise 5

You want to add a subfolder to your site, but folder is not one of the items listed in Add Items. Why?

Solution

Adding a folder has its own entry in the menu that appears by right-clicking a folder in the Solution Explorer.

Exercise 6

Microsoft has written extensive code to make it easier for programmers to create web pages. How does a programmer actually use that code?

Solution

The code is available in server-side controls. These are tags that look like HTML but start with <asp: ... >. Within the control you can set properties to determine the appearance and behavior of the control.

Exercise 7

Why are there no tools in the General panel of the Toolbox?

Solution

The General panel is a place for you to place snippets of code that you copy from a page and expect to use again later. It starts out empty.

626

Exercise Answers

Chapter 2

Exercise 1

Describe the functional difference between the Web.config file and Global.asax.

Solution

Web.config contains values (settings), whereas Global.asax contains code.

Exercise 2

What files discussed in this chapter are in XML format?

Solution

Web.config and webSite.map.

Exercise 3

Take a look at the code below for a Content page. Why does it lack directives and tags?

<!DOCTYPE HTML PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”> <head ></head>

Solution

This code lacks directives and tags because those are supplied by the Master page.

Exercise 4

What values must match between a set of Master and Content pages?

Solution

The Content page’s first line directive of MasterPageFile must match the name of the master file.

The value of xxx in the Content page’s <asp:content ... ContentPlaceHolder ID=”xxx”> must match the ID of the Master page’s <asp:ContentPlaceHolder ID=”xxx”>.

Chapter 3

Exercise 1

Practice using the drag-and-drop functionality of VWD to put together a simple web page that displays the following information:

The WroxUnited Logo (available for free download from www.wrox.com — or just use any small image of your choice).

627

Appendix A

The names of the players and some information about each of them, arranged in a table. Refer to Figure 3-44 for visual reference.

Solution

The finished page should look like Figure A-1 when it is run.

Figure A-1

The HTML that generates this page will vary according to how you set up your table, but it should be similar to the following code:

<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>

<title>Untitled Page</title> </head>

<body>

<form id=”form1” runat=”server”> <img src=”badge.gif” />

<br />

This is my fan site for the Wrox United football team! I’ve met a few of the team

- here’s my notes from my meetings:<br /> <br />

<table width=”600” cols=”2”> <tr>

<td style=”width: 150px”> Player</td>

<td style=”width: 400px”> Notes</td>

</tr>

628