10 Şubat 2015 Salı

C# - Create COM compatible DLL and use without app.config file with a web service consume

You can make your DLL as COM compatible a way which I learned from an article:


[ComVisible(true)] 

Also Register for COM interop checkbox should be checked from project properties->build page.

You can use Intellisense when using COM object with this:

[ClassInterface(ClassInterfaceType.AutoDual)] 

After all you can use DLL yet from your COM compatible language (except there is no binding). When you try to use you get an error; "OLE IDispatch exception code 0 from System.ServiceModel: ServiceModel...". You have to copy app.config file as yourexefile.EXE.app.config or add these red code parts to your class (I got help for this from Paul Mrozowski):

For add this sample web service to your project, you have to right click at Service References than Add Service Reference-> write https://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx to address and push GO.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.ServiceModel;



namespace TCKimlik
{
    [ComVisible(true)] //for visible COM object
    [ClassInterface(ClassInterfaceType.AutoDual)] //for intellisense at using COM
    [ProgId("TCKimlik.TCSorgula")] //COM object name
   
    public class TCKimlikClass
    {
        public bool Dogrula(string adi,string soyadi,Int64 tc,Int32 dogumT)
        {
            bool ret = false;

            //------ for using COM compatible DLL without APP.CONFIG
            BasicHttpsBinding binding = new BasicHttpsBinding();
            EndpointAddress address = new EndpointAddress("https://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx");

            //------ run --------
                try
            {
                Sorgula.KPSPublicSoapClient servis = new Sorgula.KPSPublicSoapClient(binding, address);

                ret = servis.TCKimlikNoDogrula(tc, adi, soyadi, dogumT);
            }
            catch
            {
            }
            return ret;
        }
    }
}


Now you can use this DLL at COM compatible languages plus .NET. But if you move DLL to client computer without setup you have to register with regasm. I  shared project.

There is a sample usage with Microsoft Visual Foxpro:

o=CREATEOBJECT("tckimlik.tcsorgula")
?o.Dogrula("METİN","EMRE",11111111111,1980)