BrokeredComponents TypeLoad和InvalidCastException

时间:2016-09-25 01:52:04

标签: c# dll uwp

我们正在尝试在UWP上使用BrokeredComponents。我们收到 TypeLoadException InvalidCastException

我们定义了一个名为BrokeredComponentsHost的类,它被用作通用类,因此我们可以访问BrokeredComponents中的其他类。

我们为每个组件使用GUID。

这是BrokeredComponentsHost:

namespace BrokeredComponents
{    
    [Guid(BrokeredComponentsHost.ClassId), ComVisible(true)]
    public sealed class BrokeredComponentsHost : IBrokeredComponentFactory
    {
        internal const string ClassId = "881724C4-E330-40D5-BDA1-7D9F7C44FB7C";
        internal const string FactoryInterfaceId = "1FA85472-5B0D-487C-B58E-F8BE9A89D470";
        internal const string BarcodeScannerInterfaceId = "840F54D5-7AF7-4C31-91B0-BA274FCDD737";

        public string LastError { get; set; }

        public IPosScanner GetPosScanner()
        {
            return new PosScanner() { Host = this };
        }
    }

}

这是IBrokeredComponentsFactory:

namespace BrokeredComponents
{
    [Guid(BrokeredComponentsHost.FactoryInterfaceId), ComVisible(true)]
    public interface IBrokeredComponentFactory
    {
        IPosScanner GetPosScanner();
        string LastError { get; set; }
    }
}

这是PosScanner及其界面:

namespace BrokeredComponents
{
    [Guid(BrokeredComponentsHost.BarcodeScannerInterfaceId), ComVisible(true)]
    public interface IPosScanner : IPosDevice
    {
        bool DecodeData { get; set; }
        bool DeviceEnabled { get; set; }
        bool DataEventEnabled { get; set; }
        bool AutoDisable { get; set; }
        byte[] ScanData { get; }
    }
}

当我们尝试创建一个新的BrokeredComponentsHost实例(bcHost = new BrokeredComponents.BrokeredComponentsHost();)时,它会抛出TypeLoadException。

如果我们定义一个新类(例如ProcessExecutor类),重建所有内容,然后尝试再次创建一个BrokeredComponentsHost的新实例,它会抛出InvalidCastException。

我们做 icacls。 / T /授予“所有应用程序包”:RX regsvr32 theproxy.dll

有没有人遇到同样的问题并知道解决方案?

谢谢!

编辑:我收到的确切错误消息是"Unable to cast COM object of type 'BrokeredComponents.BrokeredComponentsHost' to interface type 'BrokeredComponents.IBrokeredComponentFactory'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{1FA85472-5B0D-487C-B58E-F8BE9A89D470}' failed due to the following error: Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))."

我在64位Windows 10上运行,但我的BrokeredComponents项目是Windows 8.1,而BrokeredComponentsProxy是VS 2013.

编辑2:我修复了TypeLoad异常,在Appxmanifest文件中未定义公开类的Extensions是一个错误。我仍然遇到InvalidCastException。

1 个答案:

答案 0 :(得分:0)

感谢富兰克林和劳伦斯。

我已经弄明白了这个错误,原因是主要的UWP项目是针对x64的,而我们的BrokeredComponents和解决方案中的其他项目都是针对x86的。

我将UWP项目更改为x86并注册了.dll,现在效果很好。

相关问题