private async void PutData(string data, RequestType recType) { ServiceContext Context = new ServiceContext(); InputStatisticMessage Message = new InputStatisticMessage(); Message.Detail = data; Message.RequestType = recType; Message.ServerName = ServerName.USERS; Message.Time = DateTime.Now; Message.State = Guid.NewGuid(); try { Context.InputMessage.Add(Message); await Context.SaveChangesAsync(); } catch { return; } }
private async void backwork() { StatisticContext db2 = new StatisticContext(); MessageQueue InputQueue; if (MessageQueue.Exists(@".\private$\InputStatistic")) { InputQueue = new MessageQueue(@".\private$\InputStatistic"); } else { InputQueue = MessageQueue.Create(".\\private$\\InputStatistic"); } MessageQueue OutputQueue; if (MessageQueue.Exists(@".\private$\OutputStatistic")) { OutputQueue = new MessageQueue(@".\private$\OutputStatistic"); } else { OutputQueue = MessageQueue.Create(".\\private$\\OutputStatistic"); } using (InputQueue) { Statistic.Models.Statistic statistic = new Statistic.Models.Statistic(); InputStatisticMessage InputMessage = new InputStatisticMessage(); OutputStatisticMessage OutputMessage = new OutputStatisticMessage(); Message msg = new Message(); InputQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(InputStatisticMessage) }); OutputQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(OutputStatisticMessage) }); while (InputQueue.CanRead) { Message msgInput = InputQueue.Receive(); InputMessage = (InputStatisticMessage)msgInput.Body; statistic.RequestType = InputMessage.RequestType; statistic.ServerName = InputMessage.ServerName; statistic.Time = InputMessage.Time; statistic.Detail = InputMessage.Detail; statistic.State = InputMessage.State; try { db2.Statistics.Add(statistic); await db2.SaveChangesAsync(); } catch (DbUpdateException ex) { OutputStatisticMessage OutputMessageCatch = new OutputStatisticMessage(); OutputMessageCatch.Status = -1; OutputMessageCatch.Error = "Error in Input Queue"; OutputMessageCatch.Message = InputMessage; Message Msg = new Message(OutputMessageCatch); switch (OutputMessageCatch.Message.ServerName) { case ServerName.AUTHENTICATION: Msg.Label = "NON_AUTH"; break; case ServerName.GATEWAY: Msg.Label = "NON_GATEWAY"; break; case ServerName.USERS: Msg.Label = "NON_USERS"; break; case ServerName.MACHINES: Msg.Label = "NON_MACHINES"; break; case ServerName.FINES: Msg.Label = "NON_FINES"; break; } OutputQueue.Send(Msg); continue; } OutputMessage.Status = 0; OutputMessage.Error = ""; OutputMessage.Message = InputMessage; msg.Body = OutputMessage; switch (OutputMessage.Message.ServerName) { case ServerName.AUTHENTICATION: msg.Label = "NON_AUTH"; break; case ServerName.GATEWAY: msg.Label = "NON_GATEWAY"; break; case ServerName.USERS: msg.Label = "NON_USERS"; break; case ServerName.MACHINES: msg.Label = "NON_MACHINES"; break; case ServerName.FINES: msg.Label = "NON_FINES"; break; } OutputQueue.Send(msg); } } }