ASP Component Guide
Author:. J.D MeierMicrosoft Corporation
January 24, 2000
If you meet the following cases, this article is suitable for you:
From the Active Server Pages (ASP) Credit Component Design, the components that will be called from the ASP code wish to use the components in the ASP code.
Directory Introduction Why use components? Status Management Range Split Service Thread Model Security Server.createObject and CreateObject Transfer Parameter Event OnStartPage / OneendPage and ObjectContext Error Handling Global Variable Distribution Component Conclusion
Introduction
Component. Some people like them, some people are afraid. People who are afraid of components usually can tell you a horrific experience. Let us face it: When starting using components in ASP, I don't know what to hurt you. If you fall, then stand up, take it clean, then come back. In this article, I will provide a general guide to practicing to help you build a better component-based ASP solution.
Why use components?
Before I start discussing the Component Guide, it is worth considering the value of the component to the ASP application. Many developers who are unfamiliar with components always feel that everything is so fresh. Components can bring the following all kinds of benefits to the ASP application:
Package function and hidden implementation detail Reusability (including reused by different client applications) Intellectual property protection scalability (embodied in the allowable application to multiple computers) configuration and deployment flexibility (especially early Binding is an important factor) Accessing system, such as Win32 API calls, any other underlying feature in the programming language Type function ("Visual Basic® Script Editor [VBScript]" is weak, and JScript® is not too Good) Business logic is separated from user interface, or the web designer is separated from Web developers.
Interests and payings. Creating a component solution can be more expensive to increase the complexity of the development process. Deployment and troubleshooting may also become more difficult and become a reality factor. However, don't let the difficulties in front of you have hindered your long-term interests. How do I know if the cost is worth it? Consider the following aspects:
What is the existing code base? What is the level and experience of developing a team? What is your control for the primary server? What kind of tools and languages have been selected for a specific task? What is the synergistic problem? Is there any factor in performance and scalability? What is the project time frame? Who will continue to maintain and support the app? For example, can the development team involve and take over?
After reviewing the above-mentioned questions, please examine your assumption. The prototype can quickly obtain the actual situation from the idea.
Now, there is a certain understanding of the benefits that the components may bring, let us continue to discuss. The following guidelines will help you get the biggest benefit. These guidelines may become a guide that guides you smoothly establishes more stable, upgradable, and better performance ASP component applications.
Status management
Suggest
In general, there may be useless components and stateless ASP pages in a possible occasion. Components should not be required to be invoked from one way to the next state. Store complex states in the database. For simple data, along with cookies, queryString or hidden form fields that pass data between the pages.
why
Server resources are limited. The status in maintenance components means that the application will consume valuable resources when resource conflicts and concurrency issues. The stateless component will help you avoid these issues. The stateless component also provides more deployment options and enhances the ability to share resources on multiple clients.
Common traps
The mistakes of developers are wrong to design or use components that require maintenance. Please pay attention to prevent this idea that is commonly used for desktop development. Typically, developers with desktop development background will design components that depend on state.
details
Avoid using "ASP session" will increase the performance of the server because it simplifies the code path and reduces the consumption of server resources. If you don't use the ASP session, use the Internet Service Manager (see "Internet Information Service [IIS] document) to disable the" Session "status. You can also use the following tags to disable page-based "sessions" in an ASP page that requires a "session":
<% @ EnablesessionState = false%>
Deployment flexibility is another important aspect, especially when running applications in the web area. If you rely on ASP Sessions, a given user's request is bound to the specified web server because the "session" status is dedicated to the server. Avoid status in the intermediate layer and web server, and use the database, the ASP request can be processed by any valid web server in the area. Therefore, you will reduce competition, provide better redundancy, and allow more distribution options.
For additional methods of passing data between the "ASP sessions", see the "Knowledge Base (KB)" article:
Q175167 HOWTO: PERSISTING VALUES WITHOUT SESSION: Maintain State Across Pages with VBScript (English)
DON BOX also proposes more insights on MTS status management in ActiveX® Q & A (English).
range
Suggest
Typically, use components within the page range. The meaning of the page range is to create an object on the same page and use it and release it - all of this is on the same page.
Components labeled "Double" or "Unit" can work normally in the page range. Use "unit" model components only within the page range, such as VisualBasic components. If you need to store components in the Application or Session, it is recommended to use "Double". You can store components marked as "Double" within the "Session" or "Application" range, but components need to ensure thread security.
why
Use components within the page to make server resources to be recycled. The release resource will minimize concurrency issues and allow for collection of resources to be shared on the client. In addition, the page range component avoids thread problems that affect the "session" or "application" range. The thread problem will be discussed in detail in the "Thread Model Classification" below.
Common traps
One of the most common issues is to store Visual Basic or other "unit" model objects within the "Application" range. If you try to store a "unit" model object created with Server.createObject within the "App), you can see the following error:
Application Object Error 'ASP 0197: 80004005'
Not allowed to use
/Virdir/global.asa, Line 7
You cannot add an object with a unit model behavior to the internal object of the application.
However, if you use
Another common problem is to store the "Session" model object, which binds the user session to the specified thread. This behavior severely affects the performance of the server. Since all calls will continuously collect threads that create the object, it fundamentally affects the purpose of the thread buffer pool.
details
For details, see the KB article below:
Q243543 INFO: Do Not Store Sta Objects in Session or Application (English) q243548 Info: Design Guidelines for VB Components Under ASP Split Service
Suggest
Separate expression, business and data service. Business components should implement business rules. Business components should not contain data access technology. That is the task of the data layer component. Business components should not contain references to ASP objects.
ASP provides expression services. The object references to ASP should be presented as HTML. These objects can call the business objects registered with MTS / COM .
why
Split the application into separate and distinct service, have the following benefits:
More convenient for components Support Windows DNA Model (English) Better Isolated Troubleshooting Difficult Problem Difficult Deployment Options (Remove Service Coupling Allows Distribution Applications on Multiple Computers)
Common traps
There is a common problem we are called "Swiss Army Stroke" component. The "Swiss" component integrates all services (like a small Swiss army knife with 17 tools such as screws, toothpicks). Combine unrelated services into one component so that this component is difficult to use, understand and maintain.
Another trap that is easy to fall is to reference the ASP from the business component. Allows ASP and business logic (by using requests or response objects, or constructing HTML inside), not only limits your components, but also limits horizontal scalability. The object to reference the ASP built-in object should be in the same frame with the web server. Ideally, the business components can be distributed in different frames due to lateral scalability. The expression service can be provided directly in the ASP script, or the HTML of the components that reference the ASP built-in object can also be established, and these components are held in the IIS frame.
details
Successful design models can be used as a model for processing public business issues. For example, a model for processing Creating a Read Update Delete (CRUD) action helps you divide your application into several distant logical services, namely, business rules, and data access.
See below for more design model specific examples, you can imitate it in your own application:
Scalable Design Patterns (English) Simplify MTS Apps with A Design Pattern (English) FMSTOCKS Application: Start Here (English)
Thread model
Suggest
Select the range of components or select the thread model of the component, which method is preferred? Both methods should consider thread branches unless the "unit" or "double" model component is determined within the page range. (If the Visual Basic programmer does not know which thread model is the component, it is always "unit".)
If you need to store objects in Applications or Senders, you need to use the Components of Double "and the" Free Unit Grouping Program (FTM) ".
Do not use the Single Threading component and avoid using the "Free Thread" component from the ASP.
Note: If you are not careful, Visual Basic produces a "single-thread" component. Be sure to set the thread model as a cell thread on the General tab of the project property page. Also note that unattended execution and reserves the option in memory on the same tab.
why
If you are using Visual Basic, it is a "fool" development environment. Visual Basic is limited to using the "unit" model. If the Visual Basic "unit" model object is implemented very good, I don't want to consider too much on the restriction on the page range. Fitch and Mathers Stock 2000 destroy any pre-idea for performance. In addition, many existing websites built by ASP, SQL, and Visual Basic, is not anevant in proven "unit" model components that are scalable and executed. If FTM is aggregated on a component marked "Double", you can call between threads without any grouping or thread switching. If the component labeled "Double" does not have an aggregate FTM, ASP will treat it as a "unit" thread object - just like Visual Basic components. Keep in mind that if you plan to use the COM Object Pool, do not aggregate FTM. For rules about the "Object Pool", see the Platform SDK document.
"Single Thread" and "Free Thread" components run in the System security environment. Worse, the "single thread" component will cause a deadlock.
Common traps
Perhaps the most common trap is to use components that are not designed to run off in ASP, such as a "single thread" component. Most developers have fallen into it because the desktop application is moved towards the ASP, or when using third-party controls. If you don't determine the thread model of the component, you can check the registry key of the component (but you can't depend on it).
details
For the thread model and its impact on the ASP, see the following article:
Don Box's Active Server Pages and Com Apartments (English) Agility In Server Components
In addition, the following KB article provides detailed details on thread problems:
Q243543 Single-Threaded Apartment Objects in Session or Application (English) Q243544 INFO: Component Threading Model Summary Under Active Server Page (English) Q150777 INFO: Descriptions and Workings of OLE Threading Models (English)
safety
Suggest
Components do not have any assumptions to the user environment it run. Do not access user-specific information, such as HKEY_CURRENT_USER, or dedicated resources for desktop computers, as these are unavailable for components. The application does not use SendKeys or calls components that depend on the user interface, and perform an operation that usually requires a desktop interaction, such as opening the dialog.
why
Components will run on a different security desktop. First, this means that the application cannot open the dialog box and cannot interact with other GUI utilities (for example, using SendKeys). By default, inetinfo.exe is not allowed to interact with desktop. Different user environments will restrict components to access certain resources - mainly the HKEY_CURRENT_USER section of the registry.
Common traps
A common mistake is an entry that references HKEY_CURRENT_USER. For example, Visual Basic's GetSetting and SaveSetting functions cannot be used under ASP because they reference the entries under the HKEY_CURRENT_USER configuration unit. The following KB will discuss this question:
Q248348 PRB: SAVESETTING AND GETSETTING NOT AVAILABLE IN VISUAL BASIC 6.0 WebClass (IIS Application) (English) When the printer, MAPI information, and network sharing usually "fail" when calling components from the ASP instead of the desktop client.
For details, see the KB article below:
Q184291 PRB: COM OBJECTS FAIL TO Print When Called from Asp (English) Q217144 Info: Difficulties Using Net Apis in Isapi and Asp Com Objects (English) Q207671 HOWTO: Accessing Network Files from Iis Applications (English)
details
Some points of safety on security:
Which IIS authentication method is enabled? Your web application is within the process or outside the process? If the component is registered in MTS or COM , is it in the "server" or in the library package? Are you calling local DLL, remote DLL, local EXE, remote EXE?
A detailed description of security is exceeded in the scope of this article. However, due to the complexity of this topic, the following article has a great help from the perspective of ASP components:
Securing a Web-based Microsoft Transaction Server Application (English) Q172925 INFO: Security Issues with Objects in ASP and ISAPI Extensions (English) Q217202 PRB: CGI Applications and IIS OOP Applications May Fail (English)
The following outlines how IIS handles security:
Authentication and Security for Internet Developers (English)
Server.createObject & CreateObject
Suggest
Use Server.createObject. If you are using the MTS / COM library package, use Server.createObject to avoid thread blocking.
why
CreateObject is equivalent to calling CocreateInstance via a script engine. If you use createObject instead of Server.createObject, the following situation will occur:
ASP cannot identify this object. The onStartPage / OneendPage page method is not called. ASP does not know the thread model of the object.
Server.createObject is equivalent to getObjectContext.createInstance. This represents the ASP clear the object and knows its thread model. Alternatively, if the ASP page is transactional, the components are called with the ASP page in the same transaction by calling Server.createObject. (Note that transactional pages may mean the avengeous business rules and expression levels.)
Common traps
If the object is behind the firewall, you may need to call CreateObject. See Q193230 PRB: Server.createObject Fails When Object Is Behind Firewall (English) to get detailed information.
details
Although CreateObject below IIS 4.0 is fast than Server.createObject, the performance is the same in IIS 5.0. Similarly, if you are using the MTS / COM library package / application, Server.createObject prevents thread blocking. Parameters
Suggest
Declare the OUT parameter is Variant. In Visual Basic terms, this means that the reference parameters should be Variant. Parameters (IN parameters) transmitted by value are not limited to Variant, but must be compatible with Variant.
why
Script client uses Variant. The COM server can use the specified data type. When you pass the specified data type by value to the COM server, the COM server can receive without problems. However, in addition to Variant, other reference parameters cannot be "forward" to the ASP script.
Common traps
One of the most common errors is "types of mismatches". This is usually because the variable passed to the COM object as a reference is not Variant. The usual solution is to transmit parameters by value or turn the parameters to Variant.
details
If you want to distribute components in multiple computers or run them outside the process, you may see the significant performance obtained by value delivery parameters. Press the reference delivery will cause more grouping overhead in the process or computer, as the data must be recovered. If you do not actually need to pass parameters as reference, the correctness and validity of the value transfer parameters are also a problem. Note that the Visual Basic is passed in accordance with the reference parameters by default.
The following KB article discussion will pass the parameters from the ASP to the COM object:
Q197956 PRB: Passing parameters by reference to a vb com object (English) Q197957 PRB: passing parameters by reference to a vc com object (English)
event
Suggest
Avoid calling components waiting for other components to return events.
The component method should return to the ASP as soon as possible. Consider using "MSMQ" or "COM Queuing Components" to provide asynchronous calls - or when you want to do for a long time and don't have to run online.
Please assign work items asynchronously without letting ASP wait for a long run. Then you will return a response from the ASP to the client. Once the work item is completed, you can notify the client with an email or other method (see below).
why
The ASP is not designed for processing events. To optimize server performance, return to the HTTP request as soon as possible.
Common traps
The status identity on the loop check server is not a "server friendly" method for providing a browser notification.
details
Usually developers are concerned with the cause of the browser to provide a notification on the work processed on the server. Although the exquisite browser notification system can be developed, if you open another port on the server through a socket, many developers implement their needs by the following techniques:
Use email notifications to add meta-refresh tags to the page to poll the server. Send to the browser connection and let the client manually check the state of the unrequited request.
The following KB article discusses these issues:
Q243547 PRB: ASP DOES NOT Provide Progress Notifications To Client Browsers (English) Q243546 PRB: ASP DOES NOT Support Events (English)
OnStartPage / OneundPage with ObjectContext
Suggest
Use ObjectContext to access the ASP built-in object (such as response, request, server, etc.) in IIS 4.0 and later. Whenever you try to avoid using ScriptingContext objects, onStartPage and OneundPage. why
OnStartPage, OneendPage and ScriptingContext objects are used for legacy support.
Common traps
If you insert an ASP object, the ATL wizard will use onstartpage and oneundpage.
details
Use the "Double" or "Unit" model component to get ObjectContext without registering with MTS / COM . For local ActiveX EXEs cannot use ObjectContext, you need to use OnStartPage / OneNDPage. To use the "Free Thread" and "Single Thread" components, you need to register these components in MTS / COM . Otherwise you need to use OnStartPage.
Error handling
Suggest
The error processor will look forward to the accident. Capture the error in each part of the application and record as complete as possible. Good logs have great significance for tracking, isolation, and troubleshooting. These logs can be implemented as text files or written to NT event logs. In most cases, it is an effective way to notify the caller that the caller is incorrect while adding information. Extended errors allows the caller to interact with specific methods of processing specific problems.
When recording an error, it is critical to providing as many useful information as possible. Consider including the following points:
Current User Environment (Call WIN32 API - GetUsername) Current Thread ID (Call Win32 API - App.ThreadID in Visual Basic) Current time (obtained using Win32 gettickcount, millisecond data) passed to the parameter error source of the method, including Method name
why
According to our experience, good error handling and recording is the most effective way to isolate and diagnose runtime issues.
Common traps
Remember the ASP 0115 error? I hope you don't have to struggle with it. If you still worry, I suggest you see Troubleshooting With The IIS Exception Monitor.
The ASP 0115 error does not always appear under the control of the developer - but most of the time, the error handling may have avoided many of this situation, which may also help solve them when it occurs.
In short, the biggest problem is to skip the error handling or does not contain useful diagnostic information.
In COM, there is a case where the boundary spread of the components is abnormal. Capture an exception - but return to HRESULTS to transfer failed information to the caller.
details
The following article provides an example of an application for effective error processing:
Fitch & Mather stocks: Web Application Design (English)
Global variable
Suggest
Avoid using global variables in the components. In Visual Basic terms, this is indicated that there is no public or global variable in the standard .bo module.
why
Global variables are not true in real sense. Every thread has its own copy. If several methods are implemented in the same thread, they will see the same variables; otherwise they are accessing the different copies of these variables. This means that you may assign a value for a global variable (in thread A), but another user (executed in thread B) does not see the new value.
The reason is that the "Thread Local Storage (TLS)" is used in Visual Basic to reference global variables. This means that each thread has a copy of its own public variable, and because it exists multiple copies, global data is not really "global." That is, users who are running in the same thread will access the same variable, whether they expect this. Common traps
This data may have been destroyed if the PUBLIC variable is used in the standard .bo module.
details
Visual Basic Programmer's Journal in June 1999 is a must-read:
Black Belt Programming - Create Worker Threads in Dlls Component Builder - Create Efficient Multithreaded Apps
In addition, the article in Daniel Appleman is well outlined well in Visual Basic's working principle: a thread to Visual Basic (English)
Distribution component
Suggest
The distribution of components involves performance, scalability, and security issues. Different distributions of the same components may result in higher performance, more telescopic, and more manageable configurations.
The following guidelines help to increase performance and scalability when distributed in multiple computers:
Run the components of the reference ASP built-in object in the same frame of IIS. Run the database component on the application server. It is important to run business components on a computer. If you remove the coupling of business components and any ASP, you can design, computer availability and testing, from your application.
Of course there is also an exception. But these are a good start in the guide.
why
Cross computer distribution components enable applications to meet scalability requirements. Second, the guide mentioned above helps implement the performance and scalability target of the application.
Objects reference ASP built-in objects will communicate with your web server, and because they are part of expression, they are there.
The database or the most sensitive logic that is extremely sensitive to data may be in the stored procedure of the database. Place the data access component in the application server instead of the database to avoid expensive calls between components. Instead, data access components communicate with the database more efficiently using SQL Server communication (such as TCP / IP).
Common traps
You should try to avoid the following questions:
When the horizontal scalability is more suitable, continue to pursue longitudinal scalability from your computer. Ignore the firewall's consideration (help yourself. If the product environment between the computer has a firewall, add a firewall in the test scheme.) Place the components of the reference ASP built-in object (callback and group ASP) The cost of built-in objects is high.) Use post-binding inside the component (this produces additional calls to getidsOfNames, which may be expensive in distributed applications.) Use early binding.) Press the parameters by reference (this Multi-group overhead. Transfer parameters as possible to "value".)
Successfully calling remote MTS components from IIS can also be tricky. A simple and effective, improving solutions for performance and simplifying security issues, is the MTS / COM package / application in the middle. Early binding reduces network channel segments to improve performance. If you use the Server package / application, you can set the running ID of the package / application. This technology will be discussed in KB Articles 159311 Instantiating Remote Components in Microsoft Transaction Server and Internet Information Server. details
If the service has been decoupled, especially the ASP has made the ASP outside the business component, the distribution will be quite flexible. You can consider the frame more and disperse components as needed to solve the resilient and performance issues. How do you know? carry out testing. How to test? Please see the basic guidelines below:
To test the reliability of the Web site, analyze the computer and check the error. To test performance, check how much ASP requests per second. To test scalability, set the threshold that needs to process how much ASP requests per second. Test the application with important tools - add users until performance is bad to accept. Strengthening the testing of applications is very important because other issues that need to be exposed to operation conditions and no single browser testing.
For details on the application to enhance tests, see i can't Stress It Enough - Load Test Your ASP Application.
in conclusion
As you see, there are some things that need to be noted throughout the development. Here, many factors involved in the application guide have all been clarified because they help thoroughly avoid serious mistakes. In the entire development cycle, several guidelines will be described herein, not only some of the additional work, but also possible to submit contraction, reliable, high-performance ASP-based solutions.
J. D. Meier is born and grows in the East Coast of the United States. Listening to Horace Greeley, he became a "developer support" engineer, mainly to include server-side components, including MTS and ASP technology, and Windows DNA applications.
Archive Servin 'IT Up Column
December 27 ASP Guide