首页 > 解决方案 > 在 azure 函数的新应用程序域中创建对象的实例会引发 FileNotFoundException

问题描述

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

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是具有公共构造函数的自定义公共类。MyCustomLib.dll它已在我在 azure 函数中引用的类库 () 中实现。该类实现了在另一个类库 ( 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 文件夹...

标签: c#azureazure-functionsappdomain

解决方案


推荐阅读