public Message Pull(string key) { try { Topic topic = new Topic(); topic = db.Topices.Where(x => x.Key == key).FirstOrDefault(); if (topic != null) { Message message = new Message(); message = db.Messages.OrderBy(x => x.CreateDate).FirstOrDefault(x => x.TopicId == topic.Id); db.Messages.Remove(message); bool flag = db.SaveChanges() > 0; base.IsSuccess = flag; base.ReturnMsg = flag ? "出队列成功" : "出队列失败"; return(message); } else { base.IsSuccess = false; base.ReturnMsg = "不存在改主题"; return(null); } } catch (Exception ex) { base.IsSuccess = false; base.ReturnMsg = "出队列出现异常"; return(null); } }
public void Remove(Guid id) { try { Topic topic = db.Topices.FirstOrDefault(x => x.Id == id); db.Topices.Remove(topic); bool flag = db.SaveChanges() > 0; base.IsSuccess = flag; base.ReturnMsg = flag ? "删除成功" : "删除失败"; } catch (Exception ex) { base.IsSuccess = false; base.ReturnMsg = "删除失败"; } }
public void Remove(Guid id) { try { User user = new User(); user = db.Users.FirstOrDefault(x => x.Id == id); db.Users.Remove(user); bool flag = db.SaveChanges() > 0; base.IsSuccess = flag; base.ReturnMsg = flag ? "删除成功" : "删除失败"; } catch (Exception ex) { base.IsSuccess = false; base.ReturnMsg = "删除异常"; } }
static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = "localhost" }; string queueName = "RequestQueue"; var rabbitMqConnection = factory.CreateConnection(); var rabbitMqChannel = rabbitMqConnection.CreateModel(); rabbitMqChannel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null); rabbitMqChannel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); int messageCount = Convert.ToInt16(rabbitMqChannel.MessageCount(queueName)); Console.WriteLine(" Listening to the queue. This channels has {0} messages on the queue", messageCount); var consumer = new EventingBasicConsumer(rabbitMqChannel); consumer.Received += (model, ea) => { var body = ea.Body.ToArray(); Request request = JsonConvert.DeserializeObject <Request>(Encoding.UTF8.GetString(body)); using (var context = new MQContext()) { var sms = new RecievedSms { State = request.Status, }; context.RecievedSms.Add(sms); context.SaveChanges(); } Console.WriteLine("Record has been made in messages" + request.Status); rabbitMqChannel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); Thread.Sleep(1000); }; rabbitMqChannel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer); Thread.Sleep(1000 * messageCount); Console.WriteLine(" Connection closed, no more messages."); Console.ReadLine(); }
public void Save(Log log) { try { log.CreateDate = DateTime.Now; log.Thread = Thread.CurrentThread.ManagedThreadId.ToString(); db.Logs.Add(log); bool flag = db.SaveChanges() > 0; base.IsSuccess = flag; base.ReturnMsg = flag ? "保存成功" : "保存失败"; } catch (Exception ex) { base.IsSuccess = false; base.ReturnMsg = "保存异常"; } }