Author: This article taken from Zhang Jinsong: SEOUL December 3, 2002
For programmers who have been developed on Windows, COM should not feel unfamiliar. Com represents the highest level of Microsoft on the COM technology platform, and also the fulcrum of Microsoft and J2EE counter (core thinking in J2EE is actually borrowed from COM predecessor MTS). In fact, COM is indeed very excellent product, for medium difficulties, using VB6 plus COM is indeed a very fast and efficient technique. What is more worth mentioning is Com is completely free. As long as you have Windiws 2000 or XP.
After .Net is launched, people seem to forget Com . But if you pay attention to the development of Microsoft technology, you can't find it in the next year to two years, COM will not withdraw from the Windows Stage. Many of its service functions or .NET cannot be replaced in a short period of time. For example, COM provides instance management, transactions, disconnected asynchronous queued components, role-based security mechanisms, and more. Therefore, in the future, it will be a new and old technique, and COM gradually transitions to .NET. In this large technical background, it is also very meaningful to learn how to use .NET to write components running in COM .
This article will combine a small routine to describe some of the basic processes, technical points, and some basic principles that should follow in actual development work.
Introduction to Servic COMPONENT
Servic Component is a class that is developed by any .NET CLS compatible program language. It must be derived directly or indirectly in System.EnterpriseService.ServicedComponent. The class developed can run in the COM environment, call the COM service. In the .NET development environment, COM services available
· Automatic Transaction Processing) (Automatic Trading Processing)
· COM Transaction Integrator (COMTI) (COM transaction integration)
Compensating Resource Managers (CRMS) (Compensatory Resource Manager)
· Just-in-Time Activation (Instant Activation)
· Loosely Coupled Events (loose connection incident)
· Object Construction (Object)
· Object Pooling (Object Loop)
· Queued Components (Queue Components)
Role-Based Security (role-based security)
· Shared Properties (Shared Properties)
· Synchronization (Activity) (synchronous)
· Xa Interoperability (XA interoperability)
Serviced Component's development process
Create a class libray, such as routine, the class written by the user must be derived directly or indirectly in the ServicedComponent base class.
// routine file name is Bank.cs
Using system.enterprises;
Using system.Runtime.compilerServices;
Using system.reflection;
Namespace BankComponent
{
// Inherited ServicedComponent
Public Class Account: ServicedComponent
{
Public Bool Transfer (int from_account, int to_account, double amount)
{
/ / Bank transfer implementation details, here from omnis
Return True;
}
}
}
Generate key files (Key)
Servic Component running in the COM environment must be signed (with strong name), so the components you develop should be signed with the "Key" file. Key files can be generated with tool sn.exe provided by .NET. The easiest way is to open a DOS window from the CommNad Prompt provided by Visual Studio, execute the following command:
You can add COM -related properties in the source program, which will be reflected in the set of COM . But this is not necessary. You can set the COM -related properties in the program, but the component is published, set the properties of the component in the Component Services Browser in COM . Before you are hand-made, COM will give you the component to default.
COM can set the properties, which is only used here.
Publish assembly
Serviced Component is a relatively complicated step. This article does not want to discuss the principles and details of COM registration, only to introduce the most basic issues. It should be noted that the publishing component involves changes to the CATELOG, so the operator must have administrator's permissions.
a. Dynamic release
Dynamic publishing does not need to manually install the component in a COM environment, the program automatically installs components to COM when running (only the client program developed by .NET can be installed to COM , traditional COM The client does not have this feature). When the control client program is called for the first time, the .NET runs automatically detects that Servic Component is not registered to the COM environment, so the .NET runtime will be based on the properties of the component. It registers to the COM and respond to the call of the client. When the client program is called again, the components that have been installed are directly responded directly.
When controlling the customer's first call service service, .NET is registered and attribute after the backfinder, and you will feel a very obvious latency. On the author's computer (1.13GHz Sony Notebook), this process takes approximately 12 seconds. It happened only once this registration and installation process.
The benefits of dynamic release are very simple, users only need to copy compiled components (DLL assembly) to the directory of the client application. .NET will take over the rest of the matter.
b. Manual release
Handmade release is more troublesome than dynamic release, but you have more control. In addition, dynamic issuance requires a compiled component copy in the client program directory. If there are multiple client programs on your computer, it means that there are multiple copies of the same components on the computer. In addition to redundancy, there will be some potential problems in future components. In this case, you may be more willing to put the components in the public compilation buffer pool (GAC) ". Handmade Publishing Components You need a public compilation buffer tool for .NET, Gacuti.exe, and service installation tools, REGSVCS.EXE. As far as the routines given in the text, you need to type the following command in the DOS window. Development of client program
Services Component developed with .NET can be called by the .NET's client and the traditional COM client. The two small routines are given below to demonstrate.
a .NET client
There is no explanation with the .NET development client program. Just need to reference the development of a good component. If the component is installed in the Public Compilation Buffer Pool (GAC) ", the client program does not require a dedicated copy.
Using BankComponent;
Using system;
Namespace BankComponentconsoleClient
{
Class Client
{
Public static int main ()
{
BOOL BO = FALSE;
int start = environment.tickcount;
Account act = new account ();
BO = Act.Transfer (123, 234, 50.0);
Int end = environment.tickcount;
Console.writeline (Peformance: " (end-start);
Return 0;
}
}
}
b. COM client developed with VB6
Add a new project in the Integrated Development Environment of the VB, add Bank.TLB in the Project Reference (when you are running regsvcs.exe, the class library file (Type library) will be generated, then you can join program
Private submmand1_click ()
DIM OBJ AS NEW Account
DIM BOOL As Boolean
BOOL = Obj.Transfer (123, 234, 50.0)
Msgbox "The Result IS" & Bool
End Sub
Precautions for using Servic Component
1. Consider the client program
When developing Servic Component, pay attention to what kind of client will use it. Because the COM client has different requirements for Servic Components with a customer program developed with .NET. If there is a COM-based client, you must pay attention to the following points in the development component:
Do not use a creation function with parameters (Parameterized Construction "
· Do not use static function
· Define event source interface (event-source interfaces)
· HRESULTS is included in user-defined exception (User-Defined Excetion)
2. Careful use of Servic Component
The components running .NET development in the COM environment To pass the COM ->. NET middle layer, and COM to COM will also introduce a layer of Context Layer. This has a big impact on the performance of the program. If you have a small procedure that is running this article, there will be some sensual understanding. In addition, in theory, using servicesD Component will affect the portability of the program. Because COM is necessary to be Microsoft proprietary technology, other platforms such as Linux even if it is transplanted .NET, this part of the function is not transplanted. So only services that really need COM are provided, such as transaction processing of across data sources, using the service of Servic Component when role-based security management. Serviced Component Other Uses
This article is only a brief introduction to how to develop Servic Component. Comprehension of Servic Components also requires further work. The author will further introduce a complicated function and usage such as Queued Component, Loosely Courts, etc. in subsequent articles.