当前位置: 首页>>代码示例>>C#>>正文


C# IModel.Dispose方法代码示例

本文整理汇总了C#中IModel.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# IModel.Dispose方法的具体用法?C# IModel.Dispose怎么用?C# IModel.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IModel的用法示例。


在下文中一共展示了IModel.Dispose方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Start

        protected async Task Start(bool noAck, Action<object, BasicDeliverEventArgs> onReceived)
        {
            await Task.Run(() =>
            {
                while (_isRunning)
                {
                    try
                    {
                        _stopConsumerEvent = new ManualResetEventSlim(false);

                        _channel = _modelFactory.CreateModel();

                        var consumer = new EventingBasicConsumer(_channel);

                        consumer.Received += (obj, evtArgs) =>
                        {
                            try
                            {
                                onReceived(obj, evtArgs);
                            }
                            catch (Exception ex)
                            {
                                _onError(ex, $"Error processing message from {_queueName}");
                            }
                        };

                        consumer.Shutdown += (obj, evtArgs) =>
                        {
                            _onError(new Exception(evtArgs.ReplyText), $"{_queueName}: Consumer shutdown: {evtArgs.ReplyText}");
                            _stopConsumerEvent.Set();
                        };

                        _channel.BasicConsume(_queueName, noAck, consumer);

                        _stopConsumerEvent.Wait();
                    }
                    catch (Exception ex)
                    {
                        _onError(ex, $"Error while trying to consume from {_queueName}: {ex.Message}");
                    }

                    Task.Delay(1000).Wait();
                }

                _channel.Dispose();
            });
        }
开发者ID:RagtimeWilly,项目名称:WhiteRabbit,代码行数:47,代码来源:ConsumerBase.cs

示例2: StartConnection

		private void StartConnection()
		{
			var t = Task.Factory.StartNew(() =>
				{
					try
					{
						_Connection = GetConnectionFac().CreateConnection();
						AddConnectionShutdownDelegate(_Connection);

						try
						{
							_Model = _Connection.CreateModel();
						}
						catch (Exception e)
						{
							InternalLogger.Error("could not create model, {0}", e);
						}

						if (_Model != null && !Passive)
						{
							try
							{
								_Model.ExchangeDeclare(_Exchange, _ExchangeType, _Durable);
							}
							catch (Exception e)
							{
								if (_Model != null)
								{
									_Model.Dispose();
									_Model = null;
								}
								InternalLogger.Error(string.Format("could not declare exchange, {0}", e));
							}
						}
					}
					catch (Exception e)
					{
						InternalLogger.Error(string.Format("could not connect to Rabbit instance, {0}", e));
					}
				});

			if (!t.Wait(TimeSpan.FromMilliseconds(Timeout)))
				InternalLogger.Warn("starting connection-task timed out, continuing");
		}
开发者ID:schillerhiqs,项目名称:NLog.RabbitMQ,代码行数:44,代码来源:RabbitMQ.cs

示例3: ShutdownAmqp

        private void ShutdownAmqp(IConnection connection, IModel model, ShutdownEventArgs reason)
        {
            try
            {
                if (connection != null)
                {
                    connection.ConnectionShutdown -= ShutdownAmqp;
                    connection.AutoClose = true;
                }

                if (model != null)
                {
                    model.ModelShutdown -= this.OnModelShutdown;
                    this.Debug("closing amqp model. {0}.", reason);
                    model.Close(Constants.ReplySuccess, "closing rabbitmq appender, shutting down logging");
                    model.Dispose();

                    this.Debug("amqp model closed.");
                }
            }
            catch (Exception e)
            {
                ErrorHandler.Error("could not close model", e);
            }
        }
开发者ID:TarheadStudio,项目名称:log4net.RabbitMQ,代码行数:25,代码来源:RabbitMQAppenderBase.cs

示例4: StartConnection

		/// <summary>
		/// Never throws, blocks until connection is established
		/// </summary>
		private void StartConnection()
		{
			int connectionAttempts = 0;
			while (!Closed)
			{
				connectionAttempts++;
				// Shared lock with ShutdownAmqp, since they both access _Model and _Connection
				lock (this._ConnectionLock)
				{
					if (Closed) return; // double checking to make sure we don't open a connection during shutdown
					// If we already have an open Model, don't open a new one
					if (_Model != null && _Model.IsOpen) return;

					try
					{
						while (_Connection == null || !_Connection.IsOpen)
						{
							// If the connection is closed, dispose of it to make sure resources are released immediately
							if (_Connection != null)
							{
								try
								{
									_Connection.Dispose();
								}
								catch (OperationInterruptedException)
								{
								}
								catch (IOException)
								{
								}
								_Connection = null;
							}
							// If we need a new connection, we need a new model, so dispose of the existing model
							if (_Model != null)
							{
								_Model.Dispose();
								_Model = null;
							}

							// Open a new connection
							ConnectionFactory factory = GetConnectionFac();
							try
							{
								_Connection = factory.CreateConnection();
							}
							catch (BrokerUnreachableException)
							{
								InternalLogger.Error(
									"Could not reach RabbitMQ broker at {0}, attempt {1}",
									factory.HostName,
									connectionAttempts);
							}
						}

						try
						{
							_Model = _Connection.CreateModel();
							_Model.ExchangeDeclare(_Exchange, ExchangeType.Topic, _Durable);
						}
						catch (Exception e)
						{
							InternalLogger.Error("Could not create model, {0}", e);
							if (_Model != null)
							{
								_Model.Dispose();
								_Model = null;
							}
						}
					}
					catch (Exception e)
					{
						InternalLogger.Error(string.Format("Could not connect to Rabbit instance, {0}", e));
					}
				}
				// simple exponentially increasing wait time for each attempt, max at once a minute
				Thread.Sleep(Math.Min(250 * connectionAttempts * connectionAttempts, 60000));
			}
		}
开发者ID:nslowes,项目名称:NLog.RabbitMQ,代码行数:81,代码来源:RabbitMQ.cs


注:本文中的IModel.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。