1. Trang chủ
  2. » Công Nghệ Thông Tin

Professional ASP.NET 3.5 in C# and Visual Basic Part 150 potx

10 288 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 256,1 KB

Nội dung

Evjen c31.tex V2 - 01/28/2008 4:01pm Page 1455 Chapter 31: Configuration requirePermission="false" allowDefinition="MachineToApplication"/ > < sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" > < section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/ > < section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/ > < section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/ > < section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/ > < /sectionGroup > < /sectionGroup > < /sectionGroup > < /configSections > Once you have made this reference to the System.Configuration.NameValueFileSectionHandler object and have given it a name (in this case, MyCompanyAppSettings ), then you can create a section in your web.config that makes use of this reference. This is illustrated in Listing 31-47. Listing 31-47: Creating your own custom key-value pair section in the w eb.config < configuration > < MyCompanyAppSettings > < add key="Key1" value="This is value 1" / > < add key="Key2" value="This is value 2" / > < /MyCompanyAppSettings > < system.web > < ! Removed for clarity > < /system.web > < /configuration > After you have this in place within your web.config file, you can then programmatically get access to this section, as illustrated in Listing 31-48. 1455 Evjen c31.tex V2 - 01/28/2008 4:01pm Page 1456 Chapter 31: Configuration Listing 31-48: Getting access to your custom section in the web.config file VB Dim nvc As NameValueCollection = New NameValueCollection() nvc = System.Configuration.ConfigurationManager.GetSection("MyCompanyAppSettings") Response.Write(nvc("Key1") + " < br / > ") Response.Write(nvc("Key2")) C# NameValueCollection nvc = new NameValueCollection(); nvc = ConfigurationManager.GetSection("MyCompanyAppSettings") as NameValueCollection; Response.Write(nvc["Key1"] + " < br / > "); Response.Write(nvc["Key2"]); For this to work, you are going to have to import the System.Collections.Specialized namespace into the file, as this is where you will find the NameValueCollection object. Using the DictionarySectionHandler Object The DictionarySectionHandler works nearly the same as the NameValueFileSectionHandler. The dif- ference, however, is that the DictionarySectionHandler returns a HashTable object instead of returning an Object . This handler is presented in Listing 31-49. Listing 31-49: Making a reference to the DictionarySectionHandler object < configSections > < section name="MyCompanyAppSettings" type="System.Configuration.DictionarySectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" restartOnExternalChanges="false" / > < ! Removed for clarity > < /configSections > Once in place, you can then make the same MyCompanyAppSettings section in the web.config file, as shown in Listing 31-50. Listing 31-50: Creating your own custom key-value pair section in the w eb.config < configuration > < MyCompanyAppSettings > < add key="Key1" value="This is value 1" / > < add key="Key2" value="This is value 2" / > 1456 Evjen c31.tex V2 - 01/28/2008 4:01pm Page 1457 Chapter 31: Configuration < /MyCompanyAppSettings > < system.web > < ! Removed for clarity > < /system.web > < /configuration > Now that the web.config file is ready, you can call the items from code using the Configuration API, as illustrated in Listing 31-51. Listing 31-51: Getting access to your custom section in the web.config file VB Dim ht As Hashtable = New Hashtable() ht = System.Configuration.ConfigurationManager.GetSection("MyCompanyAppSettings") Response.Write(ht("Key1") + " < br / > ") Response.Write(ht("Key2")) C# Hashtable ht = new Hashtable(); ht = ConfigurationManager.GetSection("MyCompanyAppSettings") as Hashtable; Response.Write(ht["Key1"] + " < br / > "); Response.Write(ht["Key2"]); Using the SingleTagSectionHandler Object The SingleTagSectionHandler works almost the same as the previous NameValueFileSectionHandler and DictionarySectionHandler . However, this object looks to work with a single element that contains the key-value pairs as attributes. This handler is presented in Listing 31-52. Listing 31-52: Making a reference to the DictionarySectionHandler object < configSections > < section name="MyCompanyAppSettings" type="System.Configuration.SingleTagSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" restartOnExternalChanges="false" / > < ! Removed for clarity > < /configSections > Once in place, you can make a different MyCompanyAppSettings section in the web.config file, as presented in Listing 31-53. 1457 Evjen c31.tex V2 - 01/28/2008 4:01pm Page 1458 Chapter 31: Configuration Listing 31-53: Creating your own custom key-value pair section in the w eb.config < configuration > < MyCompanyAppSettings Key1="This is value 1" Key2="This is value 2" / > < system.web > < ! Removed for clarity > < /system.web > < /configuration > Now that the web.config file is complete, you can call the items from code using the Configuration API, as illustrated in Listing 31-54. Listing 31-54: Getting access to your custom section in the web.config file VB Dim ht As Hashtable = New Hashtable() ht = System.Configuration.ConfigurationManager.GetSection("MyCompanyAppSettings") Response.Write(ht("Key1") + " < br / > ") Response.Write(ht("Key2")) C# Hashtable ht = new Hashtable(); ht = ConfigurationManager.GetSection("MyCompanyAppSettings") as Hashtable; Response.Write(ht["Key1"] + " < br / > "); Response.Write(ht["Key2"]); Using Your Own Custom Configuration Handler You can also create your own custom configuration handler. To do this, you first need to create a class that represents your section in the web.config file. In your App_Code folder, create a class called MyCompany Settings . This class is presented in Listing 31-55. Listing 31-55: The MyCompanySettings class VB Public Class MyCompanySettings Inherits ConfigurationSection < ConfigurationProperty("Key1", DefaultValue:="This is the value of Key 1", _ IsRequired:=False) > _ Public ReadOnly Property Key1() As String Get Return MyBase.Item("Key1").ToString() 1458 Evjen c31.tex V2 - 01/28/2008 4:01pm Page 1459 Chapter 31: Configuration End Get End Property < ConfigurationProperty("Key2", IsRequired:=True) > _ Public ReadOnly Property Key2() As String Get Return MyBase.Item("Key2").ToString() End Get End Property End Class C# using System.Configuration; public class MyCompanySettings : ConfigurationSection { [ConfigurationProperty("Key1", DefaultValue = "This is the value of Key 1", IsRequired = false)] public string Key1 { get { return this["Key1"] as string; } } [ConfigurationProperty("Key2", IsRequired = true)] public string Key2 { get { return this["Key2"] as string; } } } You can see that this class inherits from the ConfigurationSection and the two properties that are created using the ConfigurationProperty attribute. You can use a couple of attributes here, such as the DefaultValue , IsRequired , IsKey ,and IsDefaultCollection . Once you have this class in place, you can configure your application to use this handler, as illustrated in Listing 31-56. Listing 31-56: Making a reference to the MyCompanySettings object < configSections > < section name="MyCompanySettings" type="MyCompanySettings" / > < ! Removed for clarity > < /configSections > You can now use this section in your web.config file, as illustrated in Listing 31-57. 1459 Evjen c31.tex V2 - 01/28/2008 4:01pm Page 1460 Chapter 31: Configuration Listing 31-57: Creating your own custom key-value pair section in the w eb.config < configuration > < MyCompanySettings Key2="Here is a value for Key2" / > < system.web > < ! Removed for clarity > < /system.web > < /configuration > From there, you can programmatically access this from code, as illustrated in Listing 31-58. Listing 31-58: Getting access to your custom section in the web.config file VB Dim cs As MyCompanySettings = New MyCompanySettings() cs = ConfigurationManager.GetSection("MyCompanySettings") Response.Write(cs.Key1 + " < br / > ") Response.Write(cs.Key2) C# MyCompanySettings cs = ConfigurationManager.GetSection("MyCompanySettings") as MyCompanySettings; Response.Write(cs.Key1 + " < br / > "); Response.Write(cs.Key2); Summary In this chapter, you have seen the ASP.NET configuration system and learned how it does not rely on the IIS metabase. Instead, ASP.NET uses an XML configuration system that is human-readable. You also looked at the two different ASP.NET XML configuration files: ❑ machine.config ❑ web.config The machine.config file applies default settings to all Web applications on the server. However, if the server has multiple versions of the framework installed, the machine.config file applies to a particular framework version. On the other hand, a particular Web application can customize or override its own configuration information using web.config files. Using a web.config file, you can also configure the applications on an application-by-application or folder-by-folder basis. Next, you looked at some typical configuration settings that can be applied to an ASP.NET application, such as configuring connecting strings, session state, browser capabilities, and so on. Then you looked at an overview of new ASP.NET Admin Objects and learned how to program configuration files. Finally, you learned how to protect the configuration section using cryptographic algorithms. 1460 Evjen c32.tex V2 - 01/28/2008 4:05pm Page 1461 Instrumentation Many ASP.NET developers do more than just build an application and walk away. They definitely think about how the application will behave after it is deployed. Instrumentation is the task that developers undertake to measure and monitor their ASP.NET applications. Depending on the situ- ation, some instrumentation operations occur at design time, whereas others are ongoing processes that begin at runtime. ASP.NET 3.5 gives you greater capability to apply instrumentation techniques to your applications. You will find that the ASP.NET framework includes a large series of performance counters, the capability to work with the Windows Event Tracing system, possibilities for application tracing (covered in Chapter 24), and the most exciting part of this discussion — a health monitoring system that allows you to log a number of different events over an application’s lifetime. You can monitor a deployed application in several ways. First, you learn how to work with the Windows event log. Working with the Event Log When working with Visual Studio 2008, you can use the event log in the Server Explorer of the IDE in a couple of different ways. You can get to the event log section in the Server Explorer by expanding the view of the server you want to work with (by clicking the plus sign next to the server) until you see the Event Logs section. You also have the option to right-click the Event Logs node and select Launch Event Viewer from the list of available options. This selection displays the same Event Viewer you are familiar with. From the Event Viewer or from the Server Explorer you can work directly with events recorded to your system. The other option available from the Server Explorer is to expand the Event Logs node of the tree view in the Server Explorer so that you can see the additional nodes such as Application, Secu- rity, and System. If you are on Windows Vista, you will see a series of additional event categories. Expanding any of these sub-nodes allows you to see all the events that have been registered in the event log. These events are arranged by type, which makes browsing for specific events rather easy, as illustrated in Figure 32-1. Evjen c32.tex V2 - 01/28/2008 4:05pm Page 1462 Chapter 32: Instrumentation Figure 32-1 Reading from the Event Log It is possible to read and to write events to and from the event log from your .NET application. If you are interested in reading events from the event log, you can do so rather simply by using the EventLog object that is provided by .NET in the System.Diagnostics namespace. To see an example of using this object, you can create an ASP.NET page that displays all the entries contained within a specified event log. To create it, you need a DropDownList control, a Button control, and a GridView control. In Visual Studio .NET 2002 and 2003, a Components tab was located w ithin the Toolbox, and you could simply drag and drop the Event Log component onto your design surface in order to start working with it. In Visual Studio 2005, you won’t find this tab within the Toolbox, but it still isn’t too hard to find the objects you need. Listing 32-1 shows the simple ASP.NET page that enables you to easily display the contents of the event log. Listing 32-1: Displaying the content s of the event logs within the browser VB <%@ Page Language="VB" %> <%@ Import Namespace="System.Diagnostics" %> <script runat="server"> 1462 Evjen c32.tex V2 - 01/28/2008 4:05pm Page 1463 Chapter 32: Instrumentation Protected Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Dim el As EventLog = New EventLog() el.Source = DropDownList1.SelectedItem.Text GridView1.DataSource = el.Entries GridView1.DataBind() End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Working with Event Logs</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>Application</asp:ListItem> <asp:ListItem>Security</asp:ListItem> <asp:ListItem>System</asp:ListItem> </asp:DropDownList> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /><br /> <br /> <asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None"> <FooterStyle BackColor="Tan" /> <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /> <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" /> <HeaderStyle BackColor="Tan" Font-Bold="True" /> <AlternatingRowStyle BackColor="PaleGoldenrod" /> <RowStyle VerticalAlign="Top" /> </asp:GridView> </div> </form> </body> </html> C# <%@ Page Language="C#" %> <%@ Import Namespace="System.Diagnostics" %> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { EventLog el = new EventLog(); el.Source = DropDownList1.SelectedItem.Text; GridView1.DataSource = el.Entries; GridView1.DataBind(); } </script> 1463 Evjen c32.tex V2 - 01/28/2008 4:05pm Page 1464 Chapter 32: Instrumentation Note that you are going to have to run this with credentials that have administrative rights for this code sample to work. For this code to work, you import the System.Diagnostics namespace if you are not interested in fully qualifying your declarations. After you do so, you can create an instance of the EventLog object to give you access to the Source property. In assigning a value to this property, you use the SelectedItem.Text property from the DropDownList control on the page. Next, you can provide all the EventLog entries as the data source value to the GridView control. Finally, you bind this data source to the GridView. In the end, you get something similar to the results illustrated in Figure 32-2. Figure 32-2 As you can see, it is simple to pull all the events from the event log and write them to the browser screen. The next section looks at writing values back to the event log. Writing to the Event Logs Not only can you read from the event logs, but you can write to them as well. This can be quite handy if you want to record specific events in an event log. The following table provides a short description of the main event logs available to you. Windows Vista has other event categories, but these are the main event categories that you will use. 1464 . name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version =3. 5. 0.0, Culture=neutral, PublicKeyToken =31 BF3 856 AD364E 35& quot; requirePermission="false" allowDefinition="MachineToApplication"/ > < section. section, as illustrated in Listing 31 -48. 1 455 Evjen c31.tex V2 - 01/28/2008 4:01pm Page 1 456 Chapter 31 : Configuration Listing 31 -48: Getting access to your custom section in the web.config file VB Dim. > < /configSections > Once in place, you can make a different MyCompanyAppSettings section in the web.config file, as presented in Listing 31 - 53 . 1 457 Evjen c31.tex V2 - 01/28/2008 4:01pm Page 1 458 Chapter 31 : Configuration Listing

Ngày đăng: 05/07/2014, 19:20

TỪ KHÓA LIÊN QUAN