public override async Task <Outcome> LoadAssembly(LoadDynamicAssemblyRequest request, ServerCallContext context) { try { var peBytes = request.PeBytes.ToByteArray(); var pdbBytes = request.PdbBytes.ToByteArray(); var asm = Assembly.Load(peBytes, pdbBytes); var primaryType = !String.IsNullOrWhiteSpace(request.PrimaryTypeName) ? asm.GetTypes().FirstOrDefault(x => x.Name.EndsWith(request.PrimaryTypeName)) : null; var req = new ProcessNewAssemblyRequest { Assembly = asm, PrimaryType = primaryType }; return(await _reloadManager.ProcessNewAssembly(req)); } catch (Exception ex) { _log($"An error occurred when attempting to load assembly: {request.AssemblyName}"); return(new Outcome { Success = false, Messages = { new Message { Message_ = ex.ToString() } } }); } }
public override async Task <Outcome> ProcessNewAssembly(ProcessNewAssemblyRequest req) { var types = req.Assembly.GetTypes(); _logger.Information("Types: {Types}", types); // replace reloader var reloaderType = types.Where(x => x.ImplementsServiceType <IReloadManager>() && x != GetType()).LastOrDefault(); if (reloaderType != null) { _container.Register(typeof(IReloadManager), reloaderType, ifAlreadyRegistered: IfAlreadyRegistered.Replace); var newReloader = _container.Resolve <IReloadManager>(); NotifyReplacement(newReloader); return(await newReloader.ProcessNewAssembly(req)); } // replace services foreach (var serviceType in types.Where(t => t.IsSubclassOf(typeof(ServiceBase)) && !t.IsAbstract)) { _logger.Information("Registering service type {PageType} from assembly {Assembly}", serviceType, req.Assembly); foreach (var @if in serviceType.GetImplementedInterfaces()) { _logger.Information("Registering service type {ImplementationType} against {ServiceType} from assembly {Assembly}", serviceType, @if, req.Assembly); _container.Register( @if, serviceType, Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Replace, setup: Setup.With(asResolutionCall: true) ); } } // replace viewmodels foreach (var vm in types.Where(t => t.IsSubclassOf(typeof(ViewModelBase)) && !t.IsAbstract)) { _logger.Information("Registering view model type {PageType} from assembly {Assembly}", vm, req.Assembly); _container.Register(vm, ifAlreadyRegistered: IfAlreadyRegistered.Replace, setup: Setup.With(asResolutionCall: true)); } // replace pages foreach (var page in types.Where(p => p.IsSubclassOf(typeof(ContentPage)))) { _logger.Information("Registering page type {PageType} from assembly {Assembly}", page, req.Assembly); _container.Register(typeof(object), page, serviceKey: page.Name, made: Made.Of(FactoryMethod.ConstructorWithResolvableArguments), ifAlreadyRegistered: IfAlreadyRegistered.Replace); } // go to the primary type var primaryType = req.PrimaryType ?? types.FirstOrDefault(x => x.IsSubclassOf(typeof(ContentPage))); if (primaryType != null && primaryType.IsSubclassOf(typeof(ContentPage))) { _logger.Information("Navigate to {PrimaryType}", primaryType); await Device.InvokeOnMainThreadAsync( () => _navigationServiceFactory().NavigateAsync(primaryType.Name, new NavigationParameters(), null, animated: false)); } return(new Outcome { Success = true }); }
public abstract Task <Outcome> ProcessNewAssembly(ProcessNewAssemblyRequest req);