Adding COM Objects to Asp Net Core

旧巷老猫 提交于 2019-11-28 04:28:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/46866103/adding-com-objects-to-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!