Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Includes:
Hosting integration not
Client integration
Note
This integration is part of the .NET Aspire Community Toolkit and isn't officially supported by the .NET Aspire team.
In this article, you learn about the .NET Aspire Community Toolkit Python hosting extensions package which provides extra functionality to the .NET Aspire Python hosting package. The extensions package lets you run Uvicorn applications.
Hosting integration
To get started with the .NET Aspire Community Toolkit Python hosting extensions, install the 📦 CommunityToolkit.Aspire.Hosting.Python.Extensions NuGet package in the AppHost project.
dotnet add package CommunityToolkit.Aspire.Hosting.Python.Extensions
For more information, see dotnet add package or Manage package dependencies in .NET applications.
Example usage
To work with Python apps, they need to be within a virtual environment. To create a virtual environment, refer to the Initialize the Python virtual environment section.
In the Program.cs file of your app host project, call the AddUvicornApp
method to add a Uvicorn application to the builder.
var builder = DistributedApplication.CreateBuilder(args);
var uvicorn = builder.AddUvicornApp(
name: "uvicornapp",
projectDirectory: "../uvicornapp-api",
appName: "main:app"
)
.WithHttpEndpoint(env: "PORT");
builder.Build().Run();
The PORT
environment variable is used to determine the port the Uvicorn application should listen on. By default, this port is randomly assigned by .NET Aspire. The name of the environment variable can be changed by passing a different value to the WithHttpEndpoint method.
The Uvicorn application can be added as a reference to other resources in the AppHost project.
See also
.NET Aspire