Esempio n. 1
0
        /// <summary>
        /// Set value. If was binded - override it.
        /// If type cant be casted - throw exception.
        /// </summary>
        public void BindValue(string parameterName, object value)
        {
            var data = _dataByParamName[parameterName];

            var t             = data.Info.ParameterType;
            var castException = new TelegramAspException($"Model binding exception. Object '{value}' can't be used as '{t}'.");

            if ((value != null) && !t.IsAssignableFrom(value.GetType()))
            {
                throw castException;
            }
            if (value == null && t.IsValueType)
            {
                throw castException;
            }

            data.IsBinded = true;
            data.Value    = value;
        }
        /// <summary>
        /// Execute initialization callbacks and build pipeline.
        /// <para></para>
        /// Called automatically on Start() first call.
        /// </summary>
        /// <param name="serviceProvider">
        /// If not null - will use passed provider instead builded from service collection.
        /// Useful when you wan't to set same container in ASP.NET and here.
        /// </param>
        /// <param name="updatesReceiver">
        /// Use it to customize how bot receiving updates.
        /// Default is <see cref="PollingUpdatesReceiver"/> and subscribe on <see cref="ITelegramBotClient.OnUpdate"/>.
        /// It will set webhook string empty, to enable polling.
        /// </param>
        public void Setup(IServiceProvider serviceProvider = null, IUpdatesReceiver updatesReceiver = null)
        {
            //?Why use IUpdatesReceiver? Why not just make ProcessUpdate method public?
            //Was decided to use interface with event to wrote more "infrastructure" code in BotManager.

            lock (_setupLocker)
            {
                try
                {
                    if (_isSetup)
                    {
                        return;
                    }

                    //Init bot context.
                    var botInfo = _bot.GetMeAsync().Result;
                    BotContext = new BotClientContext(_bot, botInfo);

                    //Set UpdateReceiver.
                    _updatesReceiver = updatesReceiver ?? new PollingUpdatesReceiver();
                    _updatesReceiver.Init(this);

                    //Build service provider.
                    string providerIndentifierForLog;
                    if (serviceProvider == null)
                    {
                        Services = _serviceCollectionWrapper.Services.BuildServiceProvider();
                        providerIndentifierForLog = "ServiceProvider builded from ServiceCollection.";
                    }
                    else
                    {
                        Services = serviceProvider;
                        providerIndentifierForLog = "Passed ServiceProvider used.";
                    }
                    _serviceCollectionWrapper = null;

                    //Resolve services needed for BotManager.
                    ResolveBotManagerServices();
                    _logger.LogTrace("Services initialized. " + providerIndentifierForLog);

                    //Not implemented.
                    IPipelineBuilder pipelineBuilder = new PipelineBuilder(Services);

                    //Register middleware.
                    UseMandatoryMiddleware(pipelineBuilder);
                    if (_pipelineBuilderAction == null)
                    {
                        throw new NullReferenceException(
                                  "Can`t init without pipeline builder configure action. " +
                                  "Probably ConfigureBuilder wasn't invoked."
                                  );
                    }
                    _pipelineBuilderAction.Invoke(pipelineBuilder);
                    _pipelineBuilderAction = null;

                    //Build pipeline.
                    _updateProcessingDelegate = pipelineBuilder.Build();
                    _isSetup = true;
                    _logger.LogTrace("Pipeline builded.");
                }
                catch (Exception ex)
                {
                    var exception = new TelegramAspException("BotManager setup exception.", ex);
                    if (_logger == null)
                    {
                        Debug.WriteLine(exception.ToString());
                    }
                    else
                    {
                        _logger.LogError(exception, "");
                    }
                    throw exception;
                }
            }
        }