本文介绍了如何在AssemblyResolve中使用AppDomain.CreateDomain?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用内存从WCF加载我的程序集.一切正常,何时:

I want to load my assemblies from WCF by using memory. Everything is working good WHEN:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Assembly[] assBefore = AppDomain.CurrentDomain.GetAssemblies();
foreach (byte[] binary in deCompressBinaries)
    loadedAssembly = AppDomain.CurrentDomain.Load(binary);

但是我想使用AppDomain.CreateDomain,而不是当前域:

But I want to use AppDomain.CreateDomain, not the current domain:

protected void LoadApplication()
{
    this.ApplicationHost = AppDomain.CreateDomain("TestService", null, new AppDomainSetup
    {
        ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase
    });

    ApplicationHost.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve);
    foreach (AssemblyName asmbly in System.Reflection.Assembly.GetExecutingAssembly().GetReferencedAssemblies())
    {
        ApplicationHost.Load(asmbly);
    }
    List<byte[]> deCompressBinaries = new List<byte[]>();
    foreach (var item in AppPackage.Item.AssemblyPackage)
        deCompressBinaries.Add(item.Buffer);
    var decompressvalues =  DeCompress(deCompressBinaries);
    deCompressBinaries.Clear();
    deCompressBinaries = decompressvalues.ToList();

    foreach (byte[] binary in deCompressBinaries)
        ApplicationHost.Load(binary);

    Assembly[] assAfter = AppDomain.CurrentDomain.GetAssemblies();
}

Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
    return Assembly.Load(args.Name);
}

我使用以下两个类库,ClassLibrary1和ClassLibrary2:

I have two class libraries, ClassLibrary1 and ClassLibrary2 using the below:

namespace ClassLibrary2
{
    public class Class1 : MarshalByRefObject
    {
        public Class1()
        {

        }

        public int GetSum(int a , int b)
        {
            try
            {
                ClassLibrary1.Class1 ctx = new ClassLibrary1.Class1();
                return ctx.Sum(a, b);
            }
            catch
            {
                return -1;
            }
        }

        public int GetMultiply(int a, int b)
        {
            return a * b;
        }
    }
}

Classlibrary2取决于ClassLibrary1.所以我正在使用assemblyresolver.但是我在 ApplicationHost.Load(binary); :

Classlibrary2 depends on ClassLibrary1. So I am using assemblyresolver. But I get an error on ApplicationHost.Load(binary);:

此外,它也不会点火.我的光标不会转到Assemblyresolver方法.如何使用带有resolve方法的AppDomain.CreateDomain?

Also it is NOT FIRING ASSEMBLYRESOLVER. My cursor is not going to the Assemblyresolver method. How do I use AppDomain.CreateDomain with the resolve method?

推荐答案

我个人不喜欢从字节数组中加载程序集.我认为最好将程序集保存到一个临时文件夹,然后从该文件夹中加载它们.看一下本文: 应用程序域很难…… .

I personally don't like loading assemblies from a byte array. I think it is better to save your assemblies to a temporary folder and then load them from that folder. Take a look at this article: Application Domains is hard….

这篇关于如何在AssemblyResolve中使用AppDomain.CreateDomain?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 14:22