为什么将BAD IL FORMAT返回以从wcf服务加载程序集? [英] Why return BAD IL FORMAT to load assembly from wcf service?
问题描述
我要加载此类库:
namespace ClassLibrary1
{
public class Class1
{
public Class1()
{
}
public static int Sum(int a, int b)
{
return a + b;
}
}
}
我有一个 wcf服务,它向我返回了一个byte[]
数组( ClassLibrary1 ),我无法加载该程序集
I have a wcf service which returns to me a byte[]
array (ClassLibrary1) i can not load this assembly
static void Main(string[] args)
{
FileTransferService.ApplicationHostServiceClient client = new FileTransferService.ApplicationHostServiceClient();
FileTransferService.AssemblyPackage[] asmbs = client.GetFile();
//var newDomain = AppDomain.CreateDomain("FooBar", null, null);
foreach (FileTransferService.AssemblyPackage item in asmbs)
{
byte[] mybuffer = item.Buffer;
new AssemblyLoader().LoadAndCall(mybuffer);
}
}
public class AssemblyLoader : MarshalByRefObject
{
public void LoadAndCall(byte[] binary)
{
Assembly loadedAssembly = AppDomain.CurrentDomain.Load(binary);
object[] tt = { 3, 6 };
Type typ = loadedAssembly.GetType("ClassLibrary1.Class1");
MethodInfo minfo = typ.GetMethod("Sum", BindingFlags.Public);
int x = (int)minfo.Invoke(null, tt);
Console.WriteLine(x);
}
}
此方法返回错误: Assembly loadingAssembly = AppDomain.CurrentDomain.Load(binary);
错误:
BADIMAGEFORMAT EXCEPTION
Could not load file or assembly '4096 bytes loaded from Client2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
例外:
Bad IL format
我搜索了此类错误,但没有确切的解决方案.我想使用AppDomain加载程序集.
i have googling this kind of error but no exact solution. i want to load my assembly using AppDomain.
推荐答案
在这种情况下要检查的第一件事是,您收到的byte[]
与原始内容完全相同 ,因为有使一堆二进制代码混乱的许多方法.也许将文件写入磁盘(File.WriteAllBytes
)和您喜欢的文件比较工具,或者使用诸如base-64或sha-1哈希之类的内容来验证内容.我强烈怀疑您会发现它不一样.
The first thing to check in this scenario is that the byte[]
you received is exactly identical to the original, as there are many ways of making a mess of handing a chunk of binary. Perhaps write the file to disk (File.WriteAllBytes
) and your favourite file compare tool, or use something like base-64 or a sha-1 hash to validate the contents. I strongly suspect you'll find it isn't the same.
这篇关于为什么将BAD IL FORMAT返回以从wcf服务加载程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!