Build a web service for the ASP.NET application settings

xiaoxiao2021-03-06  104

First, what is application settings

Application settings typically refer to a series of parameters closely related to the application. In a form-based Windows application, application settings mainly include parameters such as the location, size of the application layout; the color, shape, etc. of the application appearance; in addition, it includes the language, culture, etc. of the application, and Data source and connection strings, etc. These parameters are widely used in program development, and some are used to keep the application's consistency, continuity, such as saving the form size, location and background color at the end of the application, restore these settings when the application is executed next time. Further parameters make the program code more concise and efficient, such as saving the database connection string for multiple calls.

With the form-based Windows application is not exactly the same, the web-based ASP.NET application has its own specialty, which is a network. This determines the ASP.NET application settings, in addition to including the above parameters,

# Authentication and authorization and other security mechanisms

# 网 网 Transport Protocol Type and HTTP Processor for Specific Type Documents

# 事 事 处理 处理

# Set timeout for all pages

# Customize the wrong page to replace the default IIS error page

# Session and session status information

# 页面 缓行

# Custom and expand

It can be seen that the ASP.NET application sets more parameter types. In actual development, they are not only used to improve application security, efficiency, but also to implement management and customize requirements for specific users.

Second, why should I set an ASP.NET application to Web services?

With regard to the advantages of web services, it is not necessary to mention that the publicity of media covered and extensive applications in many areas is sufficient. However, it is still necessary to emphasize that the Web service is actually an ASP.NET application, but it is only re-organized. In particular, the Web service provides an implementation for shared objects between different applications. With a simple reference, you can access another program implemented in the program, not just asp.net, only the browser can be implemented. Obviously, Web services also have many features and features that are generally ASP.NET applications. ASP.NET and Web services have state management functions is a typical example.

However, what is the advantage of setting an ASP.NET application to Web services? On the one hand, you can get answers from software (application) to reuse. Imagine that if the client application (source) needs to use another application (target) set parameters, then configure the target application settings For the web service, it is convenient to call in the source application, just like the same settings in this unit. On the other hand, the WEB service (ASP.NET application application) special storage has a mechanism for its application settings (the web.config file storage settings will be described below) enables applications to upgrade the application cross-platform, across the Internet, and application. Xcopy deployment is possible.

Third, store the ASP.NET application settings

We should already know that settings for Windows applications are typically saved in the registry. Save a specific corresponding setting value by writing the "key value pair" for the registry. Then, the corresponding set value is taken by reading the "key value pair" of the registry. It seems that the process itself is not complicated, coupled with the .NET framework to provide a rich class library support, making the operation registry to read and write application settings easier (see writing questions about the registry reading and writing. The article is not expanded here).

However, the registry itself is a fairly sensitive area, which inevitably causing security hazards, even if the operation on the local host is quite careful, not to include the network environment that is not expensive, it is more Should be cautious when reading and writing of the public registry! Or, simply find another way to replace! It is because of this reason, ASP.NET application settings are stored in addition to storage in the registry, more, by storing:

# Application object

# Web.config file

Fourth, use the Application object storage application settings

For us, Application object is quite familiar. It is ASP.NET to stay from the ASP to simplify one of the two objects of the application status management (the other is the session object). As an ASP.NET application, Web services can also access Application objects like any other web application.

In ASP.NET, Application objects can be considered a global variable in advanced languages. It is consistent in the application to implement global information sharing between multiple sessions and requests in the ASP.NET application. (It is necessary to mention that the ASP.NET application is a sum of a virtual directory on a single web server and all files, pages, handlers, modules, and code in their subdirectories.)

What is different from normal global variables is that the ASP.NET application status Application object is created when the client requests any URL resources from a particular ASP.NET application virtual directory. Each ASP.NET application on the web server has to create a separate instance. Then publicly disclose references to each instance by this Application object.

Thus, the Application object is appropriate to save data that requires different users, and records the settings of the application, so that it can be run in all code access in the same web application, further, will contain the Application object. Method External as web services to share objects in the application and operate the application settings. The status programming of the application access period is read, and the interoperability programming of the remote database is read in a similar manner to implement the application sharing.

The following code uses the Application object, records the number of accesses for a particular application, and external method is used as a web service provider call:

[WebService (Namespace = "http://www.thjx.com")] Public class application: system.web.services.Webservice {///

/// Return application Access number /// [WebMethod (Description = "Returns the number of times")]] public int getAppAccesscount (String Key) {if (Application [key] == null) {Application.lock (); // Lock Application object Synchronous access Application [key] = 1; Application.unlock (); // Cancellation} else {Application.lock (); Application [key] = int32.parse (Application [key] .tostring ()) 1; Application .Unlock ();} returnck (int) application [key];}} method getAppAccessCount (String Key) is incremented by 1 unit amount after a particular application is executed, and saves in the application setting variable, next time Access from any user will increment the setting on this variable basis. 5. Use web.congfig file storage application settings

From the above, you can know that the ASP.NET application needs to configure a lot of special settings, including setting up timeouts for all pages, customized page instead of the default IIS error page and security settings and authorization level settings. It seems that so many types of settings must be very troublesome, fortunately, the ASP.NET ends the history of manual configuration, that is, save the settings in the web.config file. This is different from another way to save the application settings using the registry or Application object to save the application settings. Since the web.config file exists in the application root directory, the application settings saved in this way will greatly enhance the application cross-platform usage and scalability.

Use the web.config file storage application settings There are many advantages. Web.config is actually a plain text file. Obviously, the configuration information stored in a plain text file is very easy to modify, and is not a traditional ASP, any pair configuration Setting modifications You do not need to restart the web server, you can apply it to the current web application; at the same time, the settings of the configuration are automatically applied to the current folder and all its subfolders, making the Xcopy that the true compatible host becomes possible We only need to copy all web application files in another IIS virtual directory to implement application deployment; in addition, configurations of some specific tasks such as form-based authorization can only be implemented by using a web.config file.

Here, it is necessary to briefly talk about the construction of the web.config file. The web.config file is a standard XML file that follows all the specifications of the correct XML document. It includes multiple parts, each partially handles a special task. The web.config file consists of many sub-sections, including verification section, security section, error handling section, and web service sections. And save the application configuration information in the form of key value pairs.

As a standard XML document, the root element of the web.config file is , which contains a child element , while the latter further includes many configuration portions. The web.config file contains many tags, each tag, corresponding to a so-called "section". As, it means an authentication of authentication, indicating authorized Authorization, customized CustomerRors, session setting sessionState, and more. Although the web.config file provides a large space for user-defined, we may still need some user settings. The AppSetting section is used to meet this requirement, please remember that this part is not in the tag, but stored in the tag, database connection character Strings, Email server addresses and log files save paths, usual page can be placed in this section.

The following code snippet is a web.config file setting section, which defines the application settings, the AppSettings section, which contains the database connection string and another user setting logfilePath key value:

The .NET framework supports the ASP.NET application to access the application settings. Its ConfigurationSettings class provides access to configuration settings in the specified configuration section. The public property configurationSettings.AppSettings property of this class can get the configuration settings in the Element Configuration section, which is a nameValueCollection, which contains the name / value pair of configuration settings:

Public static namevaluecollection.appsettings {get;}

The following Web service method is used to get application configuration settings: use system.configuration;

[WebMethod] public string getAppsetting (String Key) {Return ConfigurationSettings.AppSettings [key];

Note that the appsetting property is read-only. To modify the application settings, you must edit the web.config file. At the same time, if the Key key does not exist in the web.config file, read the AppSettings property will raise an error.

The ConfigurationSettings class also provides a public method ConfigurationSettings.getConfig to return configuration settings for user-defined configuration segments.

Public Static Object getConfig (String SectionName);

Where parameter sectionname denotes the configuration section to be read. The following Web service method is used to get the specified key value of the user-defined configuration:

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

New Post(0)