C # inherit from com

Source: Internet
Author: User

After reading about the book and taking a look at it, I feel that the main function is to implement a transaction function. Others do not see any new things. In addition, this function can also implement the remoting function, which can communicate with other computers, but the book does not cover much, so I am too lazy to look for information, that is to say, I understand a basic implementation and use. You can set the com + component in the program or in the com + component service browser. The following is the code with and without Components

Components:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. EnterpriseServices;
Using System. IO;
Using MySql. Da

Ta. MySqlClient;
Using MySql. Da Ta;


// The configuration here can be configured in the component service of the management tool. The service component is deployed using regsvcs dllname. dll.
// To call a service with components, the caller must reference System. isiseservices and the dll of this component.

[Assembly: ApplicationName ("EnterpriseServiceTest")] // name of the application displayed in the componentservice Browser
[Assembly: Description ("An EnterpriseService Test")] // the Description text about this program is displayed in the com + application attributes of the componentservice browser.

// Specify whether the program runs in the client (caller) process (ActivationOption. Library) or in a separate process (ActivationOption. Server)
// If ActivationOption. Library is used, you cannot use the application proxy option when creating the installation package using the componetservice browser.
// This option can also be set and modified in the component service after registration. If ActivationOption. Server is used, you can generate the agent MSI Installation File during export in the component service, and then
// Execute the Agent installation and reference the installed DLL file in the client application on the client to achieve remote communication similar to the remoting effect. You can also generate only the msi installation file of the class library, directly on other computers
// Install and reference the installed DLL in the client application to directly use the component functions on the local machine
[Assembly: ApplicationActivation (ActivationOption. Library)]
[Assembly: ApplicationAccessControl (false)] // defines the security configuration of the program. The bool value indicates whether to enable access control.
Namespace incluiseservicetest
{
Public interface IEnterpriseServiceTest
{
String ProceedStr (string str );

}

[EventTrackingEnabled (true)] // indicates whether the image can be monitored using the componentservice browser. The default value is false, because monitoring is allowed to degrade program performance.
[Description ("A TestComponet")] // the Description text displayed in the component attributes in the browser

// If JIT activation is specified, the activation function cannot be disabled in the COM + directory, and vice versa. If the activation function is enabled in the COM + directory, you must specify the function in the component.
// For components configured in COM +, JIT activation is disabled by default, but JIT activation is automatically enabled If Automatic transactions are requested.
// The class is not activated during instantiation, but activated when the first method is called.
[JustInTimeActivation (false)]

[ObjectPooling ()] // if the component initialization takes a long time, you can use this attribute to configure the image pool and set the minimum and maximum number of images for the image pool.
// Required use the current transaction if it is already running in a transaction. If there is no transaction, create a new
// RequiresNew whether or not it is already running in a transaction, it creates a new transaction and runs in it
// Supported uses the current transaction if it runs in a transaction. If there is no transaction, no new transaction will be created.
// NotSupported does not run in this transaction even if a transaction exists.
// Disabled ignores any transaction processing in the current environment
[Transaction (TransactionOption. Required)]
// The value of the second ConstructionEnabled parameter is displayed in the Activation setting in the componentservice browser. You can modify this value first.
// Obtain the Construct Method in the ServicedComponent class
[ConstructionEnabled (true, Default = "Database = Test; Da Ta Source = 192.168.1.3; Port = 3306; User Id = root; Password = 1980425 ")]
// ServicedComponent is the base class of all service component classes and provides some methods for activation and construction.
// If you start a program that uses a class that inherits the ServiceComponet class, the com + application is automatically started. However, the startup program must have the corresponding permissions to be configured.
// For manual configuration, use the regsvcs assemblyname (xxx. dll) provided by. net. After configuration, you can verify the configuration in the com + application in the component service of the management tool.
// Strong names must be used to generate the Assembly. Otherwise, the above installation and configuration will fail. In addition, when the. net class library is created, the default value of ComVisible is false.
// You must change this attribute to true in the AssemblyInfo. cs file before installation.
Public class TestComponent: ServicedComponent, IEnterpriseServiceTest
{

Private string connStr = "";

Protected override void Construct (string s)
{
This. connStr = s;
}

# Region imo-iseservicetest Member

// This attribute indicates that the transaction is automatically processed. If the transaction succeeds, ContextUtil. SetComplete is automatically called to complete the transaction,
// If an exception occurs during the transaction process, ContextUtil. SetAbort is automatically called to stop the transaction,
// If you do not use this attribute, You need to manually call them in the program to complete or stop the transaction.
// The task (the Code executed) that supports the transaction method must support the transaction operation to show the effect of the transaction (such as writing the database). if the transaction is not supported,
// This cannot be used for rollback. (For example, if you write a file and change the value of a variable, you can call ContextUtil after it is completed successfully. setAbort cannot roll back completed operations (for example:
// Change the written file back to the status before writing, or change the changed file back to the original value)
// In addition, ContextUtil. EnableCommit indicates that the transaction processing is satisfactory. ContextUtil. DisableCommit indicates that the transaction processing is not satisfactory, but none of them indicate that the transaction is completed.
// [AutoComplete ()]
Public string ProceedStr (string str)
{
MySqlConnection conn = null;

Try
{
Conn = new MySqlConnection (this. connStr );
String comment STR = "insert into t1 (name) values (wo cao ni ma )";
MySqlCommand cmd = new MySqlCommand (reverse STR, conn );
Conn. Open ();
Cmd. ExecuteNonQuery ();
Cmd. ExecuteNonQuery ();
ContextUtil. SetComplete (); // This statement does not take effect after submission. The setting for the last execution of the method prevails.

Cmd. CommandText = "insert into t1 (name) values (hello )";
Cmd. ExecuteNonQuery ();
ContextUtil. SetComplete ();
Return "OK ";
}
Catch (Exception ex)
{
ContextUtil. SetAbort (); // if there is no following sentence, the entire three statements are not inserted successfully.
ContextUtil. SetComplete (); // after this execution, the first two correct statements are successfully inserted, and the last one is not inserted.
Throw ex;
}
Finally
{
If (conn! = Null)
Conn. Close ();
}

}

# Endregion
}
}


No components:

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. EnterpriseServices;
Using MySql. Da Ta;
Using MySql. Da Ta. MySqlClient;

Namespace NoComptESTest
{
Public class NoComptTest
{
// For services without components, ServiceConfig is used to configure the service, and ServiceDomain. Enter (config) is called to use the configuration to create the environment. The commit of transactions is the same as that of services using components.
// Use ContextUtil or use the [AutoComplete ()] attribute to modify the method, and then call ServiceDomain. Leave () to release the environment
// A service without components cannot have all functions, and the called program does not need to reference System as a service with components. enterpriseServices, you only need to reference this assembly.
Public static bool Execute (bool isAdd)
{
ServiceConfig config = new ServiceConfig ();
Config. Transaction = TransactionOption. Required;
ServiceDomain. Enter (config );
MySqlConnection conn = null;
Try
{
Conn = new MySqlConnection ("Database = Test; Da Ta Source = sai; Port = 3306; User Id = root; Password = 1980425 ");
String plain text = "insert into t1 (name) values (ni ma bi )";
MySqlCommand cmd = new MySqlCommand (plain text, conn );

Conn. Open ();

Cmd. ExecuteNonQuery ();

Cmd. ExecuteNonQuery ();

Cmd. ExecuteNonQuery ();

If (isAdd)
ContextUtil. SetComplete ();
Else
ContextUtil. SetAbort ();
Return true;

}
Catch (Exception ex)
{
ContextUtil. SetAbort ();
Console. WriteLine (ex. ToString ());
Return false;
}
Finally
{
If (conn! = Null)
Conn. Close ();
ServiceDomain. Leave ();
}
}
}
}


Call the component code: <

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.