Use ASP to call the COM + component written by C #
1. Create a class library mytestdll.
2. Right-click the project "mytestdll"-"properties-" generate-"and select" register for com"
3. Open the assemblyinfo. CS file, modify [Assembly: comvisible (true)], and add a strong name file.
4. Open the command line prompt of visual sudio 2008, input guidgen.exe, select define_guid, and click "New guid"
5Code
1. Each class name corresponds to an interface name. The interface name is preceded by a capitalized I
2. Attribute [dispid (n)] should be used for the method declared in the interface
3. The class must have a non-argument constructor.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. runtime. interopservices;
Using system. enterpriseservice
Namespace mytestdll
{
// Here, The GUID is generated in step 1.
[GUID ("FFA4B191-FB5B-4dd5-B7B1-B2F32BF6F1FF")]
Public interface imytestdll
{
[Dispid (0)]
String getabout ();
}
Public class test1: imytestdll
{
Private string summary;
Public test1 ()
{
Summary = "this is my first test ";
}
Public String getabout ()
{
Return summary;
}
}
}
6. Generate a project
ASP test code
<%
Dim o
Set o = server. Createobject ("mytestdll. test1 ")
Response. Write O. getabout ()
Set o = nothing
%>
Tip: If you want to use this COM component developed by C # on another computer, you still need to use regasm to register it.
Method:
First, copy the files in the bin \ DEBUG directory to the target computer, and then open the command prompt line tool and enter:
Regasm the directory/file name you copied to. dll/tlb f:/dll/file name. TLB/codebase
Running can be used on this computer.
COM + instance
I. assemblyinfo. CS
Using system. reflection;
Using system. runtime. compilerservices;
Using system. runtime. interopservices;
Using system. enterpriseservices;
// RelatedProgramThe general information of the set is passed through the following attribute set
// Control. You can modify these attribute values.
// Information associated with the Assembly.
[Assembly: assemblytitle ("mycomplussample")]
[Assembly: assemblydescription ("mycomplussample testing")]
[Assembly: assemblyconfiguration ("")]
[Assembly: assemblycompany ("axisoft")]
[Assembly: assemblyproduct ("mycomplussample")]
[Assembly: assemblycopyright ("Copyright axisoft 2010")]
[Assembly: assemblytrademark ("Web cornucopia")]
[Assembly: assemblyculture ("En-us")]
// Set comvisible to false to make the type in this Assembly
// The COM component is invisible. If you need to access the types in this Assembly from com,
// Set the comvisible attribute of this type to true.
[Assembly: comvisible (true)]
// If this project is made public to com, the following GUID is used for the ID of the Type Library
[Assembly: GUID ("4bc9a9f7-606c-4552-a3fe-aefb39b00ed1")]
// The Assembly version information consists of the following four values:
//
// Main version
// Version
// Internal version number
// Revision No.
//
// You can specify all these values, or use the default values of "internal version number" and "revision number,
// Use "*" as follows:
// [Assembly: assemblyversion ("1. 0. *")]
[Assembly: assemblyversion ("1.0.0.0")]
[Assembly: assemblyfileversion ("1.0.0.0")]
[Assembly: assemblydelaysign (false)]
[Assembly: applicationaccesscontrol (true)]
// [Assembly: assemblykeyfile ("mycomplussample. SNK")]
// [Assembly: assemblykeyname ("")]
// [Assembly: applicationname ("COM + bank server account manager")]
// [Assembly: applicationactivation (activationoption. Server)]
Ii. transerfer. CS
Using system;
Using system. Collections. Generic;
// Using system. LINQ;
Using system. text;
Using system. enterpriseservices;
Using system. Data. sqlclient;
Using system. Data;
Using system. diagnostics;
Using system. reflection;
Using system. runtime. interopservices;
[Assembly: applicationactivation (activationoption. Server)]
// It is not required and can be set during deployment.
[Assembly: applicationname ("mycomplussample")]
Namespace mycomplussample
{
/// <Summary>
/// Summary of transfer.
/// </Summary>
///
/// Configure component's transaction Option
[Transaction (transactionoption. Required)]
// [Objectpooling (true, 1,100)] // you can set the object to be placed in the pool.
[Classinterface (classinterfacetype. autodual)]
/// Enable event tracking
[Eventtrackingenabled (true)]
/// Enable JITA for the component
[Justintimeactivation (true)]
[Comvisible (true)]
Public class transfer: servicedcomponent
{
Public transfer ()
{
Consolewrite ("ucombank. transfe constructor ");
}
Public transfer (string S)
{
Consolewrite ("ucombank. transfe constructor" + S );
}
Private void consolewrite (string strinput)
{
EventLog. writeentry ("ucom COM + service sample", strinput, eventlogentrytype. information );
}
Protected override void activate ()
{
Consolewrite ("ucombank. transfe activated ");
}
Protected override void deactivate ()
{
Consolewrite ("ucombank. transfe deactivated ");
}
Protected override void construct (string S)
{
Base. Construct (s );
}
Protected override bool canbepooled ()
{
Consolewrite ("ucombank. transfe canbepooled ");
Return true;
}
Protected override void dispose (bool disposing)
{
Consolewrite ("ucombank. transfe dispose ");
Base. Dispose (disposing );
}
// Public int getbalance (string strconnectiongstring, string tablename, string ID, itransaction trans)
[Comvisible (true)]
Public int getbalance (string strconnectiongstring, string tablename, string ID)
{
Sqlconnection conn = new sqlconnection (strconnectiongstring );
Sqldataadapter da = new sqldataadapter ("select * from" + tablename + "where account =" + id, Conn );
Dataset DS = new dataset ();
// An error occurs when executing the following statement. The error message is: "an error occurs during logging in distributed transactions. "
Da. Fill (DS );
If (Ds. Tables [0]. Rows. Count> 0)
Return Int. parse (Ds. Tables [0]. Rows [0] ["accountid"]. tostring ());
Else
Return 0;
}
[Comvisible (true)]
Public String getinfo ()
{
Return "my com Plus sample ...";
}
[Comvisible (true)]
Public int getuserid (string connstr, string tablename, string account)
{
Try
{
Int RST;
// Rst = getbalance (connstr, tablename, account, (itransaction) contextutil. Transaction );
RST = getbalance (connstr, tablename, account );
Contextutil. setcomplete ();
Return RST;
}
Catch (exception ex)
{
Contextutil. setabort ();
Lelewrite (ex. Message. tostring ());
Return 0;
}
}
}
}
Iii. Registration
Gacutil/I mycomplussample. dll added to GAC
Regsvcs/Fc mycomplussample. dll registration already exists re-registration, Fc = find create
The registered COM component is found in componentservices. Tab: security does not check enforce access checks fot this Applicaion; tab: identity specifies a user.
Iv. asp call
<%
On Error resume next
Dim tempsql
Tempsql = "3eeee ---"&_
"Eeerer7777erere"
Set OBJ = server. Createobject ("mycomplussample. Transfer ")
Dim str1
Str1 = obj. getinfo ()
Response. Write ("strencript =" & str1 & "<br> ")
If err. Number <> 0 then
Response. Write tempsql + "<br>"
Response. Write err. Description + "<br>"
Response. Write "9999999999999999999999999"
Response. End
End if
%>
References:
Http://topic.csdn.net/u/20080625/13/0294fe91-200c-4939-b36b-c9a2c6781354.html
Http://topic.csdn.net/t/20060314/15/4613620.html
Http://cplus.e800.com.cn/articles/2009/211/1234338268521_3.html
Http://topic.csdn.net/t/20020712/10/868557.html
Http://www.itzhe.cn/news/20071123/21768.html
Http://www.cnblogs.com/illele/archive/2007/10/25/937050.html