COM 异常:“SerializationException:输入流不是有效的二进制格式。起始内容...”
我有一个 COM 程序集(我们称之为 com1.dll),我在一些 C# 代码中引用它。当我添加引用时,我在“引用”节点下看到一个 Interop.com1.dll。如果我从 Visual Studio 执行该应用程序,以下代码将正常运行。
public void DoStuff()
{
var customer = new com1.Customer();
customer.DoSomething();
}
然后,我运行构建脚本,执行以下 nAnt:
<tlbimp output="Interop.com1.dll" typelib="com1.dll" namespace="com1"/>
当
<csc output="myapp.exe" target="winexe" debug="true">
<sources>
...
</sources>
<references>
<include name="Interop.com1.dll"/>
...
</references>
</csc>
我执行从构建脚本生成的程序集时,它会在 var customer = new com1.Customer();
代码行上出现错误,其中包含以下堆栈跟踪:
System.Runtime.Serialization.SerializationException: The input stream is not a valid binary format. The starting contents (in bytes) are: 44-65-76-45-78-70-72-65-73-73-2E-58-74-72-61-45-64 ...
at System.Runtime.Serialization.Formatters.Binary.SerializationHeaderRecord.Read(__BinaryParser input)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadSerializationHeaderRecord()
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at System.ComponentModel.Design.DesigntimeLicenseContextSerializer.Deserialize(Stream o, String cryptoKey, RuntimeLicenseContext context)
at System.ComponentModel.Design.RuntimeLicenseContext.GetSavedLicenseKey(Type type, Assembly resourceAssembly)
at System.ComponentModel.LicenseManager.LicenseInteropHelper.GetCurrentContextInfo(Int32& fDesignTime, IntPtr& bstrKey, RuntimeTypeHandle rth)
at MyApp.MyClass.DoStuff()
我的问题是有人知道为什么吗?
I have a COM assembly (let's call it com1.dll) that I'm referencing in some C# code. When I Add Reference I see an Interop.com1.dll in under the References node. If I execute the application from Visual Studio, the following code will run fine.
public void DoStuff()
{
var customer = new com1.Customer();
customer.DoSomething();
}
I then run my build script, executing the following nAnt:
<tlbimp output="Interop.com1.dll" typelib="com1.dll" namespace="com1"/>
and
<csc output="myapp.exe" target="winexe" debug="true">
<sources>
...
</sources>
<references>
<include name="Interop.com1.dll"/>
...
</references>
</csc>
When I execute the assembly generated from the build script it will error on the var customer = new com1.Customer();
line of code with the following stack trace:
System.Runtime.Serialization.SerializationException: The input stream is not a valid binary format. The starting contents (in bytes) are: 44-65-76-45-78-70-72-65-73-73-2E-58-74-72-61-45-64 ...
at System.Runtime.Serialization.Formatters.Binary.SerializationHeaderRecord.Read(__BinaryParser input)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadSerializationHeaderRecord()
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at System.ComponentModel.Design.DesigntimeLicenseContextSerializer.Deserialize(Stream o, String cryptoKey, RuntimeLicenseContext context)
at System.ComponentModel.Design.RuntimeLicenseContext.GetSavedLicenseKey(Type type, Assembly resourceAssembly)
at System.ComponentModel.LicenseManager.LicenseInteropHelper.GetCurrentContextInfo(Int32& fDesignTime, IntPtr& bstrKey, RuntimeTypeHandle rth)
at MyApp.MyClass.DoStuff()
My question is does anyone know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。结果应用程序有一个
\Properties\Licenses.licx
文件。当从 NAnt 构建应用程序时,我们将该文件包含在
块中。由于某种原因,在我添加互操作引用之前,这一直有效。我需要做的是使用 NAnt
任务从 licx 创建一个许可证文件。该任务的输出替换了
部分中的\Properties\LIcenses.licx
文件构建脚本的。由此看来,鲍勃确实是我的叔叔了。
I figured this out. Turns out the application has a
<project>\Properties\Licenses.licx
file. When building the application from NAnt we were including that file in the<csc><resources/></csc>
block. For some reason this worked until I added the interop references.What I needed to be doing was creating a license file from the licx using the NAnt
<license/>
task. The output from that task replaced the<project>\Properties\LIcenses.licx
file in the<csc><resources/></csc>
portion of the build script.And with that, Bob truly was my uncle.