Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.94k stars 441 forks source link

ScriptFile + NuGet Autofac dependency #1665

Open lindydonna opened 7 years ago

lindydonna commented 7 years ago

From @jsiegmund on July 3, 2017 19:39

I've created a Functions project which is set-up as a web project with the functions executable as 'start external program' as per @lindydonna 's instructions. So far so good, everything works. In the project I'm referencing a DLL from another project, also works fine.

Now I wanted to implement Autofac to do some dependency injection magic. So I added a NuGet reference to that project and created a ServiceLocator class for implementation. Now when I run my function, it fails stating it cannot find the Autofac ServiceLocator constructor. Very strange. Is there anything else I need to do in order to get these dependencies working? I checked the bin folder which holds all of the required DLLs.

For repro, get my code from https://github.com/jsiegmund/bots-domoticz. It currently also fails on a similar problem loading the Newtonsoft Json package.

Copied from original issue: Azure/azure-functions-cli#175

lindydonna commented 7 years ago

I think this isn't currently possible, since we don't support DI. @fabiocav could you provide more info?

christopheranderson commented 7 years ago

Related: https://github.com/Azure/azure-webjobs-sdk/issues/1206

paulbatum commented 7 years ago

Looks like the function code is here. I can see the commented out service locator constructor call.

I removed the DI label because this looks more like an assembly loading issue.

christopheranderson commented 7 years ago

@jsiegmund - most likely this is related to an autofac versionn mismatch. You need to use 4.2.1 (https://github.com/Azure/azure-webjobs-sdk-script/blob/dev/src/WebJobs.Script.WebHost/packages.config#L3)

hansmbakker commented 7 years ago

I had the same problem (that the AutofacServiceLocator class would not be found at runtime but that all was fine at compile time). The workaround is to include a copy of https://github.com/autofac/Autofac.Extras.CommonServiceLocator/blob/develop/src/Autofac.Extras.CommonServiceLocator/AutofacServiceLocator.cs in your project.

I followed https://blog.kloud.com.au/2016/11/21/managing-dependencies-in-azure-functions/ but I'm using the precompiled approach - no .csx.

However that assembly loading issue is a problem indeed.