Developing COM + components under .NET.

xiaoxiao2021-03-06  41

First, the question is proposed: (I will mention the problem I have encountered)

1. Each request for the end user will read mapping files - can be solved by cache;

2, XXX subsystem needs to write mapping files - or first write memory then write to file resolution;

Second, the analysis solves the problem:

Since 2 or more subsystems need to be read and write to the same file, the client has a lot of concurrent, so it is poor to read and write conflicts or errors, and will also affect the performance of the system.

Read and write files by calling the common processes; and this common process external component only uses the content LOAD to memory when you start itself, update memory when the file changes when the file changes.

Third, COM components introduction:

As early as the WINDOW2000 released, COM has been generated, and integrated in the operating system, exists in the form of service when running. Com adds a series of MTS (a system service to manage persistence data in the database, also handling persistent messages and file systems in transactions) services:

1. Transaction services: It ensures data integrity in the distributed system.

2. Secure service: Its security model provides processing security without writing any code.

3, Synchronous Service: Provide a method of managing the problem in the component, but does not have to write code.

4, resource management: (including resource manager and resource allocator) Manage database connections, network connections, and memory, etc.

5. Just In Time is a service that inherits from MTS, which will keep one instance of the object in an active state and slowly use the object, will notify The MTS environment can release the resources occupied by the object, such as database connections.

6, the object pool: Provide a thread pool to store the current object for the next time, it is an automatic service of the system. When there is an application to access the COM component, an instance of the component is created for the application and stored in the object pool. After the application is turned off, the instance is destroyed, which can freely configure the component to make it the state in the pool, implement it. Executive reuse, improve application performance and scalability.

7. Based on role: This is easy to understand, that is, verify the role, give a secure license, give different users different permissions.

8, Queue Components (MSMQ): Provide asynchronous message queues, automatically convert data into packets in its form.

9. Sharing Properties Manager (SPM): Used to manage object status information stored in memory, can be used to share multiple objects in one server process, and provide concurrency processing.

10. Compensation Resource Manager (CRM): Used to generate a log file during processing a transaction and can handle system crash. Including the CRM grid (main part of CRM, the interface provided by CRM's infrastructure is written to the log, and retrieve log files) and CRM compensator when needed (used to complete the transaction) And the generated log file is submitted to a user who execute transaction).

11, concurrency: Although multiple processes in an application are allowed simultaneously, the process is divided into each set of context (object set with the same needs) included in the unit (a set of contexts within a process), one process can contain more The context involves the object of different needs and each context has a unique COM object to provide services such as COM transaction and JIT activation. Tools provided using COM can create multi-layer applications that execute transactions, and can also process thread allocation issues. OK, introduce the "COM " to introduce the type of COM . COM has several types of different purposes: 1. Application proxy: file containing application registration information, that is, when there is a client program to access the remote computer, the application agent running on the client will use the information of the server application. Write into the client computer. 2, server application: The COM application is executed within the process of the COM application itself, while using concurrency to handle components.

3, library applications: Yes in the process of the client application, loaded within the client application process, using task-based security, the disadvantage is not to support remote access and queue components.

4, COM Pre-installation Application: See what is COM pre-installation application in the COM application folder in the component service in the management tool, which is an application that automatically installs the component service during the COM installation process. Can not be modified or deleted.

At the same time, it involves a concept of "assembly": "it is a collection of all types of CLR and other resources (such as bitmap files, etc.). You can view it by configuring the .NET Framework "under management tools. Includes an assembly with private and shared. Private assembly: It can only be placed in an application access to the same path as the assembly. The default is private, must be placed in a folder using its application. Sharing assembly: Refers to the assembly added in GABAL Assembly Cache. GAC is specifically used to store assemblies, enabling applications to share these assemblies, there is a little attention to the assembly stored in the GAC must have a unique name, which can be entered in the .net command execution window. Sn -k asseemblyname.snk generates a unique name called a strong name, after generating a strong name, there will be a public key to attach to this assembly, which is used to prevent the name Similar and replaced.

The security of the assembly in .NET provides two mechanisms: one is the strong name mentioned earlier, one is to use SignCode (which can be understood as a digital signature). Signcode.exe is used to mark an assembly, while embedding a digital signage to the program set, allowing the user to identify developers who created this assembly. (Note: There is no strong name in the GAC, otherwise it will not be a strong name, but you can store multiple copies of an assembly and the assembly in the GAC can only be deleted by users with certain permissions. )

Regarding the version number of the assembly: Each version has a 128-bit version number, indicated as four parts: Major (for the primary version number, the project change is changed) .MINOR (add a function to the project). Build.Revision (the next two is automatically updated), these version information can be viewed by the AssemblyInfo.cs file in the application.

It is also a bunch of theory, the following introduces the application of COM : The components that use the COM service in .NET are called .NET service components, unlike general components, the difference is that the former uses COM services. The purpose of using the .NET service component is to access some of the base classes of the COM service such as ServiceDComponent and automatic transaction, JIT, object pools, and security. You must register the service component before using the .NET service component, there are three ways: 1, manual registration: registration through the regsvcs.exe command line tool, such as: regsvcs you.component.dll will result in a YourComponent.tlb type library, contain Type information of the object (Note You must register a strong name "before using this command)

2, programming registration: Using the RegistrationHelper class, mainly use the method in the IregistrationHelper interface.

3, dynamic registration: Found that check the version of the installation component during the execution of the application, if the correct version of the component is not installed, automatically install the required version at runtime, that is, automatically registration. (Note: Calling the automatic registration must be a member of the Window2000 Administrative group, because dynamic registration cannot change the COM directory, otherwise the registration process failed) four, instance analysis

1. First create a category library. (Called xmloperate in my project) it will generate two files, assemblyinfo.cs and class1.cs (I changed Class1.cs to main.cs, the specific name can be fixed)

2, add reference (System.EnterpriseServices)

3, return to main.cs: using system; using system.Runtime.interopservices; using system.enterprises;

A summary description of Namespace Xmloperator {///

/// ompMsaxmloperator. /// [objectpooling (minPoolsize = 0, maxpoolsize = 1)] [JustImeActivation (TRUE)] [ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterface (ClassInterFactype.Autodu)]

Public class xmloperties: servicedcomponent {protected {base.construct (null);}

protected override void Activate () {} protected override void Deactivate () {} protected override bool CanBePooled () {return true;} protected override void Dispose (bool disposing) {base.Dispose (disposing);}

// /// I have removed my many methods, write a simple Visit to demonstrate it ok. /// [AutoComplete] Public String Visit (String Name) {Return "Welcome" Name;}}} 4, return to assemblyInfo.cs, add reference using system.enterprisservices;

Add code:

// Set the component to the server startup mode. [assmbly: ApplicationActivation (ActivationOption.server)]

// Develop application server name [Assembly: ApplicationName ("XMloperate")]]]]

5, next, give a strong name (equivalent to registration) to the server program. Open Visual Studio .NET2003 (Maybe you use 2005) command prompt, find the address of the file, enter SN -K, generate a key, such as my file, Work, D: / Work / XMlopeRate> Sn -k Xmloperate.snkxmloperate.snk is the generated file name (after the execution is complete, one file server.snk)

6. After the strong name is generated, return to the program, in the last few rows in AssemblyInfo.cs, you can find [assembly: assemblyKeyFile (")], write the file path into [Assembly: askEMBLYKEYFILE (" ./ .. / Xmloperate.snk ")]

7, generate a solution, there will be XMLOPERATE.DLL in Obj / Debug, return to the Visual Studio .NET2003 command prompt, go to the file obj / debug directory, execute the regsvcs command, register service. (You can not register manually, in step 9, when the client calls new, if you do not register the component service, the system will automatically register, please see (3) Part of the last registration method.) My project This is this D: / Work / XMloperate / Obj / Debug> Regsvcs XMloperate.dll; which is built by the server. Open my computer, control panel, management tool, component service, COM application, now we can see more than COM , XMLOPERATE This is the server that is just built. 8, press the right click on XMloperties, open the property, modify the security: call the authentication level as: no, analog level is identified. The lichesed check (this is based on the situation, now just do the list on this machine). Then find the activation, the remote server name is changed to your own machine's IP address. 9. Establish a client. Built a WinFrom (in my project, there are 1 Web Service and a Window Service to call XMLOPERATE) Add: Using System.EnterpriseServices; using Xmlope; Direct in the client NEW can directly use the method . Since the understanding of Com is not very thorough, if there is a wrong place or not perfect enough place, please let me know! Welcome to communicate with me, msn: pinping1314@hotmail.com

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

New Post(0)