Exemple #1
0
        /// <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 async Task <IWorkerBackgroundService <T> > CreateBackgroundServiceAsync <T>(this IWorker webWorkerProxy, Action <WorkerInitOptions> workerInitOptionsModifier) where T : class
        {
            var options = new WorkerInitOptions();

            workerInitOptionsModifier(options);
            return(await webWorkerProxy.CreateBackgroundServiceAsync <T>(options));
        }
Exemple #3
0
        public async Task <IInstanceHandle> CreateInstance(Type t, Action <WorkerInitOptions> workerInitOptionsModifier)
        {
            var options = new WorkerInitOptions();

            workerInitOptionsModifier(options);
            return(await CreateInstance(t, options));
        }
Exemple #4
0
 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);
 }
Exemple #5
0
        public static async Task <IWorkerBackgroundService <T> > CreateBackgroundServiceAsync <T>(this IWorker webWorkerProxy, Action <WorkerInitOptions> workerInitOptionsModifier) where T : class
        {
            if (webWorkerProxy is null)
            {
                throw new ArgumentNullException(nameof(webWorkerProxy));
            }

            var options = new WorkerInitOptions();

            workerInitOptionsModifier(options);
            return(await webWorkerProxy.CreateBackgroundServiceAsync <T>(options));
        }
        public static async Task <IWorkerBackgroundService <T> > CreateBackgroundServiceAsync <T>(this IWorker webWorkerProxy, WorkerInitOptions workerInitOptions = null) where T : class
        {
            var proxy = new WorkerBackgroundServiceProxy <T>(webWorkerProxy, new WebWorkerOptions());

            if (workerInitOptions == null)
            {
                workerInitOptions = new WorkerInitOptions().AddAssemblyOf <T>();
            }

            await proxy.InitAsync(workerInitOptions);

            return(proxy);
        }
Exemple #7
0
        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 InitializeAsync(WorkerInitOptions options = null)
        {
            if (!IsInitialized)
            {
                if (!this.worker.IsInitialized)
                {
                    initWorker = new TaskCompletionSource <InitServiceResult>();
                    await this.worker.InitAsync(options);

                    await this.initWorker.Task;
                }

                IsInitialized = true;
            }
        }
        public static async Task <IWorkerBackgroundService <T> > CreateBackgroundServiceAsync <T>(this IWorker webWorkerProxy, WorkerInitOptions workerInitOptions = null) where T : class
        {
            var proxy = new WorkerBackgroundServiceProxy <T>(webWorkerProxy, new WebWorkerOptions());

            if (workerInitOptions == null)
            {
                workerInitOptions = new WorkerInitOptions()
                {
                    // Takes a (not so) wild guess and sets the dll name to the assembly name
                    DependentAssemblyFilenames = new[] { $"{typeof(T).Assembly.GetName().Name}.dll" }
                };
            }
            await proxy.InitAsync(workerInitOptions);

            return(proxy);
        }
Exemple #10
0
        public async Task <IInstanceHandle> CreateInstance(Type t, WorkerInitOptions options)
        {
            var id = ++sourceId;

            if (!this.simpleInstanceServiceProxy.IsInitialized)
            {
                if (options == null)
                {
                    options = new WorkerInitOptions();
                    options.AddAssemblyOfType(t);
                }

                if (options.UseConventionalServiceAssembly)
                {
                    options.AddAssemblyOfType(t);
                }

                await this.simpleInstanceServiceProxy.InitializeAsync(
                    new WorkerInitOptions
                {
                    InitEndPoint = initEndpointID,
                    EndInvokeCallBackEndpoint = endInvokeCallBackEndpointID
                }.MergeWith(options));;
            }
            var initResult = await this.simpleInstanceServiceProxy.InitInstance(
                new InitInstanceRequest
            {
                Id           = id,
                TypeName     = t.FullName,
                AssemblyName = t.Assembly.GetName().FullName
            });

            if (!initResult.IsSuccess)
            {
                throw new WorkerInstanceInitializeException(initResult.ExceptionMessage, initResult.FullExceptionString);
            }

            return(new CoreInstanceHandle(async() => await OnDispose(id)));
        }
Exemple #11
0
 public Task <IInstanceHandle> CreateInstance <T>(WorkerInitOptions workerInitOptions)
 {
     return(CreateInstance(typeof(T), workerInitOptions));
 }
Exemple #12
0
        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;
        }