private Message PrepareReply(Message message)
            {
                string str;

                if (message == null)
                {
                    return(this.CreateAckMessage(HttpStatusCode.Accepted, string.Empty));
                }
                if (!this.isSoapRequest)
                {
                    if (message.Headers.Action == null)
                    {
                        str = (this.requestAction == null ? string.Empty : string.Concat(this.requestAction, "Response"));
                    }
                    else
                    {
                        str = string.Concat(message.Headers.Action, "Response");
                    }
                    return(RelayedHttpUtility.ConvertWebResponseToSoapResponse(message, str));
                }
                if (this.encoder.MessageVersion.Addressing == AddressingVersion.None)
                {
                    message.Headers.Action    = null;
                    message.Headers.To        = null;
                    message.Headers.ReplyTo   = null;
                    message.Headers.From      = null;
                    message.Headers.FaultTo   = null;
                    message.Headers.MessageId = null;
                    message.Headers.RelatesTo = null;
                }
                return(message.ConvertSoapResponseToWrappedSoapResponse(message.Headers.Action, this.wrapper));
            }
        public RelayedOnewayHttpSender(BindingContext context, Uri uri, bool transportProtectionEnabled)
        {
            Binding wSHttpBinding;

            this.tokenProvider = TokenProviderUtility.CreateTokenProvider(context);
            if (this.tokenProvider != null)
            {
                transportProtectionEnabled = true;
            }
            if (!transportProtectionEnabled)
            {
                wSHttpBinding = new WSHttpBinding(SecurityMode.None);
                this.via      = RelayedHttpUtility.ConvertToHttpUri(uri);
            }
            else
            {
                wSHttpBinding = new WSHttpBinding(SecurityMode.Transport);
                this.via      = RelayedHttpUtility.ConvertToHttpsUri(uri);
            }
            this.channelFactory = new ChannelFactory <IOutputChannel>(wSHttpBinding);
            this.channelFactory.Open();
            this.channel = this.channelFactory.CreateChannel(new EndpointAddress(uri, new AddressHeader[0]), this.via);
            this.channel.Open();
            this.OnOnline(this, EventArgs.Empty);
        }
        public Microsoft.ServiceBus.Channels.IConnection Connect(Uri uri, TimeSpan timeout)
        {
            EventTraceActivity eventTraceActivity = new EventTraceActivity();
            TimeoutHelper      timeoutHelper      = new TimeoutHelper(timeout);
            WebStream          webStream          = (new WebStream(uri, "connection", this.useHttpsMode, eventTraceActivity, uri)).Open();

            Microsoft.ServiceBus.Channels.IConnection webStreamConnection = new WebStreamConnection(uri, this.bufferSize, eventTraceActivity, webStream, uri);
            webStreamConnection = SecureSocketUtil.InitiateSecureClientUpgradeIfNeeded(webStreamConnection, null, this.socketSecurityRole, uri.Host, timeoutHelper.RemainingTime());
            SocketMessageHelper socketMessageHelper = new SocketMessageHelper();
            Message             message             = Message.CreateMessage(socketMessageHelper.MessageVersion, "RelayedConnect", new ConnectMessage(uri));

            TrackingIdHeader.TryAddOrUpdate(message.Headers, eventTraceActivity.ActivityId.ToString());
            if (this.tokenProvider != null)
            {
                string        absoluteUri = RelayedHttpUtility.ConvertToHttpUri(uri).AbsoluteUri;
                SecurityToken token       = this.tokenProvider.GetToken(absoluteUri, "Send", false, timeoutHelper.RemainingTime());
                message.Headers.Add(new RelayTokenHeader(token));
            }
            socketMessageHelper.SendMessage(webStreamConnection, message, timeoutHelper.RemainingTime());
            Message message1 = socketMessageHelper.ReceiveMessage(webStreamConnection, timeoutHelper.RemainingTime());

            if (message1.IsFault)
            {
                throw ErrorUtility.ConvertToError(MessageFault.CreateFault(message1, 65536));
            }
            return(webStreamConnection);
        }
 private Message PrepareRequest(Message message)
 {
     if (message.Headers.FindHeader("BodyFormat", "http://schemas.microsoft.com/netservices/2009/05/servicebus/body") == -1)
     {
         this.isSoapRequest = true;
         return(message);
     }
     return(RelayedHttpUtility.ConvertSoapRequestToWebRequest(this.encoder, message, out this.isSoapRequest, this.wrapper, this.preserveRawHttp));
 }
		public Microsoft.ServiceBus.Channels.IConnection Connect(Uri uri, TimeSpan timeout)
		{
			TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
			Uri uri1 = uri;
			for (int i = 0; i < 3; i++)
			{
				EventTraceActivity eventTraceActivity = new EventTraceActivity();
				Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
				try
				{
					socket.Connect(uri1.Host, 9352);
					Microsoft.ServiceBus.Channels.IConnection socketConnection = new Microsoft.ServiceBus.Channels.SocketConnection(socket, this.bufferSize, eventTraceActivity);
					byte[] bytes = Encoding.UTF8.GetBytes(ConnectConstants.ConnectType);
					byte[] numArray = BitConverter.GetBytes((int)bytes.Length);
					socketConnection.Write(numArray, 0, (int)numArray.Length, true, timeoutHelper.RemainingTime());
					socketConnection.Write(bytes, 0, (int)bytes.Length, true, timeoutHelper.RemainingTime());
					byte[] bytes1 = Encoding.UTF8.GetBytes(uri1.ToString());
					byte[] numArray1 = BitConverter.GetBytes((int)bytes1.Length);
					socketConnection.Write(numArray1, 0, (int)numArray1.Length, true, timeoutHelper.RemainingTime());
					socketConnection.Write(bytes1, 0, (int)bytes1.Length, true, timeoutHelper.RemainingTime());
					socketConnection = SecureSocketUtil.InitiateSecureClientUpgradeIfNeeded(socketConnection, null, this.socketSecurityMode, uri.Host, timeoutHelper.RemainingTime());
					Message message = Message.CreateMessage(this.messageHelper.MessageVersion, "RelayedConnect", new ConnectMessage(uri));
					TrackingIdHeader.TryAddOrUpdate(message.Headers, eventTraceActivity.ActivityId.ToString());
					if (this.tokenProvider != null)
					{
						SecurityToken token = this.tokenProvider.GetToken(RelayedHttpUtility.ConvertToHttpUri(uri).ToString(), "Send", false, timeoutHelper.RemainingTime());
						message.Headers.Add(new RelayTokenHeader(token));
					}
					this.messageHelper.SendMessage(socketConnection, message, timeoutHelper.RemainingTime());
					Message message1 = this.messageHelper.ReceiveMessage(socketConnection, timeoutHelper.RemainingTime());
					using (message1)
					{
						if (message1.Headers.Action == "Redirect")
						{
							uri1 = message1.GetBody<RedirectMessage>().Uri;
							socket.Close();
							goto Label1;
						}
						else if (message1.IsFault)
						{
							MessageFault messageFault = MessageFault.CreateFault(message1, 65536);
							throw Fx.Exception.AsError(ErrorUtility.ConvertToError(messageFault), eventTraceActivity);
						}
					}
					return socketConnection;
				}
				catch
				{
					socket.Close();
					throw;
				}
			Label1:
			}
			throw Fx.Exception.AsError(new CommunicationException(SRClient.MaxRedirectsExceeded(3)), null);
		}
            private Message CreateAckMessage(HttpStatusCode statusCode, string statusDescription)
            {
                Message message = Message.CreateMessage(MessageVersion.None, "");
                HttpResponseMessageProperty httpResponseMessageProperty = new HttpResponseMessageProperty()
                {
                    StatusCode         = statusCode,
                    SuppressEntityBody = true
                };

                if (statusDescription.Length > 0)
                {
                    httpResponseMessageProperty.StatusDescription = statusDescription;
                }
                message.Properties.Add(HttpResponseMessageProperty.Name, httpResponseMessageProperty);
                return(RelayedHttpUtility.ConvertWebResponseToSoapResponse(message, string.Concat(this.requestAction, "Response")));
            }
        private void SendRelayedConnectAndReceiveResponse(ClientWebSocketConnection connection, TimeoutHelper timeoutHelper)
        {
            SocketMessageHelper socketMessageHelper = new SocketMessageHelper();
            Message             message             = Message.CreateMessage(socketMessageHelper.MessageVersion, "RelayedConnect", new ConnectMessage(connection.Uri));

            TrackingIdHeader.TryAddOrUpdate(message.Headers, connection.Activity.ActivityId.ToString());
            if (this.tokenProvider != null)
            {
                string        absoluteUri = RelayedHttpUtility.ConvertToHttpUri(connection.Uri).AbsoluteUri;
                SecurityToken token       = this.tokenProvider.GetToken(absoluteUri, "Send", false, timeoutHelper.RemainingTime());
                message.Headers.Add(new RelayTokenHeader(token));
            }
            socketMessageHelper.SendMessage(connection, message, timeoutHelper.RemainingTime());
            Message message1 = socketMessageHelper.ReceiveMessage(connection, timeoutHelper.RemainingTime());

            if (message1.IsFault)
            {
                throw ErrorUtility.ConvertToError(MessageFault.CreateFault(message1, 65536));
            }
        }
Beispiel #8
0
        private BindingContext CreateInnerListenerBindingContext(BindingContext context)
        {
            BinaryMessageEncodingBindingElement binaryMessageEncodingBindingElement = ClientMessageUtility.CreateInnerEncodingBindingElement(context);
            NameSettings nameSetting = context.BindingParameters.Find <NameSettings>();
            TcpRelayTransportBindingElement tcpRelayTransportBindingElement = new TcpRelayTransportBindingElement(this.RelayClientAuthenticationType)
            {
                ManualAddressing           = base.ManualAddressing,
                HostNameComparisonMode     = this.HostNameComparisonMode,
                MaxBufferPoolSize          = this.MaxBufferPoolSize,
                MaxBufferSize              = this.MaxBufferSize,
                MaxReceivedMessageSize     = this.MaxReceivedMessageSize,
                TransferMode               = System.ServiceModel.TransferMode.Streamed,
                TransportProtectionEnabled = nameSetting.ServiceSettings.TransportProtection != RelayTransportProtectionMode.None
            };

            tcpRelayTransportBindingElement.ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint = 100;
            tcpRelayTransportBindingElement.IsDynamic = nameSetting.ServiceSettings.IsDynamic;
            BindingElement[] bindingElementArray = new BindingElement[] { binaryMessageEncodingBindingElement, tcpRelayTransportBindingElement };
            CustomBinding    customBinding       = new CustomBinding(bindingElementArray)
            {
                CloseTimeout   = context.Binding.CloseTimeout,
                Name           = context.Binding.Name,
                Namespace      = context.Binding.Namespace,
                OpenTimeout    = context.Binding.OpenTimeout,
                ReceiveTimeout = context.Binding.ReceiveTimeout,
                SendTimeout    = context.Binding.SendTimeout
            };
            BindingParameterCollection bindingParameterCollection = new BindingParameterCollection();

            foreach (object bindingParameter in context.BindingParameters)
            {
                if (bindingParameter is MessageEncodingBindingElement)
                {
                    continue;
                }
                bindingParameterCollection.Add(bindingParameter);
            }
            Uri sbUri = RelayedHttpUtility.ConvertToSbUri(context.ListenUriBaseAddress);

            return(new BindingContext(customBinding, bindingParameterCollection, sbUri, context.ListenUriRelativeAddress, context.ListenUriMode));
        }