English 中文(简体)
Error when instantiating .NET/COM interop class via classic ASP
原标题:

I am having a problem when trying to instantiate a C# .NET class that s been exposed to COM in a classic ASP application. I ve used tlbexp to generate a typelib and registered it in Component Services; now when trying to create the object as such:

Server.CreateObject("The.Class.Name")

I am getting the error:

Server object error ASP 0177 : 80131534

Server.CreateObject Failed

I ve searched around online for information on this error, and found numerous discussions but no solution. The error code 0x80131534 apparently means "COR_E_TYPEINITIALIZATION, a type failed to initialize", which would suggest the problem would be in the constructor. The constructor of the class in question sets a private field to an instance of another class from the same assembly, and then reads some configuration settings from an XML file. This behaviour is unit tested and I ve checked that the file exists; I can t see anything else that could be breaking in there.

A few other points which may or may not be of use:

  • A test .NET project referencing the DLL can instantiate the class just fine; however a test VB6 project referencing the TLB blows up with the same error. Both the DLL and the TLB are in the same location.
  • This application is running locally, on Windows XP Professional SP3 and IIS 5.1.
  • The .NET assembly is built with .NET Framework 2.0, although 3.5 is installed on the machine.

I know other people who don t get this error on their builds, so I believe it may be something environmental. Any suggestions are welcome as I ve been struggling to fix this for some time.

Thanks in advance.

问题回答

We had exactly the same problem with a class that had a constructor. Funnily enough only on older servers, newer ones would work fine.

We fixed it by adding in a blank public default constructor...

public class MyClass
{
    public string MyGString
    {
        get; set;
    }

    //Com Fix
    public MyClass(){}

    //Normal Class
    public MyClass(string myString)
    {
        HashAlgorithm = hashType;
    }

Someone with the same problem to me...

http://social.msdn.microsoft.com/Forums/vstudio/en-US/4b021da2-3fc7-4f20-b3d0-0f90491a232e/regasm-not-registering-all-classes

Newer servers had this version of RegAsm 2.0.50727.5420 old ones had this version 2.0.50727.3053 This could be something to do with why it worked without a blank public default constructor.

If you can, try using your library from another dynamic, COM enabled language. If you don t know of one, here s a quick snippet that you can use, if you have perl handy. (If you don t, grab ActivePerl to get started really quick)

use strict;
use warnings;
use Win32::OLE;

my $object = Win32::OLE->new( my.object.1 )
    or die "Unable to create my.object.1!";
if (my $error = Win32::OLE->LastError()) {
    die "Still got an error starting up: $error
";
}
print "Good!
";

Alternatively, if Python is your gig, grab and install PyWin32 and try this:

try:
    import win32com.client as w32c
    from win32com.client import util
except ImportError:
    print "
pywin32 package must be installed.  Available https://sourceforge.net/projects/pywin32/
"
    sys.exit()
lib = w32c.dynamic.Dispatch("my.object.1")
# error checking and more, yatta

If this works, then it s likely a configuration issue with your ASP app. If this doesn t, then it means the object that you re trying to create either a) isn t registered, or b) has a problem with the library. .NET dll s must be set up properly to export to COM. Was this .NET library one you created yourself?

I m pretty sure Regasm.exe needs to be used to register a .Net dll to expose it to COM

Interesting post on the subject http://www.simple-talk.com/dotnet/visual-studio/build-and-deploy-a-.net-com-assembly/





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签