示例#1
0
        public virtual async Task StartHostingAsync()
        {
            // check if we can act as an injection host
            if (!Loader.RuntimeLoadAvailable && !_forceCanInject)
            {
                throw new InvalidOperationException(
                          "The current platform does not support loading assemblies at runtime.");
            }

            // start publishing host availability if we have a service definition
            if (_servicePublisher != null)
            {
                await _servicePublisher.Publish();
            }

            // wire up our client connected/disconnected methods
            _messageHub.ClientConnected.Subscribe(ClientConnected);
            _messageHub.ClientDisconnected.Subscribe(ClientDisconnected);

            // funnel any messages containing assemblies through the pipeline
            _messageHub
            .AllMessages
            .OfType <IMessageWithAssemblyBytes>()
            .Where(ShouldLoadAssembly)
            .Select(msg =>
            {
                var proxy = _messageHub.ProxyForGuid(msg.FromGuid);

                try
                {
                    return(new { Assembly = BytesToAssembly(msg.AssemblyBytes), Sender = proxy });
                }
                catch (Exception e)
                {
                    this.Log()
                    .Error(String.Format("Couldn't load assembly named {0} from {1}: '{2}'", msg.AssemblyName,
                                         proxy, e.Message));
                }

                return(null);
            })
            .Where(a => a != null)
            .Subscribe(x => ProcessNewAssembly(x.Assembly, x.Sender))
            .DisposeWith(_disposables);

            // start listening
            await _messageHub.StartListeningAsync(ServicePort);

            IsListening = true;
        }