-
Notifications
You must be signed in to change notification settings - Fork 519
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
[Aspire.Microsoft.Azure.Cosmos] How to access the CosmosDB emulator data explorer? #5163
Comments
A possible option I saw was doing the following: var cosmos = builder.AddAzureCosmosDB("CosmosDb")
.AddDatabase("mydatabase");
if (builder.Environment.IsDevelopment())
{
cosmos.WithHttpsEndpoint(8081, 8081, "emulator-port")
.RunAsEmulator();
} However, doing the thing above results in I would like to be confirmed that this is the correct approach, and have it in the documentation for reference. |
I did that but when I go to the endpoint in my browser it complains I need an authorization header. |
@obiwanjacobi per the docs for the Linux based emulator (which is what Aspire runs):
So mapping That problem has actually been fixed as of yesterday, however the version with the fix is tagged as var cosmos = builder.AddAzureCosmosDB("cosmos")
.WithHttpEndpoint(51234, 1234, "explorer-port")
.WithExternalHttpEndpoints()
.RunAsEmulator(cfgContainer =>
{
cfgContainer
.WithImageRegistry("mcr.microsoft.com")
.WithImage("cosmosdb/linux/azure-cosmos-emulator")
.WithImageTag("vnext-preview");
}); Don't get me started on the kilometers of rabbit hole I've crawled through today to figure this out and get it to work - but it works: |
Slight tweak: var cosmos = builder.AddAzureCosmosDB("cosmos")
.RunAsEmulator(cfgContainer =>
{
cfgContainer
.WithHttpEndpoint(targetPort: 1234, "explorer-port")
.WithImageRegistry("mcr.microsoft.com")
.WithImage("cosmosdb/linux/azure-cosmos-emulator")
.WithImageTag("vnext-preview");
}); |
This is what I got (AppHost) var cosmos = builder.AddAzureCosmosDB("myaccount");
var cosmosDb = cosmos.AddDatabase("mydatabase");
if (builder.Environment.IsDevelopment())
{
cosmos.RunAsEmulator(config => config
.WithHttpEndpoint(targetPort: 1234, name: "explorer-port")
.WithImageRegistry("mcr.microsoft.com")
.WithImage("cosmosdb/linux/azure-cosmos-emulator")
.WithImageTag("vnext-preview")
//.WithLifetime(ContainerLifetime.Persistent)
);
} The container running cosmosDB is marked as unhealthy. This is showing in the dashboard for the resource details: I would expect to see port (I commented out the |
Ah - so there's a proxy in front of it. Right, thanks. But still, why does my container read unhealthy when I configure this? |
EDIT: I think this is probably wrong. It's more likely related to the SSL issue - I've spent a little bit of time on it but need to look into it more. Original: I'm still trying to get my head around why exactly this happens and what the fix might be, but the problem seems to be that when using a custom container image it doesn't publish the connection string, and Aspire is waiting for the publish event to mark it as healthy. Or more specifically, waiting for a
Without debugging Apsire locally, I can't determine whether the issue is caused by a difference in the container image, or a difference in the way Aspire instantiates it when you provide a custom container spec. See:
I can't see anything here that would cause it inherently, but it's possible that because this code is adding it as
But I'm a little out of my depth here and mostly speculating. Either way I'm fairly sure that when using this image the connection string is either not published or the resource is not registered as an implementation of Maybe @davidfowl can help? Obviously aside from the health check, we need the connection string and/or client to use the resource, but there's definitely a workaround for that. I don't have time to figure that out right now but I might get some time on the weekend to poke around at this a bit more. |
Got it working for me with an update to set https on container startup as detailed in notes on the docs for the linux emulator. Both explorer, health check, persistent lifetime and also client side with connection string resolving work as expected. The explorer however gives unsafe warning. Would be great for others to verify. Should any defaults change to use https or is this only part of the preview emulator container? Example code: var cosmos = builder.AddAzureCosmosDB("cosmos")
.RunAsEmulator(cfgContainer =>
{
cfgContainer
.WithHttpsEndpoint(targetPort: 1234, name: "explorer-port")
.WithImageRegistry("mcr.microsoft.com")
.WithImage("cosmosdb/linux/azure-cosmos-emulator")
.WithImageTag("vnext-preview")
.WithLifetime(ContainerLifetime.Persistent)
.WithArgs("--protocol", "https");
}); |
@trulsmp working for me too! |
Ok so the dashboard works and the emu reports healthy. But I can't get any queries to work with the SDK. I spent a lot of time on this today and had to switch to an instance on Azure to get unblocked with my day job. Obviously not an Aspire issue, but @trulsmp just wondering whether you had this issue too? If it's not just me I'll try to find some time to create a small repro and report it on the emulator repo. |
@matt-goldman I got queries to work and tested with item create and retrieval. Using the client integration in Azure Function isolated project orchestrated by Aspire. I can share some example codebits, nothing special here just following standard setup. Program.cs in Functions project using the client from Aspire. Using same name for the resource as in AppHost. Package used is Aspire.Microsoft.Azure.Cosmos according to docs: https://learn.microsoft.com/en-us/dotnet/aspire/database/azure-cosmos-db-integration?tabs=dotnet-cli#get-started using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
var builder = FunctionsApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.AddAzureCosmosClient("cosmos");
builder.ConfigureFunctionsWebApplication();
var host = builder.Build();
await host.RunAsync(); Using the client in a function. Checking and possibly creating database/container if needed. Note that "id" needs to match case in cosmos db. record User(string userId, string id, string name, string address);
public class HttpTriggers(CosmosClient client)
{
[Function("CreateItem")]
public async Task<IActionResult> CreateItem([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req)
{
await client.CreateDatabaseIfNotExistsAsync("cosmosdb");
var db = client.GetDatabase("cosmosdb");
await db.CreateContainerIfNotExistsAsync("user", "/userId");
var container = db.GetContainer("user");
var partitionKey = Guid.CreateVersion7().ToString();
await container.CreateItemAsync(
item: new User(partitionKey, "profile", "John Doe", "123 Main St"),
partitionKey: new PartitionKey(partitionKey)
);
return new OkResult();
} |
@trulsmp I can insert (or upsert) items, but I can't query anything. I think it's an SDK issue though, as I can via the explorer (with |
You guys got the DataExplorer to work (port 1234)? I use the same code as posted above earlier with only setting the var cosmos = builder.AddAzureCosmosDB("cosmos")
.RunAsEmulator(cfgContainer =>
{
cfgContainer
.WithHttpsEndpoint(targetPort: 1234, name: "explorer-port", isProxied: false)
.WithImageRegistry("mcr.microsoft.com")
.WithImage("cosmosdb/linux/azure-cosmos-emulator")
.WithImageTag("vnext-preview")
.WithArgs("--protocol", "https");
.WithLifetime(ContainerLifetime.Persistent)
}); Upsert and reading of a document works fine and the instance is also healthy now (not sure what I did wrong before). |
I just noticed in the environment variables of the cosmos container that the protocol is set a weird value.
Shouldn't that be http or https? |
@davidortinau FYI |
Adding (Thanks to Egil Hansen on the Orleans Discord group.) |
Added a new experimental API - RunAsPreviewEmulator. This will use the new Linux-based emulator, which starts faster. And it also has support for a built-in Data Explorer which can be enabled by calling WithDataExplorer on the emulator. Fix dotnet#5163
The Aspire.Microsoft.Azure.Cosmos component provides the
method. Aspire.NET does recognize the
8081
port in the container (see screenshot). However, without any additional settings the emulator will only be exposed via a TCP endpoint which is not enough to access the data explorer dashboard from the emulator.How can I expose the data explorer inside the emulator container best, during development? Can you please add this to the documentation?
The text was updated successfully, but these errors were encountered: