Esempio n. 1
0
        private void HandleAck(HttpListenerContext ctx, CallInfo callInfo)
        {
            Logger.Debug("Received message ack for id: " + callInfo.ClientId);

            var msg = new TransportMessage {
                ReturnAddress = inputQueue
            };

            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TimeSpan.FromSeconds(30)
            }))
            {
                var p = new Persistence {
                    ConnectionString = connString
                };

                byte[] outMessage;
                NameValueCollection outHeaders;

                p.AckMessage(callInfo.ClientId, Convert.FromBase64String(callInfo.MD5), out outMessage, out outHeaders);

                if (outHeaders != null && outMessage != null)
                {
                    msg.Body = outMessage;

                    HeaderMapper.Map(outHeaders, msg);
                    if (msg.TimeToBeReceived < TimeSpan.FromSeconds(1))
                    {
                        msg.TimeToBeReceived = TimeSpan.FromSeconds(1);
                    }

                    msg.Recoverable = true;

                    if (String.IsNullOrEmpty(msg.IdForCorrelation))
                    {
                        msg.IdForCorrelation = msg.Id;
                    }

                    if (msg.MessageIntent == MessageIntentEnum.Init) // wasn't set by client
                    {
                        msg.MessageIntent = MessageIntentEnum.Send;
                    }

                    if (ctx.Request.Headers[HttpHeaders.FromKey] != null)
                    {
                        msg.Headers.Add(Headers.HttpFrom, ctx.Request.Headers[HttpHeaders.FromKey]);
                    }

                    string routeTo = Headers.RouteTo.Replace(HeaderMapper.NServiceBus + Headers.HeaderName + ".", "");
                    string destination;
                    if (msg.Headers.ContainsKey(routeTo))
                    {
                        destination = msg.Headers[routeTo];
                    }
                    else
                    {
                        destination = destinationQueue;
                    }

                    Logger.Info("Sending message to " + destination);

                    messageSender.Send(msg, destination);
                }

                scope.Complete();
            }

            ReportSuccess(ctx);

            notifier.RaiseMessageProcessed(TransportTypeEnum.FromHttpToMsmq, msg);
        }
Esempio n. 2
0
        public void Handle(TransportMessage msg, string remoteUrl)
        {
            var address = remoteUrl;
            var headers = new WebHeaderCollection();

            if (msg.Headers.ContainsKey(NServiceBus.Headers.HttpTo))
            {
                address = msg.Headers[NServiceBus.Headers.HttpTo];
            }

            var request = WebRequest.Create(address);

            request.Method = "POST";

            var buffer = msg.Body;

            request.ContentType = "application/x-www-form-urlencoded";

            if (!String.IsNullOrEmpty(msg.IdForCorrelation))
            {
                msg.IdForCorrelation = msg.Id;
            }

            HeaderMapper.Map(msg, headers);

            string hash = Hasher.Hash(buffer);

            headers[HttpHeaders.ContentMd5Key] = hash;
            headers["NServiceBus.Gateway"]     = "true";
            headers[HttpHeaders.FromKey]       = from;
            headers[HeaderMapper.NServiceBus + HeaderMapper.CallType] = Enum.GetName(typeof(CallType), CallType.Submit);

            request.Headers       = headers;
            request.ContentLength = buffer.Length;

            var stream = request.GetRequestStream();

            stream.Write(buffer, 0, buffer.Length);

            Logger.Debug("Sending message to: " + address);

            var response   = request.GetResponse() as HttpWebResponse;
            var statusCode = (int)response.StatusCode;

            response.Close();

            Logger.Debug("Got HTTP response with status code " + statusCode);


            if (statusCode == 200)
            {
                Logger.Debug("Message transferred successfully. Going to acknowledge.");

                var ack = WebRequest.Create(address);
                ack.Method      = "POST";
                ack.ContentType = "application/x-www-form-urlencoded";
                ack.Headers     = headers;
                ack.Headers[HeaderMapper.NServiceBus + HeaderMapper.CallType] = Enum.GetName(typeof(CallType), CallType.Ack);
                ack.ContentLength = 0;

                Logger.Debug("Sending ack to: " + address);

                var ackResponse = ack.GetResponse() as HttpWebResponse;
                var ackCode     = (int)ackResponse.StatusCode;
                response.Close();

                Logger.Debug("Got HTTP response with status code " + ackCode);

                if (ackCode != 200)
                {
                    Logger.Info("Ack not transferred successfully. Trying again...");
                    throw new Exception("Retrying");
                }

                notifier.RaiseMessageProcessed(TransportTypeEnum.FromMsmqToHttp, msg);
            }
            else
            {
                Logger.Info("Message not transferred successfully. Trying again...");
                throw new Exception("Retrying");
            }
        }