Conditional "Adds" to umbraco builder in program.c...
# help-with-umbraco
t
This is a beginner C# question. In program.cs we have something like this:
Copy code
builder.CreateUmbracoBuilder()
    .AddBackOffice()
    .AddWebsite()
    .AddDeliveryApi()
    .AddComposers()
    .Build();
Let's say I want to conditionally add Azure blob storage, how would I do this? This won't work:
Copy code
if (shouldUseBlobStorage)
{
    builder.AddAzureBlobMediaFileSystem()
    .AddAzureBlobImageSharpCache();
}
since 'WebApplicationBuilder' does not contain a definition for 'AddAzureBlobMediaFileSystem'. Should I hold off with calling
Build()
until everything conditional has been sorted, and store the umbracoBuilder?
Copy code
var umbracoBuilder = builder.CreateUmbracoBuilder()
    .AddBackOffice()
    .AddWebsite()
    .AddDeliveryApi()
    .AddComposers();

// do conditional stuff

umbracoBuilder.Build();
? What then happens with my previous
builder
variable? Magic methods like
Build()
in ASP.NET confuse me 😕
m
I would add your conditional inside of the .AddBlah() or within a ICompser
t
@Matt Wise But wouldn't that mean having to rewrite the
Umbraco.StorageProviders.AzureBlob
package? Or do you mean:
Copy code
builder.CreateUmbracoBuilder()
    .AddBackOffice()
    .AddWebsite()
    .AddDeliveryApi()
    .AddComposers()
    // add conditional statement mid-chain here somehow
    .Build();
?
I will try to find info on the IComposer way
m
public classs AzureStorageComposer : IComposer the function you get 😄 if ( x != y) builder.AddAzureStorage() something like this (not got code in front of me sorry
t
oh ok cool, that looks a lot cleaner than my approach I did get it to work right now with this messier approach:
Copy code
var umbracoBuilder = builder.CreateUmbracoBuilder()
    .AddBackOffice()
    .AddWebsite()
    .AddDeliveryApi()
    .AddComposers();

var blobConnectionString = builder.Configuration["Umbraco:Storage:AzureBlob:Media:ConnectionString"];
var blobContainer = builder.Configuration["Umbraco:Storage:AzureBlob:Media:ContainerName"];
if (!string.IsNullOrEmpty(blobConnectionString) && !string.IsNullOrEmpty(blobContainer))
{
    umbracoBuilder.AddAzureBlobMediaFileSystem().AddAzureBlobImageSharpCache();
}

umbracoBuilder.Build();

WebApplication app = builder.Build();
await app.BootUmbracoAsync();
But composer looks better
m
Composers are great as you dont need to edit Program.cs so if HQ changes something in that file/chain its easy to spot
s
Hi @TackleMcClean 🏅, just to let you know that we'll soon stop the ability to create new questions on Discord as we're moving the #1125392773038755890 channel over to our new forum. You can read more about this initiative here: https://discord.com/channels/869656431308189746/869656431308189749/1344602175019421736 I'd recommend you log in to the forum (https://forum.umbraco.com) with your GitHub account and post this question there. Thanks, and see you there!
m
You should be able to reuse
private readonly IOptionsMonitor<AzureBlobFileSystemOptions> _optionsMonitor;
for the appsettings discovery
Just need to remember to do a comparison when you do upgrade as nuget upgrades leave them well alone.
k
We keep this in
Program.cs
to make it easy to find. A composer in a separate project would at least need a comment in
Program.cs
to let people know the builder is being amended elsewhence. Sometimes we use just `if`s and sometimes preprocessor directives. But yeah, "keeping
Program.cs
untouched from the template" would also be useful. Also, questions about code readability, discoverability, understandability, maintainability, upgradability etc are never > a beginner C# question . 🙂
7 Views