Writing COM in C#

Shalom Levi and me have been in need to be able to write a com component in our C# team to be able to use it in an ASP page from a legacy system we have here.
This system needed to connect to an ldap server so we wrote the COM component in C#.
We struggled with the creation of the interface and the code while using examples like
Building COM Objects in C# from CodeProject.com.
both of them made the same kind of code assumption of creation of an interface implementing IDispatch and implementation of the interface in a class.
for example:
namespace Database_COMObject { [Guid("694C1820-04B6-4988-928F-FD858B95C880")] public interface DBCOM_Interface { [DispId(1)] void Init(string userid , string password); [DispId(2)] bool ExecuteSelectCommand(string selCommand); [DispId(3)] bool NextRow(); [DispId(4)] void ExecuteNonSelectCommand(string insCommand); [DispId(5)] string GetColumnData(int pos); } // Events interface Database_COMObjectEvents [Guid("47C976E0-C208-4740-AC42-41212D3C34F0"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface DBCOM_Events { } [Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E"), ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(DBCOM_Events))] public class DBCOM_Class : DBCOM_Interface { private SqlConnection myConnection = null ; SqlDataReader myReader = null ; public DBCOM_Class() { }
they allso talk about creating a strong name like this
sn -k Database_COM_Key.snk
and adding to the assembly the strong name
[assembly: AssemblyKeyFile("Database_COM_Key.snk")]

this is all good and true but missing some extra work we had to do.
first of all we simplified the source code like this:
[Guid("62525C1A-6BAA-43e5-BDF7-BACD89980022")] public interface _LDAPSeq { bool IsAuthenticated(int IDCode,int IDNumber,string password); } [Guid("214E9831-3ABE-4530-BDD6-04A651F55A93")] public class LDAPSeq : _LDAPSeq { public LDAPSeq() { }

we also made a strong named .snk file and added it to the assembly then we went along and installed the DLL in the GAC.

gacutil /i LDAPSeq.dll

now it works flawlessly.
though we had a few more things to do it now works as a COM object
if you ever encounter problems with the .Net to COM you need to look at Show Us Your blog post it has lots of information regarding the errors you might expect.
i hope this will help you guys out there when you get to the same point

Speak Your Mind


This site uses Akismet to reduce spam. Learn how your comment data is processed.