Ejemplo n.º 1
0
        private void HandleSubmit(HttpListenerContext ctx, CallInfo callInfo)
        {
            Logger.Debug("Received message submission for id: " + callInfo.ClientId);
            string hash = ctx.Request.Headers[HttpHeaders.ContentMd5Key];

            byte[] buffer = GetBuffer(ctx);
            string myHash = Hasher.Hash(buffer);

            if (myHash != hash)
            {
                CloseResponseAndWarn(ctx, "MD5 hash received does not match hash calculated on server. Consider resubmitting.", 412);
                return;
            }

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

                p.InsertMessage(DateTime.UtcNow, callInfo.ClientId, Convert.FromBase64String(callInfo.MD5), buffer, ctx.Request.Headers);

                scope.Complete();
            }

            ReportSuccess(ctx);
        }
Ejemplo 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");
            }
        }