Skip to content

BUG: Object reference not set.... .net 6 functions #352

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

Open
svrooij opened this issue Feb 1, 2022 · 16 comments
Open

BUG: Object reference not set.... .net 6 functions #352

svrooij opened this issue Feb 1, 2022 · 16 comments
Labels
bug Something isn't working Needs: Triage (Functions)

Comments

@svrooij
Copy link

svrooij commented Feb 1, 2022

Describe the issue
When using this library in a new Azure Functions project (.net6, runtime V4) it works locally but not on Azure.

Object reference not set to an instance of an object.

To Reproduce

  1. Create new Azure functions project with .Net 6
  2. Add a new project without dependencies and reference it in your functions project
  3. Deploy to Azure
  4. Call /api/swagger.json

Expected behavior
I expect that swagger would just work like it does locally.

Screenshots

image

Environment (please complete the following information, if applicable):

  • OS: Azure (Windows Service plan, FUNCTIONS_EXTENSION_VERSION = ~4, FUNCTIONS_WORKER_RUNTIME = dotnet)
  • Version 1.0.0 (nuget)
@vinhent
Copy link

vinhent commented Feb 1, 2022

I am having the exact same issue (.net6, runtime V4). It works just fine locally but not on Azure.

@PedroLucasSilva
Copy link

I had the same problem and I solved it based on an old issue. Where the person indicated that all projects on their system must have at least one dependency. I added a random package to a project of mine that didn't have any and it started working.

@McNerdius
Copy link

Bizarre issue. The "at least one dependency per project" workaround worked for me as well.

@vinhent
Copy link

vinhent commented Feb 9, 2022

It fixed it for me as well. I had a project that contains only interfaces and didn't have any dependencies.

I added this randomly:

image

Do you have an idea of a better nuget package to be used in that context?

@svrooij
Copy link
Author

svrooij commented Feb 9, 2022

I can confirm that this hack fixes my problem, but I think the need for at least one dependency in each project, should be treated as a BUG. Because it really is a strange thing to require.

I just used this package because I was already using the dependency injection anyways.

  <ItemGroup>
    <!-- Hack to fix OpenApi UI and Json in Azure, requiring at least one dependency for each project. -->
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
  </ItemGroup>

@svrooij
Copy link
Author

svrooij commented Feb 9, 2022

I had the same problem and I solved it based on an old issue. Where the person indicated that all projects on their system must have at least one dependency. I added a random package to a project of mine that didn't have any and it started working.

@PedroLucasSilva do you maybe have the link to that other issue, seems like we need to reference it here.

@justinyoo
Copy link
Contributor

@svrooij It's a known issue #302

@justinyoo justinyoo added resolved bug Something isn't working labels Feb 14, 2022
@svrooij
Copy link
Author

svrooij commented Feb 14, 2022

@justinyoo But why does it work locally?

@justinyoo
Copy link
Contributor

@svrooij I have no idea. I haven't got such an experience at all. I use exactly the same settings (.NET 6, v4, in-proc). Would you please tell me how to reproduce the issue?

@svrooij
Copy link
Author

svrooij commented Feb 14, 2022

Since the comments above provided a work-around, this is what triggers the error:

  1. Create a new azure Functions app with OpenApi support
  2. Add an additional project, WITHOUT dependencies.
  3. Add the second project as a project dependency to the functions project.
  4. The swagger ui should still appear locally, and the endpoint from the template should be in the swagger ui.
  5. Publish to Azure and it stops working, with the error above in the ligging.

@justinyoo
Copy link
Contributor

@svrooij Alright. I tried to reproduce on my end, but... my app is still working. Here's my app structure:

https://github.com/fitability/fitability-api/tree/main/users

  • Api.Users ➡️ main function app - .NET 6, in-proc, v4
  • Api.Models ➡️ class library with no dependency, but added to Api.Users - .NET Standard 2.0
  • Api.Users.Models ➡️ class library with no dependency, no added to Api.Users - .NET Standard 2.0

They are buit and deployed to Azure through GitHub Actions, and it's working.

@chris-parker-bliss
Copy link

chris-parker-bliss commented Feb 18, 2022

I wanted to make a quick note to say that we've just run into this issue when running locally - in our setup we've got an Azure function project referencing a "shared" net6.0 class library.

Using the hack suggested by @svrooij - adding the Microsoft.Extensions.DependencyInjection.Abstractions dependency to that shared project - has gotten it working again.

@justinyoo
Copy link
Contributor

justinyoo commented Mar 30, 2022

@svrooij Alright. I tried to reproduce on my end, but... my app is still working. Here's my app structure:

https://github.com/fitability/fitability-api/tree/main/users

  • Api.Users ➡️ main function app - .NET 6, in-proc, v4
  • Api.Models ➡️ class library with no dependency, but added to Api.Users - .NET Standard 2.0
  • Api.Users.Models ➡️ class library with no dependency, no added to Api.Users - .NET Standard 2.0

They are buit and deployed to Azure through GitHub Actions, and it's working.

I changed the Api.Models project targeting to net6.0

https://github.com/fitability/fitability-api/blob/27216269c6b6e684c957abbbbde27e0640b3f947/users/src/Fitability.Api.Models/Fitability.Api.Models.csproj#L3-L6

and updated the package version to v1.3.0.

https://github.com/fitability/fitability-api/blob/27216269c6b6e684c957abbbbde27e0640b3f947/users/src/Fitability.Api.Users/Fitability.Api.Users.csproj#L6-L9

It works OK both locally and Azure on my end.

@svrooij @chris-parker-bliss Would you please try on your end?

@justinyoo justinyoo added Needs: Triage (Functions) bug Something isn't working and removed resolved bug Something isn't working labels Mar 30, 2022
@svrooij
Copy link
Author

svrooij commented Mar 30, 2022

@justinyoo it happens when there are no dependencies in the class library and it is set to .NET6 as target framework.

image

@justinyoo
Copy link
Contributor

That's exactly what I did. The Api.Models project is a class library targeting net6.0 with no dependency.

@chris-parker-bliss
Copy link

Hi @justinyoo - I can confirm that even after updating to v1.3.0 of the Microsoft.Azure.WebJobs.Extensions.OpenApi package I'm still seeing the following error when my function app references a net6.0 class library which has no dependencies:

Object reference not set to an instance of an object.

   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiHttpTriggerContext.<>c.<GetRuntimeFilenameAsync>b__43_1(KeyValuePair`2 target)
   at System.Linq.Utilities.<>c__DisplayClass2_0`3.<CombineSelectors>b__0(TSource x)
   at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value, IEqualityComparer`1 comparer)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiHttpTriggerContext.<>c__DisplayClass43_0.<GetRuntimeFilenameAsync>b__3(<>f__AnonymousType0`3 r)
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiHttpTriggerContext.GetRuntimeFilenameAsync(String functionAppDirectory)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiHttpTriggerContext.SetApplicationAssemblyAsync(String functionAppDirectory, Boolean appendBin)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions.OpenApiHttpTriggerContextExtensions.AuthorizeAsync(Task`1 context, IHttpRequestDataObject req)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiTriggerFunctionProvider.RenderSwaggerUI(HttpRequest req, ExecutionContext ctx, ILogger log)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Needs: Triage (Functions)
Projects
None yet
Development

No branches or pull requests

6 participants