309045 HOW TO: Create a custom ASP.NET configuration segment processing program in Visual C # .NET (from mkba)

xiaoxiao2021-03-06  72

The release number of this article has been CHS309045

For Microsoft Visual Basic .NET versions of this article, see

318457.

This article references the following Microsoft .NET Framework Class Bank Name Space:

System.Configuration System.xml

This task content

summary

Create a configuration section handler and its component full code list test configuration section handler troubleshooting reference

SUMMARY This article describes how to create an ASP.NET custom configuration section processing program using Visual C # .NET.

Back to top

Creating a configuration section handler and its components below demonstrate how to create a configuration segment handler and its components. In order to make you better maintained and reuse these code, the following steps demonstrate how to create a name containing static methods.

Class of Confighelper. These static methods help you analyze and retrieve the XML properties in the configuration file. Because of the construction

Confighelper's code uses enumeration and a string in the configuration section, so

Confighelper contains two methods:

GetEnumValue and

GetStringValue.

The GetEnumValue method is used to analyze the properties in the section in a predefined value, confirm that the property value is valid, and then returns to this property and attribute value.

GetStringValue method Analyze the properties in the configuration section and then returns the properties and attribute values.

Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual C # Projects under Project Type, and then click Category Library under Templates. In the Name Text box, type myconfig, then click OK. Add a reference to the System.Web.dll assembly. Rename Class1.cs is rename myconfig.cs. From the Solution Explorer, open MyConfig.cs. Add the following namespace declaration to the top of the file: USING SYSTEM.CONFIGURATION;

Using system.Web;

Using system.xml; Delete the default class definition. Add an enumeration to save the properties of the custom configuration section: Public Enum LevelSetting

{

HIGH,

MEDIUM,

Low,

None

} Creating a class named MyConfigSection to save configuration information. This class is an object returned by the CREATE method. Public Class MyConfigsection

{

Private level setting level = levelsetting.none;

Private string name = "";

Public myconfigsection (LevelSetting _Level, String _Name)

{

Level = _level;

Name = _name;

}

Public LevelSetting Level

{

Get {return.

}

Public String Name

{

Get {return name;}

}

} Classified as follows as follows: Internal Class Confighelper

{

// Helper Method for Retrieving Enum Values ​​from XMLNode.

Public Static XMLNode GetEnumValue

(XMLNode _Node, String _ttribute, Type _EnumType, Ref Int_VAL)

{

XMLNode a = _node.attributes.removenamedItem (_Attribute);

IF (a == NULL)

Throw New ConfigurationException ("Attribute Required: _Attribute);

IF (Enum.Indefined (_UNumType, A.Value)

_val = (int) enum.parse (_UNumType, a.value);

Else

Throw New ConfigurationException ("INVALID Level: '" A.Value "'", A);

Return A;

}

// Helper Method for Retrieving String Values ​​from XMLNode.

Public static xmlnode getStringValue (XMLNode _Node, String _ATtribute, ref string _val)

{

XMLNode a = _node.attributes.removenamedItem (_Attribute);

IF (a == NULL)

Throw New ConfigurationException ("Attribute Required: _Attribute);

Else

_val = a.Value;

Return A;

}

} Note: You can also create a Helper method for each data type for your configuration festival (for example, GetInetValue and getBooleanValue). Create a class named myconfigsectionhandler. This class inherits the CREATE method of the IconfigurationSectionHandler interface and implements the interface. In the Create method, this code retrieves the value from the configuration file using the ConfigHELPER class. This example is then created and returned to myconfigsection object. MyConfigSectionHandler Category As shown below: Public Class MyConfigsectionHandler: iConfigurationSectionHandler

{

Public Virtual Object Create (Object Parent, Object ConfigContext, XMLNode Section)

{

INT ILVEL = 0;

String sname = "";

Confighelper.GetenumValue (Section, "Level", TypeOf (LevelSetting), Ref Ilevel

Confighelper.getstringValue (section, "name", ref sname;

Return New MyConfigsection (LevelSetting) Ilevel, SNAME;

}

} Save and compile the project.

Back to top

The full code list is in the last form, your class file should be as follows:

Using system;

Using system.Web;

USING SYSTEM.XML;

Using system.configuration;

Namespace myconfig

{

Public Enum LevelSetting

{

HIGH,

MEDIUM,

Low,

None

}

Public Class MyConfigsectionHandler: iConfigurationSectionHandler

{

Public Virtual Object Create (Object Parent, Object Configcontext, XMLNode Section) {

INT ILVEL = 0;

String sname = "";

Confighelper.GetenumValue (Section, "Level", TypeOf (LevelSetting), Ref Ilevel

Confighelper.getstringValue (section, "name", ref sname;

Return New MyConfigsection (LevelSetting) Ilevel, SNAME;

}

}

Public Class MyConfigsection

{

Private level setting level = levelsetting.none;

Private string name = NULL;

Public myconfigsection (LevelSetting _Level, String _Name)

{

Level = _level;

Name = _name;

}

Public LevelSetting Level

{

Get {return.

}

Public String Name

{

Get {return name;}

}

}

INTERNAL CLASS Confighelper

{

Public Static XMLNode GetEnumValue

(XMLNode _Node, String _ttribute, Type _EnumType, Ref Int_VAL)

{

XMLNode a = _node.attributes.removenamedItem (_Attribute);

IF (a == NULL)

Throw New ConfigurationException ("Attribute Required: _Attribute);

IF (Enum.Indefined (_UNumType, A.Value)

_val = (int) enum.parse (_UNumType, a.value);

Else

Throw New ConfigurationException ("Invalid Level", A);

Return A;

}

Public static xmlnode getStringValue (XMLNode _Node, String _ATtribute, ref string _val)

{

XMLNode a = _node.attributes.removenamedItem (_Attribute);

IF (a == NULL)

Throw New ConfigurationException ("Attribute Required: _Attribute);

Else

_val = a.Value;

Return A;

}

}

}

Back to top

Test configuration handler

Open Visual Studio .NET. In the New Project dialog box, click the Visual C # item below the project type, and then click the ASP.NET web application below the template. Specify name and location for your new project. Add a reference to MyConfig.dll. Open the web.config file. Add the following code in the section:

Add the following code in the section: Open the webform1.aspx code hidden (code-behind) file, the default name is Webform1.aspx . Add the following namespace declaration at the top of WebForm1.aspx.cs: Using myconfig; add the following code to the page_load event: This code calls the getConfig method to retrieve the instance of the MyConfigSecion object, then fill in the two attribute values ​​of the object. Myconfigsection s = (myconfigsection) context.getconfig ("System.web / myconfig);

Response.write ("Level:" S.Level "
");

Response.write ("Name:" S.Name); saves and compiles the application. View this page in your browser. The output is as follows: level: high name: Hello World

Back to top

Troubleshooting Creating a Custom ASP.NET Configuration Section Handling Program

Comply with the following principles when ICONFIGURATIONSECTIONHANDLER interface:

The class instance of the IconfigurationSectionHandler interface must be a thread safe and stateless. You must be able to simultaneously call the iConfigurationSectionHandler.create method from multiple threads. The configuration object returned by IconfigurationSectionHandler.create must be a thread safe and not changeable. Do not modify the parent parameters of iCONFIGURATIONSECTIONHANDLER.CREATE. Because the system cache configuration object is configured, it is important to modify the parent parameters of iConfigurationSectionHandler.create. For example, if the return value of iConfigurationSectionHandler.create is only a small modification of the parent parameter, you must modify the copy of the parent parameter, not the original value.

Back to top

Refer to additional information about ASP.NET configuration, click the following article number to view the corresponding Microsoft Knowledge Base article:

307626 INFO: ASP.NET Configuration overview (Info: ASP.NET Configuration Overview)

307513 PRB: Access Violation Occurs WHEN You Use A Custom Configuration Handler In an ASP.NET Application That Under Stress (PRB: Access Conflict when using custom configuration sections in a load-bearing ASP.NET application

Back to top

The information in this article applies to:

Microsoft ASP .NET (provided with .NET Framework) Microsoft Visual C # .NET (2002)

转载请注明原文地址:https://www.9cbs.com/read-108944.html

New Post(0)