Skip to content

Commit

Permalink
Culture env variables (Tewr#71)
Browse files Browse the repository at this point in the history
* Fix AddHttpClient extension code being ignored on .net6

* Adds options for environment variables. Fixes Tewr#67

* Link to examples in error message.

* Some cleanup + docs
  • Loading branch information
Tewr authored Jan 25, 2022
1 parent 581c5e8 commit 3453903
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/BlazorWorker.WorkerCore/BlazorWorker.WorkerCore.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 39 additions & 3 deletions src/BlazorWorker.WorkerCore/InitOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,28 @@ public WorkerInitOptions()
/// </summary>
public string EndInvokeCallBackEndpoint { get; set; }

/// <summary>
/// Sets environment variables in the target worker.
/// </summary>
/// <remarks>
/// Defaults to a single entry: DOTNET_SYSTEM_GLOBALIZATION_INVARIANT = '1'.
/// For more information see https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables
/// </remarks>
public Dictionary<string, string> EnvMap { get; set; }
= new Dictionary<string, string>() {
{ "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "1" },
};

public WorkerInitOptions MergeWith(WorkerInitOptions initOptions)
{

var newEnvMap = new Dictionary<string , string>(this.EnvMap);
if (initOptions.EnvMap != null)
{
foreach (var entry in initOptions.EnvMap)
{
newEnvMap[entry.Key] = entry.Value;
}
}
return new WorkerInitOptions
{
CallbackMethod = initOptions.CallbackMethod ?? this.CallbackMethod,
Expand All @@ -87,7 +106,8 @@ public WorkerInitOptions MergeWith(WorkerInitOptions initOptions)
UseConventionalServiceAssembly = initOptions.UseConventionalServiceAssembly,
MessageEndPoint = initOptions.MessageEndPoint ?? this.MessageEndPoint,
InitEndPoint = initOptions.InitEndPoint ?? this.InitEndPoint,
EndInvokeCallBackEndpoint = initOptions.EndInvokeCallBackEndpoint ?? this.EndInvokeCallBackEndpoint
EndInvokeCallBackEndpoint = initOptions.EndInvokeCallBackEndpoint ?? this.EndInvokeCallBackEndpoint,
EnvMap = newEnvMap
};
}
}
Expand Down Expand Up @@ -156,7 +176,7 @@ public static WorkerInitOptions AddHttpClient(this WorkerInitOptions source)
source.AddAssemblies("System.Net.Http.dll", "System.Net.Http.WebAssemblyHttpHandler.dll");
#endif

#if NET5
#if NET5_0_OR_GREATER
source.AddAssemblies(
"System.Net.Http.dll",
"System.Security.Cryptography.X509Certificates.dll",
Expand All @@ -169,5 +189,21 @@ public static WorkerInitOptions AddHttpClient(this WorkerInitOptions source)

return source;
}

/// <summary>
/// Set the specified <paramref name="environmentVariableName"/> to the specified <paramref name="value"/> when the worker runtime has been initialized
/// </summary>
/// <param name="source"></param>
/// <param name="environmentVariableName"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <remarks>
/// For more information see https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables
/// </remarks>
public static WorkerInitOptions SetEnv(this WorkerInitOptions source, string environmentVariableName, string value)
{
source.EnvMap[environmentVariableName] = value;
return source;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private static InitInstanceResult InitInstance(long callId, string typeName, str

private static Assembly LogFailedAssemblyResolve(object sender, ResolveEventArgs args)
{
Console.Error.WriteLine($"{typeof(SimpleInstanceService).FullName}: '{args.RequestingAssembly}' is requesting missing assembly '{args.Name}')");
Console.Error.WriteLine($"{typeof(SimpleInstanceService).FullName}: '{args.RequestingAssembly}' is requesting missing assembly '{args.Name}'). See https://github.com/Tewr/BlazorWorker#setup-dependencies for common solutions to this problem.");

//return null;
// Nobody really cares about this exception for now, it can't be caught.
Expand Down
12 changes: 10 additions & 2 deletions src/BlazorWorker/BlazorWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,15 @@ window.BlazorWorker = function () {
default: return fileName;
}
};
module.onRuntimeInitialized = () =>
MONO.mono_wasm_setenv('DOTNET_SYSTEM_GLOBALIZATION_INVARIANT', '1');
module.onRuntimeInitialized = () => {
const envMap = initConf.envMap;
for (const key in envMap) {
if (Object.prototype.hasOwnProperty.call(envMap, key)) {
MONO.mono_wasm_setenv(key, envMap[key]);
}
}
}

module.preRun.push(() => {
const mono_wasm_add_assembly = Module.cwrap('mono_wasm_add_assembly', null, [
'string',
Expand Down Expand Up @@ -269,6 +276,7 @@ window.BlazorWorker = function () {
endInvokeCallBackEndpoint: initOptions.endInvokeCallBackEndpoint,
wasmRoot: initOptions.wasmRoot,
blazorBoot: "_framework/blazor.boot.json",
envMap: initOptions.envMap,
debug: initOptions.debug
};

Expand Down

0 comments on commit 3453903

Please sign in to comment.