Azure / azure-webjobs-sdk

Azure WebJobs SDK
MIT License
733 stars 357 forks source link

IWebJobsStartup not triggered on netcore 2.1 but on 2.0 #2001

Open rasmuschristensen opened 5 years ago

rasmuschristensen commented 5 years ago

I Created a new function and because I make som configurations I'm using IWebJobsStartup. For some reason it's not called and I did the same in another function made some time ago. The only difference is the new one is based on net core 2.1 and the old on net core 2.0. I downgraded the new to 2.0 and now it also works. I searched around but could not find any changes regarding this issue, can someone help?

ncipollina commented 5 years ago

I have seen the same thing. Has startup changed from .net core 2.0 to .net core 2.1?

ncipollina commented 5 years ago

@rasmuschristensen, I think I've figured out why this isn't working for you. If you look at the code here, it loads the Startup classes foreach assembly. Since this project is a .Net Standard project, it can't load a .Net Core 2.1 application. Until this is upgraded, this won't work for you. You could do what I did, and create a project that is a .Net Standard project that has your Startup class and reference it from your function app. This will fix allow you to continue working on your function until this project is upgraded.

PureKrome commented 5 years ago

As suggested by @ncipollina ☝️ ...

not sure if this is a fix or not .. but .. I just changed the project type for my Function app from netcore2.1 (default setting) to netstandard20 ... which sounds insane because you shouldn't/can't run a NETStandard 'library' ... but .... magically it still runs and it now finds my Startup class which inherits IWebJobsStartup.

Original:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
.....

to

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard20</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
...
PureKrome commented 5 years ago

Any further information about this issue @fabiocav ? What should we be targetting/doing?

Here's my current .. stuff:

image

kamil-mrzyglod commented 5 years ago

I can confirm - I just created a fresh new project with func init and then had to change the target framework to netstandard20 to get the interface working.

tostringtheory commented 5 years ago

I'd love some guidance on this as well. I've tried setting the projects target framework to netstandard20 and netcoreapp2.0, which works locally in the emulator, but whenever I publish the function to the Azure app - it does not. I've distilled my registration to the IServiceCollection to just be a single Canary class to track if an object can be injected via the IServiceCollection. Locally - it gets registered/injected with it's initialized set to true (via the singleton registration). In Azure, the function fails to resolve the type.

Others here seem to have got around their issue by changing the target framework - and I haven't seen anyone mention issues with it working once published. Am I missing something?

tostringtheory commented 5 years ago

Turns out after pushing a little further, this was not the issue I was running into. In fact, I was finally able to distill my problem down to issue #4203 where 2.0.12353 runtime broke injection.

albertherd commented 5 years ago

Turns out after pushing a little further, this was not the issue I was running into. In fact, I was finally able to distill my problem down to issue #4203 where 2.0.12353 runtime broke injection.

Seconded. I'm facing the same issue. Switching the Azure Runtime from ~2 (keeps using latest) to 2.0.12342.0 solves it.

Link to original issue: https://github.com/Azure/azure-functions-host/issues/4203

This has now been solved here: https://github.com/Azure/azure-functions-host/pull/4228

Once the patch is live we can switch back.

maaex commented 5 years ago

Seconded. I'm facing the same issue. Switching the Azure Runtime from ~2 (keeps using latest) to 2.0.12342.0 solves it.

Thank you for posting this. I'm facing the same issue - I've been looking frenetically for a fix. The problems I've had was with dependency injection in Durable Functions, but turns out it was probably due to the Startup not running