Setting Up Optimizely on Aspire
Dive into setting up Optimizely within a dotnet aspire environment, utilizing C# 8.0. Follow this detailed guide to configure your Optimizely project from the ground up. For a deeper dive and access to the full codebase, visit the Optimizely Aspire GitHub repository.
Note: You will need Visual Studio Preview and Aspire installed - https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/setup-tooling?tabs=visual-studio
Another Note: I know I have passwords in code, but this is a POC to prove it works. It will never go out to a production environment. At some point I may well move the scaffolding into a console app, and change to an if statement in the AppHost to move the functionality completely out of the web app, but for now it's fine...
Initiate an Optimizely Project:
dotnet new epicmsempty --name Something.Web
Begin by creating a new Optimizely project with the specified command.
Solution Setup in Visual Studio: Open Visual Studio, create a new empty solution, and then add the newly created web project to this solution.
Upgrade to C# 8.0: Ensure your project is using C# 8.0 for Aspire.
Configure Launch Settings: Address a potential known issue by setting your launch settings to use ports 8000 and 8001.
Integrate .Net Aspire Orchestration: Right-click on the web project, select Add → .Net Aspire Orchestration, and proceed with the given instructions (an error might pop up, ignore it for now, we fix it later).
Database Configuration: Add the database to your apphost project with the following configuration:
builder.AddSqlServerContainer("something.db", "0pti_R0cks", 1433) .WithVolumeMount("./App_Data/database-files", "/var/opt/mssql/data", VolumeMountType.Bind);
Update Launch Settings and Environment Variables: Modify the launch settings to include
RUN_MODE
among the environment variables and switch the protocol to HTTPS for security purposes:{ "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { "Web": { "commandName": "Project", "dotnetRunMessages": "true", "launchBrowser": "true", "applicationUrl": "https://localhost:15138", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "DOTNET_ENVIRONMENT": "Development", "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:16093", "RUN_MODE": "WEB" } }, "Scaffold": { "commandName": "Project", "dotnetRunMessages": "true", "launchBrowser": "true", "applicationUrl": "https://localhost:15138", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "DOTNET_ENVIRONMENT": "Development", "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:16093", "RUN_MODE": "SCAFFOLD" } } } }
Propagate
RUN_MODE
Environment Variable: Ensure theRUN_MODE
environment variable is accurately passed from the apphost to the web app by appending the following to yourAddProject
configuration:.WithEnvironment("RUN_MODE", Environment.GetEnvironmentVariable("RUN_MODE"));
Service Extension Adaptation: Modify the ServiceDefaults extensions class to support
Host.CreateDefaultBuilder(args)
for Optimizely compatibility. Specific changes are detailed in the Optimizely Aspire GitHub repository.Logging Configuration: Update the logging setup in
Program.cs
to centralize logging management:.ConfigureLogging(l => { l.AddOpenTelemetry(logging => { logging.IncludeFormattedMessage = true; logging.IncludeScopes = true; }); })
Final Integration: Complete the setup by integrating
services.AddServiceDefaults(_webHostingEnvironment, _configuration);
into yourStartup.cs
.Database Initialization and Application Launch:
- Run the AppHost in
Scaffold
mode to initialize the database. - Switch to
Web
mode to get your Optimizely site operational through Aspire.
- Run the AppHost in
For the complete source code and additional resources, explore the Optimizely Aspire GitHub repository, where you can dive deeper into the project's structure, setup nuances, and custom configurations.
Andy Blyth
Andy Blyth, an Optimizely MVP (OMVP) and Technical Architect at 26 DX with a keen interest in martial arts, occasionally ventures into blogging when memory serves.