Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding COM Objects to Asp Net Core

I have a Unit Test project that makes use of Interop.ADODB. Here is the code:

public CDO.Message ReadMessage(string emlFileName)
{
    if (string.IsNullOrEmpty(emlFileName)) return null;
    CDO.Message msg = new CDO.MessageClass();
    ADODB.Stream stream = new ADODB.StreamClass();
    stream.Open(Type.Missing,
        ADODB.ConnectModeEnum.adModeUnknown,
        ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, string.Empty, string.Empty);
    stream.LoadFromFile(emlFileName);
    stream.Flush();
    msg.DataSource.OpenObject(stream, "_Stream");
    msg.DataSource.Save();
    return msg;
}

The problem is that I converted this project to .NET Core. I cannot figure out how to import the COM libraries that I need to make this method works. The ones that I need are Interop.ADODB and Interop.CDO.

All this method does is takes an email file and converts to the object so that I may read the values out of it and then compare to the email that was sent. Really a simple unit test to validate email contents.

Is there a way for me to import COM objects or is there a library that replaced CDO.Message that I am suppose to use now?

like image 349
Bagzli Avatar asked Oct 21 '17 17:10

Bagzli


People also ask

Does .NET Core support COM interop?

In . NET Core 3.0, a large portion of this support has been added to . NET Core on Windows. The documentation here explains how the common COM interop technologies work and how you can utilize them to interoperate with your existing COM libraries.

Does .NET Core support COM?

NET Core 3.1 app, that will run in Windows OS, can I use COM? Yes, you can use COM, is the answer.

What is @model in Cshtml?

The @model directive allows access to the list of movies that the controller passed to the view by using a Model object that's strongly typed. For example, in the Index.cshtml view, the code loops through the movies with a foreach statement over the strongly typed Model object: CSHTML Copy.


1 Answers

It's unadvisable to access COM objects from .NET Core, as .NET Core applications are designed to be platform independent, and COM objects such as ADODB.Stream and CDO.Message are specific to Windows.

However, it is indeed possible - if you don't mind late binding and weak typing.

dynamic msg = Activator.CreateInstance(Type.GetTypeFromProgID("CDO.Message", true));

...

dynamic stream = Activator.CreateInstance(Type.GetTypeFromProgID("ADODB.Stream", true));

etc.

This works on .NET Core 2.0, when running on Windows. Apparently, according to this article, it was not possible on previous versions.

Still, it would be better to re-write your method using managed platform-neutral code.

like image 114
Matt Johnson-Pint Avatar answered Sep 24 '22 01:09

Matt Johnson-Pint