Пример #1
0
        private void HandleAck(HttpListenerContext ctx, CallInfo callInfo)
        {
            Logger.Debug("Received message ack for id: " + callInfo.ClientId);

            var msg = new TransportMessage {
                ReturnAddress = ReturnAddress
            };

            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TimeSpan.FromSeconds(30)
            }))
            {
                byte[] outMessage;
                NameValueCollection outHeaders;

                persister.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;
                    }


                    //todo the from key isn't used for anything, remove?
                    if (ctx.Request.Headers[HttpHeaders.FromKey] != null)
                    {
                        msg.Headers.Add(Headers.HttpFrom, ctx.Request.Headers[HttpHeaders.FromKey]);
                    }


                    MessageReceived(this, new MessageForwardingArgs {
                        Message = msg
                    });
                }

                scope.Complete();
            }

            ReportSuccess(ctx);
        }
Пример #2
0
        void HandleAck(CallInfo callInfo)
        {
            var msg = new TransportMessage();

            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TimeSpan.FromSeconds(30)
            }))
            {
                byte[] outMessage;
                NameValueCollection outHeaders;

                persister.AckMessage(callInfo.ClientId, 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;
                    }


                    //todo this header is used by the httpheadermanager and need to be abstracted to support other channels
                    if (callInfo.Headers[HttpHeaders.FromKey] != null)
                    {
                        msg.Headers.Add(Headers.HttpFrom, callInfo.Headers[HttpHeaders.FromKey]);
                    }


                    MessageReceived(this, new MessageReceivedOnChannelArgs {
                        Message = msg
                    });
                }

                scope.Complete();
            }
        }
Пример #3
0
        public void Send(TransportMessage msg, string remoteUrl)
        {
            var headers = new WebHeaderCollection();

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

            HeaderMapper.Map(msg, headers);


            var buffer = msg.Body;


            var request = WebRequest.Create(remoteUrl);

            request.Method = "POST";


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


            string hash = Hasher.Hash(buffer);

            headers[HttpHeaders.ContentMd5Key] = hash;
            headers["NServiceBus.Gateway"]     = "true";

            //todo the from key isn't used for anything, remove?
            headers[HttpHeaders.FromKey] = ListenUrl;
            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: " + remoteUrl);
            int statusCode;

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

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


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

            Logger.Debug("Message transferred successfully. Going to acknowledge.");

            var ack = WebRequest.Create(remoteUrl);

            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: " + remoteUrl);

            int ackCode;

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

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

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