Skip to content

Commit 3453903

Browse files
authored
Culture env variables (#71)
* Fix AddHttpClient extension code being ignored on .net6 * Adds options for environment variables. Fixes #67 * Link to examples in error message. * Some cleanup + docs
1 parent 581c5e8 commit 3453903

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

src/BlazorWorker.WorkerCore/BlazorWorker.WorkerCore.xml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BlazorWorker.WorkerCore/InitOptions.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,28 @@ public WorkerInitOptions()
7474
/// </summary>
7575
public string EndInvokeCallBackEndpoint { get; set; }
7676

77+
/// <summary>
78+
/// Sets environment variables in the target worker.
79+
/// </summary>
80+
/// <remarks>
81+
/// Defaults to a single entry: DOTNET_SYSTEM_GLOBALIZATION_INVARIANT = '1'.
82+
/// For more information see https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables
83+
/// </remarks>
84+
public Dictionary<string, string> EnvMap { get; set; }
85+
= new Dictionary<string, string>() {
86+
{ "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "1" },
87+
};
88+
7789
public WorkerInitOptions MergeWith(WorkerInitOptions initOptions)
7890
{
79-
91+
var newEnvMap = new Dictionary<string , string>(this.EnvMap);
92+
if (initOptions.EnvMap != null)
93+
{
94+
foreach (var entry in initOptions.EnvMap)
95+
{
96+
newEnvMap[entry.Key] = entry.Value;
97+
}
98+
}
8099
return new WorkerInitOptions
81100
{
82101
CallbackMethod = initOptions.CallbackMethod ?? this.CallbackMethod,
@@ -87,7 +106,8 @@ public WorkerInitOptions MergeWith(WorkerInitOptions initOptions)
87106
UseConventionalServiceAssembly = initOptions.UseConventionalServiceAssembly,
88107
MessageEndPoint = initOptions.MessageEndPoint ?? this.MessageEndPoint,
89108
InitEndPoint = initOptions.InitEndPoint ?? this.InitEndPoint,
90-
EndInvokeCallBackEndpoint = initOptions.EndInvokeCallBackEndpoint ?? this.EndInvokeCallBackEndpoint
109+
EndInvokeCallBackEndpoint = initOptions.EndInvokeCallBackEndpoint ?? this.EndInvokeCallBackEndpoint,
110+
EnvMap = newEnvMap
91111
};
92112
}
93113
}
@@ -156,7 +176,7 @@ public static WorkerInitOptions AddHttpClient(this WorkerInitOptions source)
156176
source.AddAssemblies("System.Net.Http.dll", "System.Net.Http.WebAssemblyHttpHandler.dll");
157177
#endif
158178

159-
#if NET5
179+
#if NET5_0_OR_GREATER
160180
source.AddAssemblies(
161181
"System.Net.Http.dll",
162182
"System.Security.Cryptography.X509Certificates.dll",
@@ -169,5 +189,21 @@ public static WorkerInitOptions AddHttpClient(this WorkerInitOptions source)
169189

170190
return source;
171191
}
192+
193+
/// <summary>
194+
/// Set the specified <paramref name="environmentVariableName"/> to the specified <paramref name="value"/> when the worker runtime has been initialized
195+
/// </summary>
196+
/// <param name="source"></param>
197+
/// <param name="environmentVariableName"></param>
198+
/// <param name="value"></param>
199+
/// <returns></returns>
200+
/// <remarks>
201+
/// For more information see https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables
202+
/// </remarks>
203+
public static WorkerInitOptions SetEnv(this WorkerInitOptions source, string environmentVariableName, string value)
204+
{
205+
source.EnvMap[environmentVariableName] = value;
206+
return source;
207+
}
172208
}
173209
}

src/BlazorWorker.WorkerCore/SimpleInstanceService/SimpleInstanceService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ private static InitInstanceResult InitInstance(long callId, string typeName, str
231231

232232
private static Assembly LogFailedAssemblyResolve(object sender, ResolveEventArgs args)
233233
{
234-
Console.Error.WriteLine($"{typeof(SimpleInstanceService).FullName}: '{args.RequestingAssembly}' is requesting missing assembly '{args.Name}')");
234+
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.");
235235

236236
//return null;
237237
// Nobody really cares about this exception for now, it can't be caught.

src/BlazorWorker/BlazorWorker.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,15 @@ window.BlazorWorker = function () {
9292
default: return fileName;
9393
}
9494
};
95-
module.onRuntimeInitialized = () =>
96-
MONO.mono_wasm_setenv('DOTNET_SYSTEM_GLOBALIZATION_INVARIANT', '1');
95+
module.onRuntimeInitialized = () => {
96+
const envMap = initConf.envMap;
97+
for (const key in envMap) {
98+
if (Object.prototype.hasOwnProperty.call(envMap, key)) {
99+
MONO.mono_wasm_setenv(key, envMap[key]);
100+
}
101+
}
102+
}
103+
97104
module.preRun.push(() => {
98105
const mono_wasm_add_assembly = Module.cwrap('mono_wasm_add_assembly', null, [
99106
'string',
@@ -269,6 +276,7 @@ window.BlazorWorker = function () {
269276
endInvokeCallBackEndpoint: initOptions.endInvokeCallBackEndpoint,
270277
wasmRoot: initOptions.wasmRoot,
271278
blazorBoot: "_framework/blazor.boot.json",
279+
envMap: initOptions.envMap,
272280
debug: initOptions.debug
273281
};
274282

0 commit comments

Comments
 (0)