创建自定义配置的第二步是创建解释配置文件的 XML 的类,并使用该信息填充配置对象。此类必须实现 System.Configuration.IConfigurationSectionHandler 接口。此接口只有一个方法,称为 Create。以下给出了 ProcessViewerSectionHandler 的 Visual Basic .NET 源(请参阅 C# 源的下载)。
Public Function Create(ByVal parent As Object, _
ByVal configContext As Object, _
ByVal section As System.Xml.XmlNode) As Object _
Implements Configuration.IConfigurationSectionHandler.Create
' 节具有以下格式:
'<processView
' localOnly="true|false"
' requestLimit="<=100"
' enabled="true|false"
' permittedHosts="comma-delimited list of IP addresses" />
Dim result New ProcessViewerConfiguration()
Dim config As New ConfigurationHelper(section)
Dim max As Integer
Dim hosts As String
Const delimiter As String = ", "
Const MaximumReturnCount As Integer = 100
'确认设置,并设定
result.Enabled = config.GetBooleanAttribute("enabled")
result.LocalOnly = config.GetBooleanAttribute("localOnly")
max = config.GetIntegerAttribute("requestLimit")
If max <= MaximumReturnCount Then
result.requestLimit = max
End If
hosts = config.GetStringAttribute("permittedHosts")
result.PermittedHosts = hosts.Split(delimiter.ToCharArray())
Return result
End Function
Create 方法传递了三个对象:
- parent - 表示父配置节(如果可用)。
- configContext - 表示 HttpConfigurationContext 对象,即 Web 配置的剩余部分。可以使用此对象从当前 web.config 文件中检索值。
- section - 最为重要的参数,实际的配置节。使用此对象填充配置对象。
以上代码使用了 ConfigurationHelper 对象。此对象是一个用于从节中检索特定数据类型的简单对象。此类的代码如下所示。
Friend Class ConfigurationHelper
Dim _section As XmlNode
Public Sub New(ByVal configSection As XmlNode)
_section = configSection
End Sub
'接受 true/false、yes/no
Public Function GetBooleanAttribute(ByVal name As String) As Boolean
Dim value As String
Dim result As Boolean
value = GetStringAttribute(name).ToLower()
If ((Boolean.TrueString.ToLower() = value) _
OrElse (value = "yes")) Then
result = True
Else
result = False
End If
Return result
End Function
Public Function GetIntegerAttribute(ByVal name As String) As Integer
Dim value As String
Dim result As Integer
value = GetStringAttribute(name)
result = Int32.Parse(value)
Return result
End Function
Public Function GetStringAttribute(ByVal name As String) As String
Dim theAttribute As XmlAttribute
Dim result As String
theAttribute = _section.Attributes(name)
If Not theAttribute Is Nothing Then
result = theAttribute.Value
End If
Return result
End Function
End Class
要使用这个节处理程序和配置对象,必须将其注册在相应的 ASP.NET 配置文件中。因为可以在任何进程中调用该类,所以最好将其添加到 machine.config 文件中。在 machine.config 类的 configSection 节中的相应位置注册节处理程序(我将其添加到了 system.web 节中)
<sectionGroup name="system.web">
... other sections
<section name="processView"
type="Microsoft.Samples.Msdn.Web.ProcessViewerSectionHandler,
MsdnProcessHandler, Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=f5f94c20bb90ce64" />
</sectionGroup>
注册完毕后,便可以向 machine.config 文件中添加节,并且该类将成为可配置类。
小结
创建 HTTP 处理程序可以提供超出 ASP.NET. 功能的强有力机制,使得开发者避开页面模型,并创建、修改或扩展 Web 站点的内容。通过添加用于查看 ASP.NET 的进程历史记录的 HTTP 处理程序,可以诊断代码或服务器中导致客户投诉的问题,例如代码中存在内存泄漏或未处理的异常,或服务器的内存不足。
创建并安装 HTTP 处理程序后,您将可以更敏锐地发现在此重要进程中发生的状况。到时候您就会有时间来喝杯咖啡,而不是忙于追查 Web 站点进程重新启动的原因了。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




