Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor: Managing Environment Specific Variables

How can I manage access variables which differ among environments in client side blazor? Normally since I use Azure to publish applications, I'd use the appsettings.json file for local app settings and then set up entries in the Azure Application Settings section of my App Service for entries that differ between local environments and other environments.

An example of what I'm looking to accomplish:

Client side Blazor:

@functions {
    //...more code here
    await Http.PostJsonAsync<object>("http://localhost:50466/api/auth/register", vm);
}

on the deployed web server this should be transformed to:

@functions {
    //...more code here
    await Http.PostJsonAsync<object>("http://wwww.mywebsite.com/api/auth/register", vm);
}

So I'm looking for a way to store the site root url in an environment variable and transform it upon publish. Is there a Blazor-ey way to do this?

like image 438
GregH Avatar asked Jan 31 '19 14:01

GregH


People also ask

Why is Blazor not used?

Blazor is a tough sell to current web developer, because it means leaving behind many of the libraries and technologies that have been up over a decade of modern JavaScript development. It's not a seamless transition, and there's no way to migrate a JavaScript application to a Blazor project.

Is Blazor fast enough?

Blazor is very fast in terms of build and debugging. Because it takes advantage of Visual Studio and the whole experience accumulated among its tools and plugins, the development and debugging can be really fast.

How do you maintain a session in Blazor?

First to access browser seesionStorage in Blazor apps, write a custom code or use a third party package. The accessed data can be stored in the localStorage and sessionStorage. The localStorage is scoped to the user's browser. If the user reloads the page or closes and reopens the browser, the state persists.


1 Answers

You can create singleton with configuration interface and inject it in you components.

.csproj

<ItemGroup>
   <EmbeddedResource Include="appsettings.Development.json" Condition="'$(Configuration)' == 'Debug'">
     <LogicalName>appsettings.json</LogicalName>
   </EmbeddedResource>
   <EmbeddedResource Include="appsettings.json" Condition="'$(Configuration)' == 'Release'">
      <LogicalName>appsettings.json</LogicalName>
   </EmbeddedResource>
</ItemGroup>

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(GetConfiguration());
    }

    private IConfiguration GetConfiguration()
    {
        // Get the configuration from embedded dll.
        using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("appsettings.json"))
        using (var reader = new StreamReader(stream))
        {
            return JsonConvert.DeserializeObject<IConfiguration>(reader.ReadToEnd());
        }
    }

MyComponent.razor

@inject Configuration.IConfiguration Configuration;

Or look this issue

like image 134
Kliment Ru Avatar answered Oct 27 '22 20:10

Kliment Ru