示例#1
0
 private void GetProductFromRabbitMQ()
 {
     Task.Run(() =>
     {
         var queueName = "product";
         var rabbitMQ  = Configuration.GetValue <string>("RabbitMQHost");
         var factory   = new ConnectionFactory()
         {
             HostName = rabbitMQ
         };
         using (var connection = factory.CreateConnection())
         {
             using (var channel = connection.CreateModel())
             {
                 channel.QueueDeclare(queue: queueName,
                                      durable: false,
                                      exclusive: false,
                                      autoDelete: false,
                                      arguments: null);
                 byte[] body = null;
                 //using (var memorystream = new MemoryStream())
                 //{
                 //    var bf = new BinaryFormatter();
                 //    bf.Serialize(memorystream, product);
                 //    body = memorystream.ToArray();
                 //}
                 var consumer       = new EventingBasicConsumer(channel);
                 consumer.Received += async(model, ea) =>
                 {
                     var body                = ea.Body;
                     var message             = Encoding.UTF8.GetString(body);
                     var sqlConnectionString = Configuration.GetConnectionString("CatelogService");
                     var dboptions           = new DbContextOptionsBuilder <CatelogDbContext>().UseSqlServer(sqlConnectionString).Options;
                     var catelogDbContext    = new CatelogDbContext(dboptions);
                     var product             = JsonConvert.DeserializeObject <Product>(message);
                     if (product != null)
                     {
                         catelogDbContext.Add(new Product
                         {
                             Name  = product.Name,
                             Price = product.Price
                         });
                         await catelogDbContext.SaveChangesAsync();
                     }
                 };
                 channel.BasicConsume(queue: queueName,
                                      autoAck: true,
                                      consumer: consumer);
                 while (true)
                 {
                     Thread.Sleep(1000);
                 }
             }
         }
     });
 }
 public AccountController(CatelogDbContext context)
 {
     _context = context;
 }
 public LocalTenantCreation(CatelogDbContext catelogDbContext)
 {
     this.catelogDbContext = catelogDbContext;
 }
示例#4
0
 public CatelogController(CatelogDbContext catelogDbContext)
 {
     this.catelogDbContext = catelogDbContext;
 }