/// <summary> /// Creates a background service using the specified <paramref name="factoryExpression"/>. /// </summary> /// <typeparam name="TFactory"></typeparam> /// <typeparam name="TService"></typeparam> /// <param name="webWorkerProxy"></param> /// <param name="factoryExpression"></param> /// <param name="workerInitOptionsModifier"></param> /// <returns></returns> public static async Task <IWorkerBackgroundService <TService> > CreateBackgroundServiceUsingFactoryAsync <TFactory, TService>( this IWorker webWorkerProxy, Expression <Func <TFactory, TService> > factoryExpression, Action <WorkerInitOptions> workerInitOptionsModifier = null) where TFactory : class where TService : class { if (webWorkerProxy is null) { throw new ArgumentNullException(nameof(webWorkerProxy)); } if (factoryExpression is null) { throw new ArgumentNullException(nameof(factoryExpression)); } var workerInitOptions = new WorkerInitOptions(); if (workerInitOptionsModifier == null) { workerInitOptions.AddAssemblyOf <TService>(); } else { workerInitOptionsModifier(workerInitOptions); } var factoryProxy = new WorkerBackgroundServiceProxy <TFactory>(webWorkerProxy, new WebWorkerOptions()); await factoryProxy.InitAsync(workerInitOptions); var newProxy = await factoryProxy.InitFromFactoryAsync(factoryExpression); newProxy.Disposables.Add(factoryProxy); return(newProxy); }
public static WorkerInitOptions AddBlazorWorkerJsRuntime(this WorkerInitOptions source) { source.AddAssemblyOf <Microsoft.JSInterop.IJSRuntime>() .AddAssemblyOf <BlazorWorker.Extensions.JSRuntime.BlazorWorkerJSRuntime>() .AddAssemblyOf <System.Text.Json.JsonElement>() .AddAssemblyOf <System.Text.Encodings.Web.TextEncoderSettings>(); return(source); }
public async Task InitAsync(WorkerInitOptions workerInitOptions = null) { workerInitOptions = workerInitOptions ?? new WorkerInitOptions(); if (this.initTask != null) { await initTask.Task; } if (this.IsInitialized) { return; } initTask = new TaskCompletionSource <bool>(); if (!this.worker.IsInitialized) { initWorkerTask = new TaskCompletionSource <bool>(); if (workerInitOptions.UseConventionalServiceAssembly) { workerInitOptions.AddAssemblyOf <T>(); } await this.worker.InitAsync(new WorkerInitOptions { DependentAssemblyFilenames = WorkerBackgroundServiceDependencies.DependentAssemblyFilenames, InitEndPoint = InitEndPoint }.MergeWith(workerInitOptions)); this.worker.IncomingMessage += OnMessage; await initWorkerTask.Task; } var message = this.options.MessageSerializer.Serialize( new InitInstance() { WorkerId = this.worker.Identifier, // TODO: This should not really be necessary? InstanceId = instanceId, AssemblyName = typeof(T).Assembly.FullName, TypeName = typeof(T).FullName }); Console.WriteLine($"{nameof(WorkerBackgroundServiceProxy<T>)}.InitAsync(): {this.worker.Identifier} {message}"); await this.worker.PostMessageAsync(message); await initTask.Task; }
public async Task InitAsync(WorkerInitOptions workerInitOptions = null) { workerInitOptions = workerInitOptions ?? new WorkerInitOptions(); if (this.initTask != null) { await initTask.Task; } if (this.IsInitialized) { return; } initTask = new TaskCompletionSource <bool>(); if (!this.worker.IsInitialized) { initWorkerTask = new TaskCompletionSource <bool>(); if (workerInitOptions.UseConventionalServiceAssembly) { workerInitOptions.AddAssemblyOf <T>(); } await this.worker.InitAsync(new WorkerInitOptions() { DependentAssemblyFilenames = new[] { $"{typeof(BaseMessage).Assembly.GetName().Name}.dll", $"{typeof(WorkerInstanceManager).Assembly.GetName().Name}.dll", $"{typeof(Newtonsoft.Json.JsonConvert).Assembly.GetName().Name}.dll", $"{typeof(IWorkerMessageService).Assembly.GetName().Name}.dll", "System.Xml.dll", "Serialize.Linq.dll", "System.dll", "System.Buffers.dll", "System.Data.dll", "System.Core.dll", "System.Memory.dll", "System.Numerics.dll", "System.Numerics.Vectors.dll", "System.Runtime.CompilerServices.Unsafe.dll", "System.Runtime.Serialization.dll", $"{typeof(System.Reflection.Assembly).Assembly.GetName().Name}.dll", "Microsoft.Bcl.AsyncInterfaces.dll", "System.Threading.Tasks.Extensions.dll", "Mono.Security.dll", "System.ServiceModel.Internals.dll" }, InitEndPoint = InitEndPoint }.MergeWith(workerInitOptions)); this.worker.IncomingMessage += OnMessage; await initWorkerTask.Task; } var message = this.options.MessageSerializer.Serialize( new InitInstance() { WorkerId = this.worker.Identifier, // TODO: This should not really be necessary? InstanceId = instanceId, AssemblyName = typeof(T).Assembly.FullName, TypeName = typeof(T).FullName }); Console.WriteLine($"{nameof(WorkerBackgroundServiceProxy<T>)}.InitAsync(): {this.worker.Identifier} {message}"); await this.worker.PostMessageAsync(message); await initTask.Task; }