Autofac 动态程序集绑定在 Azure 函数中不起作用



不确定如何在 Azure 函数中解析程序集引用,但以下代码在控制台应用程序中工作正常(我已经尝试过使用 .net core 控制台和 .net 4.6.1 控制台(。如果我尝试在函数应用中使用相同的代码,则会收到代码注释中提到的错误消息。我正在使用最新版本 (15.10.2046.0( 的函数和 Web 作业工具。

注意:我已手动将 myplugin.dll 复制到函数应用的 bin 文件夹中。我在控制台应用程序上所做的同样的事情,其中相同的代码工作。

var assemblyRoot = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var pluginPath = Directory.GetFiles(assemblyRoot, $"myplugin.dll").FirstOrDefault();
var pluginAssembly = Assembly.LoadFile(pluginPath);
var builder = new ContainerBuilder(); // I am using Autofac
builder.RegisterAssemblyTypes(pluginAssembly).AsImplementedInterfaces().SingleInstance();
var container = builder.Build();
 // Here I am getting errors like "Unable to find assembly '<assembly>'. Are you missing a private assembly file?" for various referenced assemblies like automapper, MongoDB.Bson etc.
var plugIn = container.Resolve<IPlugin>(); 
// Here I am getting "'IPlugIn' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.'"

任何帮助或见解将不胜感激。谢谢。

V1 (NET 4.6(:

https://blog.wille-zone.de/post/azure-functions-dependency-injection/

https://blog.wille-zone.de/post/azure-functions-proper-dependency-injection/

http://codingsoul.de/2018/01/19/azure-function-dependency-injection-with-autofac/

V2(净核心(:

https://blog.wille-zone.de/post/dependency-injection-for-azure-functions/

http://codingsoul.de/2018/06/12/azure-functions-dependency-injection-autofac-on-functions-nuget-package/

最新更新