Currently if you try to define a non-static C# function, things will blow up in our invoker pipeline with an error "Non-static method requires a target.". We should either allow this, or make this an early compilation error.
I actually had an MVP ask me about this. He asked why we had the restriction, and mentioned that he wanted to be able to do local testing with dependency injection. I'm not sure how that would work really, but it made me question our restriction.
The core SDK allows both static and non-static. We could choose to do the same. It would simplify method signatures. Importantly, this also unblocks DI scenarios, allowing functions to take dependencies as constructor parameters.
We could relax this restriction. This was an early decision made because it reduces a fair amount of complexity, but it's definitely something we can do.
I would like to better understand the scenario, but although the current behavior does add a bit of friction, it does not prevent local testing with DI for the actual function logic.
Truly, statics make local mocking / IoC injection of unit tests all but impossible.
I second that.
Any ETA on this?
Did this get fixed and released?
@StingyJack yes. This is now supported.
@fabiocav - Is there a trick to making it work then? Trying to debug a new Function from VS and its telling me it cant find the function.
public class FunctionExecutor
{
[FunctionName("My_Functions_DbCleanup_ExecuteCleanup_Run")]
public void Run([TimerTrigger("03:00:00", RunOnStartup = true)]
TimerInfo myTimer, ILogger azLogger)
{
// etc...
[2/12/2019 9:30:46 PM] A ScriptHost error has occurred
[2/12/2019 9:30:46 PM] Microsoft.Azure.WebJobs.Script.WebHost: Unable to resolve function name 'My_Functions_DbCleanup_ExecuteCleanup_Run'.
[2/12/2019 9:30:45 PM] Executed 'My_Functions_DbCleanup_ExecuteCleanup_Run' (Failed, Id=498dbd0d-418b-486c-a000-4fdf51bffec2)
[2/12/2019 9:30:45 PM] Microsoft.Azure.WebJobs.Script.WebHost: Unable to resolve function name 'My_Functions_DbCleanup_ExecuteCleanup_Run'.
[2/12/2019 9:30:46 PM] Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '498dbd0d-418b-486c-a000-4fdf51bffec2'
[2/12/2019 9:30:46 PM] Microsoft.Azure.WebJobs.Script.WebHost: Unable to resolve function name 'My_Functions_DbCleanup_ExecuteCleanup_Run'.
If I change the Run() method to static then the function executes.
Nothing special required. What version of the Core Tools/Runtime are you using?
The project is .net 4.7.2, VS is up to date. Is there a specific component you would be looking for the version of?
I was clicking about in ILSpy before I left the office and trying to figure out what that func.exe and Microsoft.Azure.WebJobs.Script dll's want for the FunctionName attribute value. I tried just "Run" as that is what I think the FunctionShortName it derives when doing the lookup just before tossing that "unable to resolve" error. The problem is definitely looking like a name mismatch but its kinda hard to follow without debugging through parts of it. Is there any trace or diagnostic I can enable to dump out the function names it is able to resolve so I can try to appease it?
@fabiocav - I spent a few hours now trying different combinations and toying with this, but it does not seem to be possible to use non-static functions. The Microsoft.NET.Sdk.Functions package is v1.0.13
The namespace is My.Programs.AzFunctions, and the class name is FunctionExecutor and the method name is Run.
if I set the FunctionName attribute to FunctionName("Run") the func.exe output is
[2/14/2019 9:02:26 PM] Found the following functions:
[2/14/2019 9:02:26 PM] My.Programs.AzFunctions.FunctionExecutor.Run
[2/14/2019 9:02:26 PM]
[2/14/2019 9:02:26 PM] Host initialized (2263ms)
[2/14/2019 9:02:26 PM] A ScriptHost error has occurred
[2/14/2019 9:02:26 PM] Microsoft.Azure.WebJobs.Script.WebHost: Unable to resolve function name 'Run'.
[2/14/2019 9:02:26 PM] Executed 'Run' (Failed, Id=769581ef-8200-46b0-ac05-2f101ceca8e6)
[2/14/2019 9:02:26 PM] Microsoft.Azure.WebJobs.Script.WebHost: Unable to resolve function name 'Run'.
[2/14/2019 9:02:27 PM] Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '769581ef-8200-46b0-ac05-2f101ceca8e6'
[2/14/2019 9:02:27 PM] Microsoft.Azure.WebJobs.Script.WebHost: Unable to resolve function name 'Run'.
[2/14/2019 9:02:27 PM] The next 5 occurrences of the schedule will be:
If I use [FunctionName("FunctionExecutor")]
[2/14/2019 8:59:20 PM] Found the following functions:
[2/14/2019 8:59:20 PM] My.Programs.AzFunctions.FunctionExecutor.Run
[2/14/2019 8:59:20 PM]
[2/14/2019 8:59:20 PM] Host initialized (2082ms)
[2/14/2019 8:59:20 PM] A ScriptHost error has occurred
[2/14/2019 8:59:20 PM] Microsoft.Azure.WebJobs.Script.WebHost: Unable to resolve function name 'FunctionExecutor'.
[2/14/2019 8:59:20 PM] Executed 'FunctionExecutor' (Failed, Id=551a32f7-bc7f-48eb-bcb4-4fa4b75e5cdd)
[2/14/2019 8:59:20 PM] Microsoft.Azure.WebJobs.Script.WebHost: Unable to resolve function name 'FunctionExecutor'.
[2/14/2019 8:59:20 PM] Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '551a32f7-bc7f-48eb-bcb4-4fa4b75e5cdd'
[2/14/2019 8:59:20 PM] Microsoft.Azure.WebJobs.Script.WebHost: Unable to resolve function name 'FunctionExecutor'.
[2/14/2019 8:59:20 PM] The next 5 occurrences of the schedule will be:
When using [FunctionName("My.Programs.AzFunctions.FunctionExecutor.Run")] (to give it the name of the function it seems to be able to discover)
[2/14/2019 9:03:50 PM] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).
[2/14/2019 9:03:50 PM] Host initialized (1046ms)
[2/14/2019 9:03:50 PM] Host started (1114ms)
[2/14/2019 9:03:50 PM] Job host started
[2/14/2019 9:03:50 PM] The following 1 functions are in error:
[2/14/2019 9:03:50 PM] My.Programs.AzFunctions.FunctionExecutor.Run 'My.Programs.AzFunctions.FunctionExecutor.Run' is not a valid function name.
... so either the merge linked to this does not resolve the stated problem, or I dont have that merge compiled in whatever tooling is present.
Most helpful comment
Truly, statics make local mocking / IoC injection of unit tests all but impossible.