-
Notifications
You must be signed in to change notification settings - Fork 189
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
.NET 8, Function Decorators Not Recognized #629
Comments
You need to add these usings instead: Best regards, |
.NET 8 doesn't support in-process worker as of now. More precisely, .NET 8 "delayed" supports the in-process worker. Therefore, you can't use .NET 8 for your in-process worker. Please refer to this page:
|
He is using .NET 8 isolated process:) -> "I am attempting to migrate an Azure Function from .NET 6 to .NET 8 and from in-process to isolated." |
I added specified usings, build is ok but http://localhost:7071/api/swagger/ui loading endless |
Oops! My bad! I should have read more carefully. @eat-sleep-code @remitonning Would you please be able to provide me with a sample repo so that I can repro on my end? |
@viktor11prykhidko doing that results in your aforementioned never-ending loading scenario. |
I have the exact same issue and was trying to figure out if the openapi nuget is compatible with aspnet integration, can't find any info on this topic.. Still wating on someone to confirm.. |
@eat-sleep-code This issue likely stems from the fact that the Azure Functions OpenAPI Extension hasn't been updated yet to fully support ASP.NET Core Integration in Azure Functions .NET isolated model. Swagger is trying to load the API definition using the isolated worker process port (dotnet.exe/55495) instead of using the webjobs host process port (func.exe/7071). Updating the port in Swagger UI to localhost |
@remitonning |
I was able to get it to work by doing the following in the configuration which isn't pretty but at least locally it now runs. services.AddSingleton(_ =>
{
var options = new OpenApiConfigurationOptions
{
// Your normal options
};
options.IncludeRequestingHostName = false;
options.Servers.Clear();
if (hostContext.Configuration["WEBSITE_HOSTNAME"].Contains("localhost", StringComparison.InvariantCultureIgnoreCase))
{
options.Servers.Add(new OpenApiServer()
{
Url = $"http://{hostContext.Configuration["WEBSITE_HOSTNAME"]}/api"
});
}
else
{
options.Servers.Add(new OpenApiServer()
{
Url = $"https://{hostContext.Configuration["WEBSITE_HOSTNAME"]}/api"
});
}
return options;
});``` |
For what it's worth to people, if you are adding the worker AspNetCore extensions, you can use a wrapped ForwardedHeadersMiddleware to patch the HttpRequest before the OpenApi function endpoint processes the request // Program.cs
var host = new HostBuilder()
.ConfigureFunctionsWebApplication(app =>
{
// get current request, and switch from http to https(request scheme), apply the options per request
app.UseMiddleware<FunctionApp1.Middleware.ForwardedHeadersMiddleware>();
})
.ConfigureServices(services =>
{
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
})
... // ForwardedHeadersMiddleware.cs
namespace FunctionApp1.Middleware
{
public class ForwardedHeadersMiddleware : Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersMiddleware, IFunctionsWorkerMiddleware
{
public ForwardedHeadersMiddleware(ILoggerFactory loggerFactory, IOptions<ForwardedHeadersOptions> options)
: base( ctx => Task.CompletedTask, loggerFactory, options)
{
}
public Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
var httpContext = context.GetHttpContext();
if (httpContext != default)
this.ApplyForwarders(httpContext);
return next(context);
}
}
} |
Hi @kshyju Best regards, |
Hi @eat-sleep-code and @davidpetric When you are using Azure Functions isolated (out-of-process) you should not use the using directives and not use HttpRequest in your function(endpoint) as parameter. Instead use HttpRequestData In you function you can than write for example: Se image below for an example. ...and make sure you have Best regards, |
Hi, thanks for getting back, The problem is I am using AspNetCore integration which mostly works with OpenAPI(I had opened an issue to have an official answer but no one from the team responded) The problem is that the Swagger will use a different PORT than the Azure function port for some reason... |
Take a look at what I did to get it to work above #629 (comment) |
@joeliir Where do you get the |
Why is that @remitonning? |
|
This is a program.cs that works. You can see where I'm getting the using System;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices((hostContext, services) => {
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddSingleton<IOpenApiConfigurationOptions>(_ =>
{
var options = new OpenApiConfigurationOptions
{
Info = new OpenApiInfo
{
Version = OpenApiVersionType.V3.ToString(),
Title = "",
Description = "",
TermsOfService = new Uri(""),
Contact = new OpenApiContact
{
Name = "",
Email = ""
}
},
Servers = DefaultOpenApiConfigurationOptions.GetHostNames(),
OpenApiVersion = OpenApiVersionType.V3,
IncludeRequestingHostName = DefaultOpenApiConfigurationOptions.IsFunctionsRuntimeEnvironmentDevelopment(),
ForceHttps = DefaultOpenApiConfigurationOptions.IsHttpsForced(),
ForceHttp = DefaultOpenApiConfigurationOptions.IsHttpForced(),
};
options.Servers.Clear();
options.Servers.Add(new OpenApiServer()
{
Url = $"http://{hostContext.Configuration["WEBSITE_HOSTNAME"]}/api"
});
if (hostContext.HostingEnvironment.IsDevelopment())
{
options.IncludeRequestingHostName = false;
}
return options;
});
}).Build();
host.Run(); |
Thanks to @joeliir for the example shared above! It worked for me locally, but it failed when published because the function in Azure was running under https. I first tried using using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Extensions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
var host = new HostBuilder()
.ConfigureFunctionsWebApplication(builder => builder.UseNewtonsoftJson())
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddSingleton<IOpenApiConfigurationOptions>(_ => new DefaultOpenApiConfigurationOptions
{
// The important parts:
IncludeRequestingHostName = false,
Servers = [new OpenApiServer { Url = "/api" }],
// Optional settings:
Info =
{
Version = "1.0.0", // Version of your API
Title = "",
Description = "",
Contact = new OpenApiContact
{
Name = "",
Email = ""
}
},
OpenApiVersion = OpenApiVersionType.V3
});
})
.Build();
host.Run(); Note that it's possible there are downsides to using a relative URL ( |
@viktor11prykhidko @joeliir @eat-sleep-code |
I think this was solved by PR Azure/azure-functions-dotnet-worker#2149 for this issue Azure/azure-functions-dotnet-worker#2071 |
I can't follow this thread. Did we indeed determine if these libraries are the correct ones to reference to use the OpenApi attributes? using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes; Because, as mentioned before, the documentation to migrate to the isolated model from in-proc clearly states in multiple places that references to the Microsoft.Azure.WebJobs.* namespaces should be removed. |
Those two |
Hi, I'm currently integrating Azure Functions with Swagger. I've successfully done this for .NET 6.0, and now I'm exploring a proof of concept (POC) using .NET 8.0. Could someone please provide a step-by-step guide or share documentation and sample code that demonstrates the integration of Swagger with Azure Functions in .NET 8? It would be greatly appreciated. |
I am attempting to migrate an Azure Function from .NET 6 to .NET 8 and from in-process to isolated.
I followed the instructions outlined here: https://github.com/Azure/azure-functions-openapi-extension/blob/main/docs/enable-open-api-endpoints-out-of-proc.md
Including updating the the csproj and performing
dotnet restore
:However, it appears that the decorators are still not recognized?
Visual Studio Code indicates: 'OpenApiOperation' is not an attribute class
I have tried
dotnet clean
, I have tried completely exiting VS Code and restarting.The text was updated successfully, but these errors were encountered: