Exemplo n.º 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var serviceProvider = new ServiceCollection()
                                  .AddSingleton <IPublisherService, PublisherService>()
                                  .AddSingleton <IChannelService, ChannelService>()
                                  .AddSingleton <ISubscriberService, SubscriberService>()
                                  .BuildServiceProvider();

            publisherService  = serviceProvider.GetService <IPublisherService>();
            channelService    = serviceProvider.GetService <IChannelService>();
            subscriberService = serviceProvider.GetService <ISubscriberService>();

            var channelCentre = new Channel();
            var publisher     = new Publisher("Publisher");

            for (int i = 0; i < 5; i++)
            {
                channelService.SubscribeChannel(channelCentre, new Subscriber());
            }

            publisherService.Post(publisher, new Message("Update from Publisher"), channelCentre);
            channelService.Broadcast(channelCentre);

            int           subno = 0;
            StringBuilder html  = new StringBuilder();

            foreach (var subscriber in channelCentre.Subscribers)
            {
                subno += 1;
                RetrieveMessages(html, subscriber, subno);
            }
            SubscriberMessage.Controls.Add(new Literal {
                Text = html.ToString()
            });
        }