为什么返回 BAD IL FORMAT 以从 wcf 服务加载程序集? [英] Why return BAD IL FORMAT to load assembly from wcf service?

查看:17
本文介绍了为什么返回 BAD IL FORMAT 以从 wcf 服务加载程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加载这个类库:


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 loadedAssembly = AppDomain.CurrentDomain.Load(binary);

Error return to me in this method : Assembly loadedAssembly = 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆