Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Aspire.sln
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DatabaseMigration.Migration
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DatabaseMigration.ApiModel", "playground\DatabaseMigration\DatabaseMigration.ApiModel\DatabaseMigration.ApiModel.csproj", "{C15F3F13-AB63-47CF-AAFE-D319F02E7B33}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ProxylessEndToEnd", "ProxylessEndToEnd", "{9C30FFD6-2262-45E7-B010-24B30E0433C2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxylessEndToEnd.ApiService", "playground\ProxylessEndToEnd\ProxylessEndToEnd.ApiService\ProxylessEndToEnd.ApiService.csproj", "{51654CD7-2E05-4664-B2EB-95308A300609}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxylessEndToEnd.AppHost", "playground\ProxylessEndToEnd\ProxylessEndToEnd.AppHost\ProxylessEndToEnd.AppHost.csproj", "{0244203D-7491-4414-9C88-10BFED9C5B2D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -707,6 +713,14 @@ Global
{C15F3F13-AB63-47CF-AAFE-D319F02E7B33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C15F3F13-AB63-47CF-AAFE-D319F02E7B33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C15F3F13-AB63-47CF-AAFE-D319F02E7B33}.Release|Any CPU.Build.0 = Release|Any CPU
{51654CD7-2E05-4664-B2EB-95308A300609}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{51654CD7-2E05-4664-B2EB-95308A300609}.Debug|Any CPU.Build.0 = Debug|Any CPU
{51654CD7-2E05-4664-B2EB-95308A300609}.Release|Any CPU.ActiveCfg = Release|Any CPU
{51654CD7-2E05-4664-B2EB-95308A300609}.Release|Any CPU.Build.0 = Release|Any CPU
{0244203D-7491-4414-9C88-10BFED9C5B2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0244203D-7491-4414-9C88-10BFED9C5B2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0244203D-7491-4414-9C88-10BFED9C5B2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0244203D-7491-4414-9C88-10BFED9C5B2D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -835,6 +849,9 @@ Global
{E5C93F8B-D31B-4268-89EB-830EDC5524D0} = {8B1802BC-6CB0-4027-850C-2AED42A82C9E}
{E7DB736B-C316-460E-A609-2200E58BF0C2} = {8B1802BC-6CB0-4027-850C-2AED42A82C9E}
{C15F3F13-AB63-47CF-AAFE-D319F02E7B33} = {8B1802BC-6CB0-4027-850C-2AED42A82C9E}
{9C30FFD6-2262-45E7-B010-24B30E0433C2} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
{51654CD7-2E05-4664-B2EB-95308A300609} = {9C30FFD6-2262-45E7-B010-24B30E0433C2}
{0244203D-7491-4414-9C88-10BFED9C5B2D} = {9C30FFD6-2262-45E7-B010-24B30E0433C2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6DCEDFEC-988E-4CB3-B45B-191EB5086E0C}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using StackExchange.Redis;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

builder.AddKeyedRedis("redis");

var app = builder.Build();

app.MapGet("/", () =>
{
return Random.Shared.Next();
});

app.MapGet("/redis", ([FromKeyedServices("redis")] IConnectionMultiplexer redis) =>
{
var db = redis.GetDatabase();
db.StringAppend("key", "a");
return (string?)db.StringGet("key");
});

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:12345",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Playground.ServiceDefaults\Playground.ServiceDefaults.csproj" />
<ProjectReference Include="..\..\..\src\Components\Aspire.StackExchange.Redis\Aspire.StackExchange.Redis.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<!-- NOTE: This line is only required because we are using P2P references, not NuGet. It will not exist in real apps. -->
<Import Project="../../../src/Aspire.Hosting/build/Aspire.Hosting.props" />

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" />

<!-- NOTE: These lines are only required because we are using P2P references, not NuGet. They will not exist in real apps. -->
<Import Project="..\..\..\src\Aspire.Hosting\build\Aspire.Hosting.targets" />
<Import Project="..\..\..\src\Aspire.Hosting.Sdk\SDK\Sdk.targets" />

</Project>
27 changes: 27 additions & 0 deletions playground/ProxylessEndToEnd/ProxylessEndToEnd.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

var builder = DistributedApplication.CreateBuilder(args);

var redis = builder.AddRedis("redis", 9999).WithEndpoint("tcp", (endpoint) =>
{
endpoint.IsProxied = false;
});

builder.AddProject<Projects.ProxylessEndToEnd_ApiService>("api")
.WithEndpoint(12345, "http", isProxied: false)
.WithReference(redis);

builder.AddProject<Projects.ProxylessEndToEnd_ApiService>("api2")
.ExcludeLaunchProfile()
.WithEndpoint(13456, "http")
.WithReference(redis);

// This project is only added in playground projects to support development/debugging
// of the dashboard. It is not required in end developer code. Comment out this code
// to test end developer dashboard launch experience. Refer to Directory.Build.props
// for the path to the dashboard binary (defaults to the Aspire.Dashboard bin output
// in the artifacts dir).
builder.AddProject<Projects.Aspire_Dashboard>(KnownResourceNames.AspireDashboard);

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15888",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16175",
"DOTNET_ASPIRE_SHOW_DASHBOARD_RESOURCES": "true"
}
},
"generate-manifest": {
"commandName": "Project",
"launchBrowser": true,
"dotnetRunMessages": true,
"commandLineArgs": "--publisher manifest --output-path aspire-manifest.json",
"applicationUrl": "http://localhost:15888",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16175"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(SharedDir)KnownResourceNames.cs" Link="KnownResourceNames.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Aspire.Dashboard\Aspire.Dashboard.csproj" />
<ProjectReference Include="..\..\..\src\Aspire.Hosting\Aspire.Hosting.csproj" IsAspireProjectResource="false" />

<ProjectReference Include="..\ProxylessEndToEnd.ApiService\ProxylessEndToEnd.ApiService.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
11 changes: 10 additions & 1 deletion src/Aspire.Hosting/ApplicationModel/EndpointAnnotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public sealed class EndpointAnnotation : IResourceAnnotation
/// <param name="containerPort">If the endpoint is used for the container, this is the port the container process is listening on.</param>
/// <param name="isExternal">Indicates that this endpoint should be exposed externally at publish time.</param>
/// <param name="env">The name of the environment variable that will be set to the port number of this endpoint.</param>
public EndpointAnnotation(ProtocolType protocol, string? uriScheme = null, string? transport = null, string? name = null, int? port = null, int? containerPort = null, bool? isExternal = null, string? env = null)
/// <param name="isProxied">Specifies if the endpoint will be proxied by DCP. Defaults to true.</param>
public EndpointAnnotation(ProtocolType protocol, string? uriScheme = null, string? transport = null, string? name = null, int? port = null, int? containerPort = null, bool? isExternal = null, string? env = null, bool isProxied = true)
{
// If the URI scheme is null, we'll adopt either udp:// or tcp:// based on the
// protocol. If the name is null, we'll use the URI scheme as the default. This
Expand All @@ -46,6 +47,7 @@ public EndpointAnnotation(ProtocolType protocol, string? uriScheme = null, strin
ContainerPort = containerPort ?? port;
IsExternal = isExternal ?? false;
EnvironmentVariable = env;
IsProxied = isProxied;
}

/// <summary>
Expand Down Expand Up @@ -90,4 +92,11 @@ public EndpointAnnotation(ProtocolType protocol, string? uriScheme = null, strin
/// The name of the environment variable that will be set to the port number of this endpoint.
/// </summary>
public string? EnvironmentVariable { get; set; }

/// <summary>
/// Indicates that this endpoint should be managed by DCP. This means it can be replicated and use a different port internally than the one publicly exposed.
/// Setting to false means the endpoint will be handled and exposed by the resource.
/// </summary>
/// <remarks>Defaults to <c>true</c>.</remarks>
public bool IsProxied { get; set; } = true;
}
33 changes: 28 additions & 5 deletions src/Aspire.Hosting/Dcp/ApplicationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private async Task CreateServicesAsync(CancellationToken cancellationToken = def
var srvResource = needAddressAllocated.Where(sr => sr.Service.Metadata.Name == updated.Metadata.Name).FirstOrDefault();
if (srvResource == null) { continue; } // This service most likely already has full address information, so it is not on needAddressAllocated list.

if (updated.HasCompleteAddress)
if (updated.HasCompleteAddress || updated.Spec.AddressAllocationMode == AddressAllocationModes.Proxyless)
{
srvResource.Service.ApplyAddressInfoFrom(updated);
needAddressAllocated.Remove(srvResource);
Expand Down Expand Up @@ -311,16 +311,22 @@ private static void AddAllocatedEndpointInfo(IEnumerable<AppResource> resources)
foreach (var sp in appResource.ServicesProduced)
{
var svc = (Service)sp.DcpResource;
if (!svc.HasCompleteAddress)

if (!svc.HasCompleteAddress && sp.EndpointAnnotation.IsProxied)
{
// This should never happen; if it does, we have a bug without a workaround for th the user.
throw new InvalidDataException($"Service {svc.Metadata.Name} should have valid address at this point");
}

if (!sp.EndpointAnnotation.IsProxied && svc.AllocatedPort is null)
{
throw new InvalidOperationException($"Service '{svc.Metadata.Name}' needs to specify a port for endpoint '{sp.EndpointAnnotation.Name}' since it isn't using a proxy.");
}

var a = new AllocatedEndpointAnnotation(
sp.EndpointAnnotation.Name,
PortProtocol.ToProtocolType(svc.Spec.Protocol),
svc.AllocatedAddress!,
sp.EndpointAnnotation.IsProxied ? svc.AllocatedAddress! : "localhost",
(int)svc.AllocatedPort!,
sp.EndpointAnnotation.UriScheme
);
Expand All @@ -344,7 +350,7 @@ void addServiceAppResource(Service svc, IResource producingResource, EndpointAnn
{
svc.Spec.Protocol = PortProtocol.FromProtocolType(sba.Protocol);
svc.Annotate(CustomResource.UriSchemeAnnotation, sba.UriScheme);
svc.Spec.AddressAllocationMode = AddressAllocationModes.Localhost;
svc.Spec.AddressAllocationMode = sba.IsProxied ? AddressAllocationModes.Localhost : AddressAllocationModes.Proxyless;
_appResources.Add(new ServiceAppResource(producingResource, svc, sba));
}

Expand Down Expand Up @@ -750,6 +756,12 @@ private async Task CreateContainersAsync(IEnumerable<AppResource> containerResou
ContainerPort = sp.DcpServiceProducerAnnotation.Port,
};

if (!sp.EndpointAnnotation.IsProxied)
{
// When DCP isn't proxying the container we need to set the host port that the containers internal port will be mapped to
portSpec.HostPort = sp.EndpointAnnotation.Port;
}

if (!string.IsNullOrEmpty(sp.DcpServiceProducerAnnotation.Address))
{
portSpec.HostIP = sp.DcpServiceProducerAnnotation.Address;
Expand Down Expand Up @@ -825,7 +837,7 @@ private void AddServicesProducedInfo(IResource modelResource, IAnnotationHolder
var servicesProduced = _appResources.OfType<ServiceAppResource>().Where(r => r.ModelResource == modelResource);
foreach (var sp in servicesProduced)
{
// Projects/Executables have their ports auto-allocated; the the port specified by the EndpointAnnotation
// Projects/Executables have their ports auto-allocated; the port specified by the EndpointAnnotation
// is applied to the Service objects and used by clients.
// Containers use the port from the EndpointAnnotation directly.

Expand All @@ -838,6 +850,17 @@ private void AddServicesProducedInfo(IResource modelResource, IAnnotationHolder

sp.DcpServiceProducerAnnotation.Port = sp.EndpointAnnotation.ContainerPort;
}
else if (!sp.EndpointAnnotation.IsProxied)
{
if (appResource.DcpResource is ExecutableReplicaSet ers && ers.Spec.Replicas > 1)
{
throw new InvalidOperationException($"'{modelResourceName}' specifies multiple replicas and at least one proxyless endpoint. These features do not work together.");
}

// DCP will not allocate a port for this proxyless service
// so we need to specify what the port is so DCP is aware of it.
sp.DcpServiceProducerAnnotation.Port = sp.EndpointAnnotation.Port;
}

dcpResource.AnnotateAsObjectList(CustomResource.ServiceProducerAnnotation, sp.DcpServiceProducerAnnotation);
appResource.ServicesProduced.Add(sp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ private static void PrepareServices(DistributedApplicationModel model)
{
var uri = new Uri(url);

if (projectResource.Annotations.OfType<EndpointAnnotation>().Any(sb => string.Equals(sb.Name, uri.Scheme, StringComparisons.EndpointAnnotationName)))
var endpointAnnotations = projectResource.Annotations.OfType<EndpointAnnotation>().Where(sb => string.Equals(sb.Name, uri.Scheme, StringComparisons.EndpointAnnotationName));
if (endpointAnnotations.Any(sb => sb.IsProxied))
{
// If someone uses WithEndpoint in the dev host to register a endpoint with the name
// http or https this exception will be thrown.
throw new DistributedApplicationException($"Endpoint with name '{uri.Scheme}' already exists.");
}

if (endpointAnnotations.Any())
{
// We have a non-proxied endpoint with the same name as the 'url', don't add another endpoint for the same name
continue;
}

var generatedEndpointAnnotation = new EndpointAnnotation(
ProtocolType.Tcp,
uriScheme: uri.Scheme,
Expand Down
Loading