0

Not sure how assembly references get resolved in Azure Function but the following code works fine in a console app(I have tried with both .net core console and .net 4.6.1 console). If I try to use the same code in a function app, I would get the error messages I have mentioned in my code comments. I am using latest version (15.10.2046.0) of Functions and Web Job Tools.

Note: I have manually copied myplugin.dll into my function app's bin folder. Same thing I have done with my console app where the same code works.

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.'"

Any help or insight would be appreciated. Thank you.

Travis Illig
  • 23,195
  • 2
  • 62
  • 85
Alpesh
  • 606
  • 6
  • 15

1 Answers1

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 Core):

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/

Alexey Rodionov
  • 1,436
  • 6
  • 8
  • Thank you for effort to direct me to some useful resources. Unfortunately Its same thing what I have already done but directly inside function method instead of creating a bootstrapper. builder.RegisterAssemblyTypes() still doesn't work. – Alpesh Apr 03 '19 at 09:49