在Azure函数中的新应用程序域中创建对象的实例会引发FileNotFoundException

时间:2018-07-25 08:03:26

标签: c# azure azure-functions appdomain

我需要在新的应用程序域中运行一些代码。因此,我试图在新的应用程序域中创建我的对象的实例...这是我正在使用的代码:

public static class Program
{
    private static ITemplateEngineProvider _templateEngineProvider;

    static Program()
    {
        AppDomain ad = AppDomain.CreateDomain("New domain");

        ObjectHandle handle = ad.CreateInstance(
                assemblyName: typeof(RazorTemplateEngineProvider).Assembly.FullName,
                typeName: "RazorTemplateEngineProvider"
                //ignoreCase: false,
                //bindingAttr: BindingFlags.CreateInstance,
                //binder: null,
                //args: new object[] { new string[] { templatePath, layoutPath } },
                //culture: CultureInfo.InvariantCulture,
                //activationAttributes: null
                );

            _templateEngineProvider = (RazorTemplateEngineProvider)handle.Unwrap();
    }
}

RazorTemplateEngineProvider是具有公共构造函数的自定义公共类。我已经在azure函数中引用的类库(MyCustomLib.dll)中实现了它。该类实现了一个在另一个类库(IMyCustomLib.dll)中定义的接口,该类库仅由先前的类库而不是由Azure函数引用。

目前RazorTemplateEngineProvider类中没有代码:

public class RazorTemplateEngineProvider : MarshalByRefObject, ITemplateEngineProvider
{
    public RazorTemplateEngineProvider()
    { }
}

当我尝试做ad.CreateInstance时,抛出了FileNotFoundException

  

无法加载文件或程序集“ MyCustomLib.dll,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = ...”或其依赖项之一。系统找不到指定的文件。

但是该文件存在,应该已经正确加载了...如果我运行此“查询”,则实际上是

IEnumerable<string> loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies()
   .Where(a => !a.IsDynamic && !a.FullName.Contains("Version=0.0.0.0") && File.Exists(a.Location) && !a.Location.Contains("CompiledRazorTemplates.Dynamic") &&  a.FullName.Contains("My"))
   .Select(f => f.FullName)
   .ToArray();

我都看到了我的dll。那么,为什么我会收到该错误?

谢谢

更新

我认为问题很明显,因为我将代码复制并粘贴到控制台应用程序中,并且可以正常工作。

更新

我正在观看Fusionlong,看来它正在尝试从“错误”路径中加载程序集:file:///C:/Users/csimb/AppData/Local/Azure.Functions.Cli/1.0.12/MyCustomLib.dll ..我希望路径是bin文件夹...

0 个答案:

没有答案