Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="channelInfo"></param>
        /// <returns></returns>
        public IChannelContext CreateContext(ChannelInfo channelInfo)
        {
            #region Validate parameters
            if (channelInfo == null)
            {
                throw new ArgumentNullException(nameof(channelInfo));
            }
            #endregion

            //channelInfo.Description.Type
            MicroserviceDescription description = _addinManager.FindMicroservice(channelInfo.Provider);
            return(new ProcessChannelContext(channelInfo, description, _factory, _dataAdapter));
        }
Exemplo n.º 2
0
        public void LoadChannels()
        {
            var errors = new List <Exception>();

            GroupInfo          deafaultGroup = GetOrCreateDefaultGroup();
            List <ChannelInfo> allChannels   = _dataAdapter.GetChannels();
            List <GroupInfo>   allGroups     = _dataAdapter.GetGroups();

            allChannels.ForEach(channelInfo =>
            {
                try
                {
                    // Если канал не принадлежит ни одной группе, то включаем его в группу по умолчанию
                    if (!allGroups.Any(group => group.Channels.Contains(channelInfo.LINK)))
                    {
                        var map = new GroupChannelMap()
                        {
                            GroupLINK = deafaultGroup.LINK, ChannelLINK = channelInfo.LINK
                        };
                        _dataAdapter.SaveGroupMap(map);
                    }
                }
                catch (Exception ex)
                {
                    lock (errors)
                    {
                        errors.Add(ex);
                    }
                }
            });
            RefreshGroups();

            // Создание и запуск каналов
            allChannels.ForEach(channelInfo =>
                                //allChannels.AsParallel().ForAll(channelInfo =>
            {
                try
                {
                    MicroserviceDescription description = _addinManager.FindMicroservice(channelInfo.Provider);
                    if (description == null)
                    {
                        channelInfo.Enabled = false;
                    }
                    else
                    {
                        channelInfo.SetDescription(description);
                    }

                    if (channelInfo.IsSystem())
                    {
                        channelInfo.SetRealAddress(_busSettings.Database.ConnectionString);
                    }

                    _dataAdapter.SaveChannelInfo(channelInfo);

                    if (channelInfo.Enabled)
                    {
                        IChannelContext context = _contextFactory.CreateContext(channelInfo);
                        if (!_channels.TryAdd(channelInfo.VirtAddress, context))
                        {
                            context.Dispose();
                            throw new InvalidOperationException($"Канал {channelInfo.VirtAddress} уже существует.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    lock (errors)
                    {
                        errors.Add(ex);
                    }
                }
            });


            _channels.Values.ToList().ForEach(context =>
                                              //_channels.Values.AsParallel().ForAll(context =>
            {
                ChannelInfo channelInfo  = context.ChannelInfo;
                ChannelSettings settings = channelInfo.ChannelSettings();
                if (settings.AutoOpen)
                {
                    try
                    {
                        IChannel channel = context.CreateChannelAsync().Result;
                        //channel.SetSettingsAsync();
                        channel.OpenAsync().Wait();
                    }
                    catch (Exception ex)
                    {
                        lock (errors)
                        {
                            errors.Add(ex);
                        }
                    }
                }
            });

            _channels.Values.ToList().ForEach(context =>
                                              //_channels.Values.AsParallel().ForAll(context =>
            {
                ChannelInfo channelInfo = context.ChannelInfo;
                IChannel channel        = context.Channel;
                if (channel != null && channel.IsOpened)
                {
                    ChannelSettings settings = channelInfo.ChannelSettings();
                    if (settings.AutoRun)
                    {
                        try
                        {
                            channel.RunAsync().Wait();
                        }
                        catch (Exception ex)
                        {
                            lock (errors)
                            {
                                errors.Add(ex);
                            }
                        }
                    }
                }
            });

            if (errors.Count > 0)
            {
                throw new AggregateException(errors);
            }
        }