Azure / azure-functions-openapi-extension

This extension provides an Azure Functions app with Open API capability for better discoverability to consuming parties
https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.OpenApi/
MIT License
370 stars 194 forks source link

OpenAPI extension does not work when using EF Core >= 3.0 #173

Open mogrodniczak opened 3 years ago

mogrodniczak commented 3 years ago

Hi all,

Today I faced an issue when I started to use EF Core in my function app.

Error:

Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=3.1.16.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

   at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
   at System.Reflection.RuntimeMethodInfo.GetParameters()
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.DocumentHelper.<>c.<GetHttpTriggerMethods>b__3_4(MethodInfo p)
   at System.Linq.Utilities.<>c__DisplayClass1_0`1.<CombinePredicates>b__0(TSource x)
   at System.Linq.Enumerable.WhereEnumerableIterator`1.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.DocumentHelper.GetHttpTriggerMethods(Assembly assembly)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Document.Build(Assembly assembly, OpenApiVersionType version)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiTriggerFunctionProvider.RenderOpenApiDocument(HttpRequest req, String version, String extension, ExecutionContext ctx, ILogger log)

Steps to reproduce:

  1. Create brand new Function App project in VS - .NET Core 3.1 / Function App v3
  2. Install OpenAPI Extension
  3. Run function app
  4. Open http://localhost:7071/api/openapi/1.0 in the web browser - it does work
  5. Install EF Core 3.1.6
  6. Run function app
  7. Open http://localhost:7071/api/openapi/1.0 in the web browser - returns an error

so EF Core 3.1.16 requires Microsoft.Extensions.Logging.Abstractions >= 3.1.16

It seems that the OpenAPI extension can work only when it is < 3.0.

Do you have any insight into it?

Skovvart commented 3 years ago

Same issue seems to occur with other extensions such as <PackageReference Include="Microsoft.Extensions.Http" Version="3.1.15" /> that also has a dependency to Microsoft.Extensions.Logging.Abstractions.

(as long as you are injecting ILogger in your HttpTrigger functions).

A workaround seems to be using constructor-based injection for ILogger instead (though this is likely dependent on using IoC based functions)

mogrodniczak commented 3 years ago

Hi @Skovvart, thank you for your suggestion. Your workaround works well If you are not using the OpenApiSecurity attribute. If I have it in my function decoration I get this error:


Exception has been thrown by the target of an invocation.

   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& hasNoDefaultCtor)
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.DocumentHelper.GetSecurityOAuthFlows(OpenApiSecurityAttribute attr)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.DocumentHelper.GetOpenApiSecurityRequirement(MethodInfo element, NamingStrategy namingStrategy)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Document.Build(Assembly assembly, OpenApiVersionType version)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiTriggerFunctionProvider.RenderOpenApiDocument(HttpRequest req, String version, String extension, ExecutionContext ctx, ILogger log)```
Skovvart commented 3 years ago

I am guessing that the the above is a separate issue, where your OpenApiSecurityAttribute implementation lacks a parameterless constructor (see #168 for some more context).

tkhadimullin commented 3 years ago

Not sure if this is still relevant, I was able to work around this issue by downgrading EF Core to 3.1.13

DanielHead commented 2 years ago

I'm facing the same issues, could it be related to #281?