简体   繁体   English

ASP.NET和COM互操作

[英]ASP.NET and COM interop

I am trying to use a COM component in my asp.net web application. 我正在尝试在asp.net Web应用程序中使用COM组件。 In this Component I am passing the path of an image file stored in my server directory and this component is giving me an IPicture datatype in return. 在此组件中,我传递了存储在服务器目录中的图像文件路径,并且该组件为我提供了IPicture数据类型。

Following are the steps that I am going through, I post my questions at each steps so that it remains specific. 以下是我要执行的步骤,我在每个步骤中发布我的问题,以使其始终保持具体。

  1. I am referencing the DLL in my solution. 我在解决方案中引用了DLL。
  2. Early binding the DLL and instantiating the specific class and the interface. 尽早绑定DLL并实例化特定的类和接口。

Question 1. First up, I am not able to see all the methods and properties visible in the IL DASM in the VS intelisence. 问题1.首先,我无法在VS智能中看到IL DASM中可见的所有方法和属性。 Only a few are available. 只有几个可用。 why? 为什么?

ViewerClass cview = null; // ViewerClass is the class from the COM Component
Viewer Icsview = null; // Viewer is one of the interfaces that ViewerClass has.
cview = new ViewerClass();

if (cview is Viewer)
{
    Icsview = (Viewer)cview;
    Icsview.Open(filePath, 0, 0); // filePath is a string, passing the path of the file present in my local directory.

Question 2: This is where the error occures: - when the code comes to this line, I receive an InvalidCast exception: 问题2:这是发生错误的地方:-当代码到达此行时,我收到一个InvalidCast异常:

错误的屏幕截图

  1. Calling the method that is supposed to work on the file and convert into an IPicture type variable: 调用应该在文件上起作用的方法并转换为IPicture类型变量:

In current scenario, this code does not execute. 在当前情况下,此代码无法执行。

I have tried in different ways to talk to this Component. 我已经尝试以不同的方式来与该组件交谈。 I have tried latebinding. 我尝试过后期绑定。

following is the code which I am using for latebinding: 以下是我用于后期绑定的代码:

object objCSViewerLateBound;
Type objTypeCSViewer;
object[] arrayInput = new object[3];
arrayInput[0] = filePath;
arrayInput[1] = 0;
arrayInput[2] = 0;
objTypeCSViewer = Type.GetTypeFromCLSID(new Guid("{89251546-3F1C-430D-BA77-F86572FA4EF6}"));
objCSViewerLateBound = Activator.CreateInstance(objTypeCSViewer);
objTypeCSViewer.InvokeMember("Open", BindingFlags.Default | BindingFlags.InvokeMethod, null, objCSViewerLateBound, arrayInput);
// getting the values from the properties
double viewMaxX = (double)objTypeCSViewer.InvokeMember("ViewMaxX", BindingFlags.Default | BindingFlags.GetProperty, null, objCSViewerLateBound, new object[] { });
double viewMaxY = (double)objTypeCSViewer.InvokeMember("ViewMaxY", BindingFlags.Default | BindingFlags.GetProperty, null, objCSViewerLateBound, new object[] { });
double viewMinX = (double)objTypeCSViewer.InvokeMember("ViewMinX", BindingFlags.Default | BindingFlags.GetProperty, null, objCSViewerLateBound, new object[] { });
double viewMinY = (double)objTypeCSViewer.InvokeMember("ViewMinY", BindingFlags.Default | BindingFlags.GetProperty, null, objCSViewerLateBound, new object[] { });
// geting the image height and width
int imagewidth, imageheight, pictype;
imagewidth = 1280; imageheight = 800; pictype = 1;
object[] previewDetails = new object[7];
previewDetails[0] = viewMinX;
previewDetails[1] = viewMinY;
previewDetails[2] = viewMaxX;
previewDetails[3] = viewMaxY;
previewDetails[4] = imagewidth;
previewDetails[5] = imageheight;
previewDetails[6] = pictype;
IPicture pict = (IPicture)objTypeCSViewer.InvokeMember("Preview", BindingFlags.Default | BindingFlags.GetProperty, null, objCSViewerLateBound, previewDetails); 

latebinding works, but only for a few steps, I can pass through the Open method, I can get the values from the properties, but in the last line, I receive an error that TargetInvocationException was unhandled by user code. 后期绑定可以工作,但是仅需几个步骤,我就可以通过Open方法,可以从属性中获取值,但是在最后一行,我收到一个错误,提示用户代码未处理TargetInvocationException。 I dont know what it means. 我不知道这是什么意思。

  1. Why is this error occuring? 为什么会发生此错误?

Please note: I have tried using the component through Javascript block. 请注意:我已经尝试通过Javascript块使用该组件。 It works fine. 工作正常。

Question 4: I have tried the same code snippets posted here in a winforms application. 问题4:我尝试过在winforms应用程序中发布的相同代码段。 It works without any problem. 它可以正常工作。 this gets me thinking there is something wrong in my approach. 这让我觉得我的方法有问题。 Is there any special process needed to talk to a COM Component in an ASP.NET app? 与ASP.NET应用程序中的COM组件进行通讯是否需要任何特殊的过程?

Update: OK. 更新:好的。 I have finally got this COM object to work, but some problem still remains. 我终于有了这个COM对象,但是仍然存在一些问题。 Some brilliant guy in some other forum(I dont remember now which one)asked me to check the ThreadingModel of the component, as it turns out it is set to Apartment. 在其他论坛上(我现在不记得是哪个论坛)的某个才华横溢的人要我检查该组件的ThreadingModel,因为事实证明它设置为Apartment。 So I changed the code as following: 所以我将代码更改如下:

    ...
    using System.Threading;

    namespace...
    protected void Button1_Click(object sender, EventArgs e)
     {
       Thread thread = new Thread(new ThreadStart(STAOperation));
       thread.SetApartmentState(ApartmentState.STA);
       thread.Start();
       thread.Join();

     }
     public void STAOperation()
      {
        CSViewerRL.ViewerClass csv = new ViewerClass();
        csv.Open(filePath, 0, 0);//strange error
        //other codes..
      }

Now this code works, but once in a while, I am getting a strange error while debugging, which states AccessViolationException: Attempted to read or write protected memory. 现在这段代码可以工作了,但是偶尔,我在调试时遇到一个奇怪的错误,该错误指出AccessViolationException:尝试读取或写入受保护的内存。 This is often an indication that other memory is corrupt. 这通常表明其他内存已损坏。

generally the app runs fine the first time I try to debug, but this error occures mostly on the second or third time I press f5 from my VS while the ASP.Net server is still running. 通常,应用程序在我第一次尝试调试时运行良好,但是此错误主要发生在我第二次或第三次在ASP.Net服务器仍在运行时从VS按f5时。 and generally it goes away if I stop the server and run again. 通常,如果我停止服务器并再次运行,它就会消失。 I think this problem is because of this new STA thread. 我认为这个问题是由于这个新的STA线程。 Maybe I am not handling the new thread correctly. 也许我没有正确处理新线程。 Or there is some other thing I should look into? 还是我应该考虑的其他事项? Can someone point out whatshould I do under this circumstance? 有人可以指出在这种情况下我该怎么办?

Please Note: I dont know if this can be considered an answer or not, but I cant seem to edit my question here, and the comment has too short space... :-( 请注意:我不知道这是否可以视为答案,但是我似乎无法在此处编辑我的问题,并且注释的空间太短... :-(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM