Set up building Web Services for Asp.net applications

Source: Internet
Author: User
1. What is an application? Program Set

Application settings usually refer to a series of parameters closely related to the application. In Windows applications based on forms, application settings mainly include parameters such as location and size that affect the application layout, and parameters such as color and shape that affect the application appearance. In addition, it also includes application language, culture, and other settings as well as data sources and connection strings. These parameter settings are widely used in program development, and some are used to maintain the consistency and continuity of the application, such as saving the form size, position, and background color at the end of the application, restore these settings when you run the application next time.CodeMore concise and efficient, such as saving database connection strings for multiple calls.

Unlike Windows applications based on forms, Web-based Asp.net applications have their own particularity and are network-oriented. This determines that the Asp.net application settings include not only the above parameters, but also,

# Identity authentication, authorization, and other security mechanisms

# Network transmission protocol type and HTTP handler for specific types of documents

# Transaction Processing

# Set timeout for all pages

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

# Session and session status information

# Page cache behavior

# Customization and Expansion

It can be seen that the parameter types set by the Asp.net application are more abundant. In actual development, they are not only used to improve application security and efficiency, but also easy to manage and customize requirements for specific users.

Ii. Why do we need to set the Asp.net application as a Web Service externally?

There is no need to mention the advantages of Web Services. The overwhelming publicity and wide application of the media in many fields have been enough to explain the problem. However, it should be emphasized that the Web Service is actually an Asp.net application, but it is re-organized. Specifically, Web Services provide the implementation of shared objects between different applications. Through simple reference, you can access the functions implemented by another program in the program, not just the functions implemented by Asp.net in the browser. Obviously, Web services also have many features and features of General Asp.net applications. Asp.net and web services both have the state management function, which is a typical example.

However, what are the advantages of setting the Asp.net application as an external web service? On the one hand, you can get the answer from the reusability of software (Application). Let's think about it. If the customer's application (source) needs to use the setting parameters of another application (target), then, configure the target application as a Web service so that it can be conveniently called in the source application, just as if the same settings are configured on the local machine. On the other hand, the Web Service (Asp.net application) has a special mechanism for storing its application settings (that is, the web. configuration file storage settings) make it possible to upgrade applications across platforms, across the Internet, and to deploy xcopy applications.

3. Storage Asp.net application settings

We should know that Windows application-based settings are generally stored in the registry. Write a "key-Value Pair" to the Registry to save the specific settings. Then, read the "key-Value Pair" in the registry to obtain the corresponding set value. It seems that the process itself is not complex, and the. NET Framework provides a wealth of class library support for registry read and write, making it easier to set up the Registry to read and write applications (for information on registry read and write, seeArticle).

However, the Registry itself is a very sensitive area, and its read/write operations inevitably lead to security risks, even if it is operated on a local host, it should be very careful, moreover, in an unpredictable network environment, you should be cautious when disclosing the Registry's read and write operations! Or, try another method!

For this reason, the Asp.net application settings are stored in addition to the registry, and are more stored in the following two ways:

# Application Object

# Web. config file

4. Use Application Object Storage application settings

For us, the application object is quite familiar. It is one of the two objects that Asp.net saves from ASP to simplify application state management (the other is the session object ). As an Asp.net application, Web services can access application objects just like any other web application.

In Asp.net, the application object can be regarded as a global variable in the advanced language. It is consistent in applications to achieve global information sharing between multiple sessions and requests in ASP. NET applications. (It is necessary to mention that ASP. NET applications are the sum of all files, pages, handlers, modules and code in a virtual directory on a single web server and its subdirectories .)

Unlike common global variables, the Asp.net application state application object is created when the client requests any URL resource from a specific virtual directory of the ASP. NET application for the first time. Each ASP. NET application on the Web server must create a separate instance. Then, the application object is used to publish a reference to each instance.

In this way, the application object is suitable for storing data that needs to be shared by different users and recording the settings of the application so that it can be accessed by all the code running in the same web application, in addition, the method that contains the application object is used as the Web service to share the object in the application and manipulate the application settings. State programming for the number of application accesses and interoperability programming for reading remote databases all achieve application setting sharing in a similar way.

The following code uses the application object to record the number of accesses to a specific application, and externally calls the method for the Web Service Supply Application:

[WebService (namespace = "http://www.thjx.com")]
Public class application: system. Web. Services. WebService
{
/// <Summary>
/// Return the number of times the application is accessed
/// </Summary>
[Webmethod (description = "returns the number of times a specific application is accessed")]
Public int getappaccesscount (string key)
{
If (application [Key] = NULL)
{
Application. Lock (); // lock the application object for Synchronous access
Application [Key] = 1;
Application. Unlock (); // unlock
}
Else
{
Application. Lock ();
Application [Key] = int32.parse (application [Key]. tostring () + 1;
Application. Unlock ();
}
Return (INT) application [Key];
}
}

The getappaccesscount (string key) method increases the number of visits per unit after a specific application is executed once and is saved in the application setting variables, the next access from any user will increment this setting based on this variable.

5. Use web. congfig to store application settings

As you can see above, the Asp.net application requires many special settings, including setting timeout for all pages, customizing error pages to replace the default IIS error page, security settings, and authorization level settings. It seems that it is very troublesome to set so many types. Fortunately, Asp.net has ended the manual configuration history, that is, saving the settings in the web. config file. This is different from the previously mentioned method of saving application settings by using the registry or application object. Because the Web. config file exists in the application root directory, the application settings saved in this way will greatly enhance the cross-platform usage and scalability of the application.

Use web. config file storage application settings have many advantages, Web. config is actually a plain text file. Obviously, the configuration information stored in plain text files is very easy to modify, and unlike the traditional ASP, any modification to the configuration settings does not need to restart the web server, so that it can be immediately applied to the current web application. At the same time, the configured configuration is automatically applied to the current folder and all its subfolders, to make xcopy compatible with hosts possible, we only need to copy all web application files in the other IIS virtual directory to implement Application Deployment. In addition, configuration of some specific tasks, such as form-based authorization, can only be performed through the Web. config file.

It is necessary to briefly discuss the structure of the web. config file. The Web. config file is a standard XML file and complies with all the rules of the correctly formatted XML document. It consists of multiple parts, each of which processes a special task. The Web. config file consists of multiple sections, including the verification section, Security Section, error handling section, and Web Service Section. The application configuration information is saved as a key-value pair.

As a standard XML document, the root element of the web. config file is <configuration>, which contains a sub-element <system. Web>, which contains many configuration parts. The Web. config file contains many tags, each of which corresponds to a so-called "section )". For example, authentication indicating authentication, authorization indicating authorization, custom wrong mermerrors, and session setting sessionstate.

Although the Web. config file provides a lot of space for user customization, we may still need some user settings. The deleetaskpart is used to meet this requirement. Remember that this part is not in the <system. web> </system. web> tag, but stored separately in the <configuration> </configuration> tag. The database connection string, email server address, and log file storage path are usually stored on this page.

The following code snippet is a web. the configuration section of the config file defines the application settings, that is, the appsettings section, which contains the database connection string and another user sets the logfilepath key-Value Pair:

<Configuration>
<Deleetask>
<Add key = "conn" value = "Server = (local); uid = sa; Pwd =; database = mydb"/>
<Add key = "logfilepath" value = "C: \ mylogs"/>
</Appsettings>
</Configuration>

The. NET Framework supports Asp.net application access application settings. Its configurationsettings class provides access to the configuration settings in the specified configuration section. You can obtain the configuration settings in the <deleetask> element configuration section for the public attribute configurationsettings. The attribute is a namevaluecollection that contains the name/value pair of the configuration settings:

Public static namevaluecollection. deleetask{ get ;}

The following web service method is used to obtain application configuration settings: using system. configuration;

[Webmethod]
Public String getreceivetting (string key)
{
Return configurationsettings. deleetask[ key];
}

Note that the appsetting attribute is read-only. To modify the application settings, you must edit the Web. config file. At the same time, if the key does not exist in the web. config file, an error will be thrown when the appsettings attribute is read.

The configurationsettings class also provides a public method for configurationsettings. getconfig to return the configuration settings of the User-Defined configuration section.

Public static object getconfig (string sectionname );

The sectionname parameter indicates the configuration section to be read. The following web service method is used to obtain the specified key value of the User-Defined Configuration:

[Webmethod]
Public String getconfig (string key)
{
Namevaluecollection NV = new namevaluecollection ();
// Instantiate the namevaluecollection Class Object
NV = (namevaluecollection) configurationsettings. getconfig ("deleetask ");
// Return the settings of the User-Defined configuration section
Return NV [Key]. tostring ();
// Return the specified key value
}

6. consume "Web Services for implementing Asp.net application settings"

You can write web applications, Windows desktop applications, web services, and other applications to consume the created applications and set web services. The implementation method is the same as that of any standard web service. It is very simple and will not be explained here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.