BorisWilhelms / azure-function-dependency-injection

Dependency Injection for Azure Functions v2
https://blog.wille-zone.de/
MIT License
145 stars 45 forks source link

Not working with TimerTrigger functions #31

Closed richardhauer closed 5 years ago

richardhauer commented 5 years ago

Somehow the Inject attribute isn't binding for a function that is triggered by a TimerTrigger - I get an error saying the runtime cannot bind the parameter.

I will fork the code and try to find the issue.

tmoon8730 commented 5 years ago

Checkout #17 I think it may be a similar issue with it not registering the extension correctly.

richardhauer commented 5 years ago

Checkout #17 I think it may be a similar issue with it not registering the extension correctly.

Unfortunately, that's probably not the case. The Inject attribute works perfectly on functions that have Message Queue and HTTP triggers (that I've tried), but raises a binding error against functions with Timer bindings. It's not clear what's different about the process of binding these parameters when compared with HTTP triggered functions, but I wouldn't rule out a timing issue.

At this stage I've resorted to a Service Locator pattern as a workaround for Timer triggered functions, but I'm not super-happy with that due to the inherent problems with testability. I prefer to code TDD-style so crafting a test that necessitates the creation of the ServiceLocator is a pain, and exposes a writable dependency as a property that really shouldn't be there.

BorisWilhelms commented 5 years ago

@richardhauer is this issue still relevant? Do you have a repo? I have tested it localy with a 5 second timer and it works for me.

[FunctionName("TimerGreeter")]
public static void RunTimerFunction(
    [TimerTrigger("0/5 * * * * *")]TimerInfo myTimer,
    [Inject]LoggingGreeter greeter)
{
    greeter.Greet();
}
richardhauer commented 5 years ago

Will update and re-check.

I had a function with an Inject that was always null. An HTTP triggered function right beside it worked fine.

Could it be that the issue only happens when there’s both in the same project? Doesn’t seem like it would be related but you never know.

I will also report back the platform library versions.

Richard Hauer CTO

PING Works 1-7 Albion Place, Sydney NSW 2000 p: +61 2 9321 1551<tel:+61%202%209321%201551> | www.ping-works.com.auhttps://www.ping-works.com.au d: +61 2 9321 1556<tel:+61%202%209321%201556> | m: +61 414 189 170<tel:+61%20414%20189%20170> Download a vCardhttps://ping-vcards.azurewebsites.net/api/GenerateVCF?name=richard.hauer

On 2 Mar 2019, at 3:38 am, Boris Wilhelms notifications@github.com<mailto:notifications@github.com> wrote:

@richardhauerhttps://github.com/richardhauer is this issue still relevant? Do you have a repo? I have tested it localy with a 5 second timer and it works for me.

[FunctionName("TimerGreeter")] public static void RunTimerFunction( [TimerTrigger("0/5 *")]TimerInfo myTimer, [Inject]LoggingGreeter greeter) { greeter.Greet(); }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/BorisWilhelms/azure-function-dependency-injection/issues/31#issuecomment-468727623, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AANyD30ukaJKJ0weTCDINzKHLwbOq6B2ks5vSVeIgaJpZM4aNtDv.

BorisWilhelms commented 5 years ago

In my test I had a HttpTrigger function in the same class, so this should not be the issue.

zarusz commented 5 years ago

I also confirm that dependency injection in the a time trigger function V2 works. Here is more info: https://github.com/Azure/azure-functions-host/issues/3386#issuecomment-468182846