不确定在Azure Function中如何解析程序集引用,但以下代码在控制台应用程序中能正常工作(我已经尝试过使用.net核心控制台和.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.'"
任何帮助或见识将不胜感激。谢谢。
答案 0 :(得分:2)
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(NET核心):
https://blog.wille-zone.de/post/dependency-injection-for-azure-functions/