public int DispatchOrders(Dictionary <Concentrator.Objects.Models.Orders.Order, List <OrderLine> > orderLines, Vendor vendor, IAuditLogAdapter log, IUnitOfWork unit) { try { var orders = new BasOrderExporter().GetOrder(orderLines, vendor); int msgID = 0; string ediUrl = vendor.VendorSettings.GetValueByKey("EDIUrl", string.Empty); if (string.IsNullOrEmpty(ediUrl)) { throw new Exception("Unable process orders empty EDI url for BasDispatcher"); } foreach (var order in orders) { StringBuilder requestString = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; using (XmlWriter xw = XmlWriter.Create(requestString, settings)) { xw.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""); XmlSerializer serializer = new XmlSerializer(typeof(WebOrderRequest)); XmlSerializerNamespaces nm = new XmlSerializerNamespaces(); nm.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); serializer.Serialize(xw, order, nm); XmlDocument document = new XmlDocument(); document.LoadXml(requestString.ToString()); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ediUrl); request.Method = "POST"; HttpPostState state = new HttpPostState(orderLines, order.WebOrderDetails.Select(x => x.CustomerReference), request); byte[] byteData = UTF8Encoding.UTF8.GetBytes(document.OuterXml); using (Stream s = request.GetRequestStream()) { s.Write(byteData, 0, byteData.Length); } IAsyncResult result = request.BeginGetResponse(HttpCallBack, state); result.AsyncWaitHandle.WaitOne(); if (order.WebOrderHeader != null && !string.IsNullOrEmpty(order.WebOrderHeader.CustomerOrderReference)) { LogOrder(document, vendor.VendorID, string.Format("{0}.xml", order.WebOrderHeader.WebSiteOrderNumber), log); } } } return(msgID); } catch (Exception e) { throw new Exception("Bas dispatching failed", e); } }
public static void HttpCallBack(IAsyncResult result) { try { HttpPostState state = (HttpPostState)result.AsyncState; using (HttpWebResponse httpResponse = (HttpWebResponse)state.Request.EndGetResponse(result)) { var order = (from o in state.OrderLines where o.Value[0].OrderLineID.ToString() == state.CustomerReference.Select(x => x.CustomerOrderLine).FirstOrDefault() select o.Key).FirstOrDefault(); var orderLines = state.OrderLines[order]; //(from ol in state.OrderLines // where ol.Key.OrderID.ToString() == state.CustomerReference.Select(x => x.CustomerOrder).FirstOrDefault() // select ol.Value.); switch (httpResponse.StatusCode) { case HttpStatusCode.OK: //POST OK foreach (var ol in state.CustomerReference) { var line = orderLines.Where(x => x.OrderLineID == int.Parse(ol.CustomerOrderLine)).FirstOrDefault(); if (line != null) { line.Response = httpResponse.StatusDescription; } } break; default: foreach (var ol in state.CustomerReference) { var line = orderLines.Where(x => x.OrderLineID == int.Parse(ol.CustomerOrderLine)).FirstOrDefault(); if (line != null) { line.Response = httpResponse.StatusDescription; } } throw new Exception("Error in order response"); } } } catch (Exception) { throw new Exception("Callback failed"); } }