方法调用从ASP到.NET COM失败

时间:2017-02-16 15:24:08

标签: vb.net vbscript asp-classic com vb6

背景

我有一个用ASP(VBScript)编写的遗留应用程序,它调用用VB6编写的COM组件。我们正在分阶段升级,需要首先将COM组件更新到.NET,同时保持与ASP的互操作性。

场合

  1. 我创建了一个示例.NET类,并根据MSDN文档使用各种属性将其公开给COM系统
  2. 我能够实例化该课程
  3. 我可以在课程
  4. 上调用所需的方法

    问题

    1. 参数值未发送到COM方法。例如所有数值类型在方法内都有值0;所有引用类型在方法内都具有null值。文字字符串正确传递给方法。但是,不传递字符串变量。
    2. 返回值的问题相同。无论方法返回什么值,ASP上的变量都有默认值(视情况而定为0或null)。
    3. COM代码

      <ComVisible(True)>
      <InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>
      Public Interface IClass1
          Function Triple(ByVal input As String) As Integer
      End Interface
      
      <ComVisible(True)>
      <ProgId("TESTCOM.Class1")>
      <ClassInterface(ClassInterfaceType.None)>
      Public Class Class1
          Inherits ServicedComponent
          Implements IClass1
      
          Public Sub New()
              ' needed for COM
          End Sub
      
          Public Function Triple(input As String) As Integer Implements IClass1.Triple
              IO.File.WriteAllText("C:\TestCOM.Class1_Triple.log", String.Format("[{0:s}] Input: {1}", Date.Now, input)) ''' this file is updated; so I know method is being called.
              Return 97
          End Function
      End Class
      

      备用COM代码

      <ComVisible(True)>
      <ProgId("TESTCOM.Class1")>
      <ComClass("E26FE8A0-8AC7-4824-9776-30ECDD473AA3")>
      Public Class Class1
          'Inherits ServicedComponent
          'Implements IClass1
      
          Public Sub New()
              ' needed for COM
          End Sub
      
          Private Const LogMessageFormat As String = "[{0:s}] Input: {1}, {2}" + vbCrLf
      
          Public Function Triple(csinput As String, nInput As Integer) As Integer 'Implements IClass1.Triple
              IO.File.AppendAllText("C:\TestCOM.Class1_Triple.log", 
                                    String.Format(LogMessageFormat, Date.Now, if(csinput isnot Nothing, csinput, "**NULL**"), nInput))
              Return 97
          End Function
      End Class
      

      ASP代码

      dim testNETCOM 
      set testNETCOM = Server.CreateObject("TESTCOM.Class1")
      
      ' ** use this to check if there was any error during instantiation    
      If Err.Number <> 0 Then
         Response.Redirect("http://"&Err.Description&"/")
        'Response.Write (Err.Description& "<br><br>")
      else
          'Response.Redirect("http://no-error/")
      End If
      ' ** use this to check if object is actually instantiated
      if testNETCOM is nothing then
          Response.Redirect("http://no-com/")
      else
          'Response.Redirect("http://yes-com/")
      end if
      
      dim nInput
      set nInput = 41
      
      dim nOutput
      set nOutput = -1
      set nOutput = CLng(testNETCOM.Triple("test message")) ' this string is received in the method
      set nOutput = CLng(testNETCOM.Triple(CStr(nInput)))   ' this string is not received in the method
      
      ' ** use this to check if return value is what we expected
      if nOutput <> 0 then
          Response.Redirect("http://test/")
      else
          Response.Redirect("http://notest/") ''' **this happens**
      end if
      

2 个答案:

答案 0 :(得分:2)

问题在于ASP

的语法
  1. 对于简单数据类型,请勿使用“set”
  2. 对于对象,请使用“set”
  3. 因为我使用set来表示简单类型,所以在赋值过程中出现了一个错误,并且变量的值为null / nothing / empty。

    set nInput = 41 ' this is wrong
    nInput = 41     ' this is right
    set nOutput = CLng(testNETCOM.Triple(CStr(nInput))) ' this is wrong
    nOutput = CLng(testNETCOM.Triple(CStr(nInput)))     ' this is right
    

答案 1 :(得分:0)

尝试其中任何一个:

this.model.attributes.AcmeData.attributes.isEmpty

或者:

<ComVisible(True)> 
<Guid("79571A9D-2345-48D9-8F86-7F6761A97DBA")>
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>
Public Interface IClass1
    Function Triple(ByVal input As String) As Integer
End Interface

<ComVisible(True)>
<Guid("E26FE8A0-8AC7-4824-9776-30ECDD473AA3")>
<ProgId("TESTCOM.Class1")>
<ClassInterface(ClassInterfaceType.None)>
Public Class Class1
    Inherits ServicedComponent
    Implements IClass1

    Public Sub New()
        ' needed for COM
    End Sub

    Public Function Triple(input As String) As Integer Implements IClass1.Triple
        IO.File.WriteAllText("C:\TestCOM.Class1_Triple.log", String.Format("[{0:s}] Input: {1}", Date.Now, input)) ''' this file is updated; so I know method is being called.
        Return 97
    End Function
End Class