Exemplo n.º 1
0
        public IObservable <InteractiveSessionEvent> ObserveSessionEvents()
        {
            var events = GetSession().Events;

            events.Subscribe(new Observer <InteractiveSessionEvent> (evnt => {
                if (hostingEnvironment.IsDevelopment())
                {
                    var settings = new Serialization.ExternalInteractiveJsonSerializerSettings();
                    logger.LogDebug(
                        "posting session event: {0}",
                        JsonConvert.SerializeObject(evnt, settings));
                }

                if (evnt.Data is Compilation compilation)
                {
                    compilation.References.ForEach(assemblyDefinition => {
                        if (assemblyDefinition.Content.Location.Exists)
                        {
                            referenceWhitelist.Add(assemblyDefinition.Content.Location);
                        }
                    });
                }
            }));

            return(events);
        }
        public ChannelReader <InteractiveSessionEvent> ObserveSessionEvents()
        {
            var channel = Channel.CreateUnbounded <InteractiveSessionEvent> ();

            void OnNext(InteractiveSessionEvent evnt)
            {
                if (hostingEnvironment.IsDevelopment())
                {
                    var settings = new Serialization.ExternalInteractiveJsonSerializerSettings();
                    logger.LogDebug(
                        "posting session event: {0}",
                        JsonConvert.SerializeObject(evnt, settings));
                }

                if (evnt.Data is Compilation compilation)
                {
                    compilation.References.ForEach(assemblyDefinition => {
                        if (assemblyDefinition.Content.Location.Exists)
                        {
                            referenceWhitelist.Add(assemblyDefinition.Content.Location);
                        }
                    });
                }

                channel.Writer.TryWrite(evnt);
            }

            var subscription = GetSession().Events.Subscribe(new Observer <InteractiveSessionEvent> (
                                                                 OnNext,
                                                                 error => channel.Writer.TryComplete(error),
                                                                 () => channel.Writer.TryComplete()));

            var abortRegistration = Context.ConnectionAborted.Register(() => channel.Writer.TryComplete());

            channel.Reader.Completion.ContinueWith(task => {
                subscription.Dispose();
                abortRegistration.Dispose();
            });

            return(channel.Reader);
        }