将COM对象添加到Asp Net Core

编程入门 行业动态 更新时间:2024-10-25 16:20:53
本文介绍了将COM对象添加到Asp Net Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个使用Interop.ADODB的单元测试项目.这是代码:

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; }

问题是我将此项目转换为.NET Core.我无法弄清楚如何导入使此方法有效所需的COM库.我需要的是Interop.ADODB和Interop.CDO.

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.

我是否可以导入COM对象,或者是否有一个库替换了现在应该使用的CDO.Message?

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?

推荐答案

不建议从.NET Core访问COM对象,因为.NET Core应用程序被设计为与平台无关,并且ADODB.Stream和CDO.Message特定于Windows.

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));

在Windows上运行时,此功能可在.NET Core 2.0上使用.显然,根据本文 ,在以前的版本中是不可能的.

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.

更多推荐

将COM对象添加到Asp Net Core

本文发布于:2023-11-17 05:48:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Asp   Core   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!