Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FunctionsStartup is not being called, causing function dependency resolution to fail. #5

Closed
worleywa7 opened this issue May 8, 2019 · 27 comments

Comments

@worleywa7
Copy link

We downloaded the sample project, put a break point on builder.Services.AddScoped<IGreeter, SampleGreeter>(); expecting it to be called when the runtime started and discovered functions. Unfortunately it did no, so we attempted to invoke the function directly to see if the runtime would call the FunctionStartup class upon first function invocation. Unfortunately it did not and both times resulted in:

Executed 'SampleFunction' (Failed, Id=eeaabc43-d8a6-4815-9fe2-0834f7d4c47b)
[5/8/2019 9:58:22 PM] Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'Microsoft.Azure.Functions.Samples.DependencyInjectionBasic.IGreeter' while attempting to activate 'Microsoft.Azure.Functions.Samples.DependencyInjectionBasic.SampleFunction'.

Is there something we are missing? It does not appear that the FunctionsStartup method is actually being called by the runtime.

@bentmar
Copy link

bentmar commented May 9, 2019

#8
heres the fix

@worleywa7
Copy link
Author

worleywa7 commented May 9, 2019

Thank you very much for your time, your solution did the trick and we got it working!

@bentmar
Copy link

bentmar commented May 15, 2019

@worleywa7 Just curious, have you tried publishing a function with DI to azure?

Mine works locally, but crashes instantly in the cloud with a DependencyIncetion error..

@bentmar
Copy link

bentmar commented May 16, 2019

Okay, let the randomness begin..

1.0.27 and 1.0.28 doesnt work in azure for dependency resolution, BUT 1.026 does.

And when downgradig to 1.0.26 it suddenly works locally aswell!

@worleywa7 worleywa7 reopened this May 16, 2019
@worleywa7
Copy link
Author

Looks like we're getting the same issue as well; wasn't readily apparent until now. We're going to downgrade as well; see what we come up with.

@worleywa7
Copy link
Author

worleywa7 commented May 16, 2019

I downgraded back to 1.0.26 and things returned to throwing the DI errors...I'm going to try 1.0.28.

I take it back, not trying 1.0.28.

@worleywa7
Copy link
Author

worleywa7 commented May 16, 2019

Looks like we're back to the drawing board:

'The function runtime is unable to start. Functions: Method not found: 'Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder.get_Services()'.
Session Id: ~~~~~

Timestamp: 2019-05-16T15:13:38.648Z'

@worleywa7
Copy link
Author

So we did what you said at first, and it worked; that's why I closed the ticket.. I saw exactly what I wanted to see in my project. However, since then, I added nugets from two .NET Standard libraries, injected a dependency from one of those libraries, and have created functions that rely on this DIs.

@antichaosdb
Copy link

I tried the scopes sample - no other references. As provided with 1.0.26 it failed locally and on Azure. With Dependencies updated (1.0.28) it runs locally but when deployed to Azure I get :
Unable to resolve service for type 'Microsoft.Azure.Functions.Samples.DependencyInjectionScopes.MyServiceA' while attempting to activate 'Microsoft.Azure.Functions.Samples.DependencyInjectionScopes.SampleFunction'.

Is there a way to do some logging in the startup function so we can see if it being run when deployed?

@worleywa7
Copy link
Author

So what it ended up being was, for me at least, there was a dependency that I forgot to inject. There was a single "Object cannot be null" line in a log on the azure portal that seemed to be the issue. We're back to running our functions on 1.0.28 in the Cloud.

@jedjohan
Copy link

The only way to get DI to work for me when deployed to Azure is using 1.0.26.

1.0.26, 1.0.27, 1.0.28 all works fine local

@eliashdezr
Copy link

1.0.26 doesn't work locally for me, I'm testing it with the sample project.

@antichaosdb
Copy link

Finally got this working. According to [(https://github.com/Azure/azure-functions-host/issues/3386#issuecomment-497492442)] the suggestion to add a reference to ExtensionsMetadataGenerator is bad. Remove this reference if you have it.

@ErikAndreas
Copy link

1.0.29 not working, same issue as described in #14 (closed due to author moving on to Aws Lambda...)

Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type '...' while attempting to activate '...'

@itorian
Copy link

itorian commented Oct 17, 2019

That's totally fine for me on development and production when I create new project, but same error when i try to add DI in existing project/solution.

So why this error in existing project/solution, that's discussed here dotnet/extensions#1718

Here's my working project configurations:

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
  </ItemGroup>

Notice i'm using, function v2, netcoreapp3.0, Sdk.Function 1.0.29 and Function.Extension 1.0.0.

Sample project here https://github.com/itorian/AzureFunctionDISample, if you running this sample please update AzureWebJobsStorage in local.settings.json or use emulator "AzureWebJobsStorage": "UseDevelopmentStorage=true"

image

@apavelm
Copy link

apavelm commented Oct 17, 2019

Same issue for me

Method not found: 'Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder.get_Services()'.

@robertracz
Copy link

Same here... Happening when I reference

<PackageReference Include="Microsoft.Extensions.Http" Version="3.0.0" />

@dacris
Copy link

dacris commented Oct 24, 2019

I had the same issue today. I removed some Nuget packages and added a namespace declaration to my Startup.cs and the problem was fixed. Using SDK 1.0.29. Not sure if this matters, but check if your Startup class has a namespace.

@apavelm
Copy link

apavelm commented Oct 24, 2019

At this moment the solution is only to downgrade to .NET Core 2.2

@eliashdezr
Copy link

I'm on the same boat, these combinations of NuGet versions are not working:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AzureFunctionsVersion>v3-preview</AzureFunctionsVersion>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.0-preview2" />
    <PackageReference Include="Utf8Json" Version="1.3.7" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Error is
Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type

@arkiaconsulting
Copy link

arkiaconsulting commented Dec 5, 2019

<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.0" />

is out.

Still

Microsoft.Azure.Functions.Extensions: Method not found: 'Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Azure.WebJobs.IWebJobsBuilder.get_Services()'.

EDIT: I got rid of the exception above by downgrading all Microsoft.Extensions.* to 3.0.1

@eliashdezr
Copy link

Is the runtime version still on preview?

https://docs.microsoft.com/en-us/azure/azure-functions/functions-versions

@arkiaconsulting
Copy link

Tested locally with v3 instead of v3-preview with no success (No function runtime available)

@eliashdezr
Copy link

eliashdezr commented Dec 5, 2019

And this error still persist:
Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type

@coolhome
Copy link

coolhome commented Dec 6, 2019

I had issues when using preview without having visual studio 16.4 preview installed & preview environment variable enabled. Maybe try installing visual studio 16.4 with the official release nuget?

old ref: https://dev.to/azure/develop-azure-functions-using-net-core-3-0-gcm

Here is my sample csproj that I had it working with locally during preview.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AzureFunctionsVersion>v3-preview</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.5" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SignalRService" Version="1.0.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.10" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.0-preview1" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
  </ItemGroup>
</Project>

@wtt235-bh
Copy link

This seems like it could also be related to Azure/azure-functions-host#5401.

@fabiocav
Copy link
Member

fabiocav commented Jan 8, 2020

Closing this issue as there is a mix of different problems tracked here, and they should be resolved with the release of Azure Functions 3.0 (many of the issues here are related to running on Azure Functions 2.0 while targeting .NET Core 3.0).

If you continue to have issues, please open a new issue with details and we'll investigate them. Thank you!

@fabiocav fabiocav closed this as completed Jan 8, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Feb 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests