Esempio n. 1
0
        void RunTest()
        {
            listener.Open();
            Thread thread = new Thread(ServerThread);

            thread.Start(this);

            CustomBinding binding = new CustomBinding(new WseTcpTransportBindingElement());
            IChannelFactory <IDuplexSessionChannel> channelFactory = binding.BuildChannelFactory <IDuplexSessionChannel>();

            channelFactory.Open();
            IDuplexSessionChannel channel = channelFactory.CreateChannel(new EndpointAddress(this.uri));
            Message requestMessage;

            channel.Open();
            requestMessage = Message.CreateMessage(binding.MessageVersion, "http://SayHello", "to you.");
            channel.Send(requestMessage);
            Message hello = channel.Receive();

            using (hello)
            {
                Console.WriteLine(hello.GetBody <string>());
            }
            Console.WriteLine("Press enter.");
            Console.ReadLine();
            requestMessage = Message.CreateMessage(binding.MessageVersion, "http://NotHello", "to me.");
            channel.Send(requestMessage);
            channel.Close();
            thread.Join();

            channelFactory.Close();
            listener.Close();
            Console.WriteLine("Press enter.");
            Console.ReadLine();
        }
            protected override void OnSend(Message message, TimeSpan timeout)
            {
                TimeoutHelper         timeoutHelper        = new TimeoutHelper(timeout);
                ChannelPoolKey        key                  = null;
                bool                  isConnectionFromPool = true;
                IDuplexSessionChannel channel              = this.GetChannelFromPool(ref timeoutHelper, out key, out isConnectionFromPool);
                bool                  flag2                = false;

                try
                {
                    if (!isConnectionFromPool)
                    {
                        this.StampInitialMessage(message);
                        channel.Open(timeoutHelper.RemainingTime());
                        this.StartBackgroundReceive(channel);
                    }
                    channel.Send(message, timeoutHelper.RemainingTime());
                    flag2 = true;
                }
                finally
                {
                    if (!flag2)
                    {
                        this.CleanupChannel(channel, false, key, isConnectionFromPool, ref timeoutHelper);
                    }
                }
                this.CleanupChannel(channel, true, key, isConnectionFromPool, ref timeoutHelper);
            }
Esempio n. 3
0
            protected override void OnSend(Message message, TimeSpan timeout)
            {
                TimeoutHelper         timeoutHelper        = new TimeoutHelper(timeout);
                ChannelPoolKey        key                  = null;
                bool                  isConnectionFromPool = true;
                IDuplexSessionChannel innerChannel         =
                    GetChannelFromPool(ref timeoutHelper, out key, out isConnectionFromPool);

                bool success = false;

                try
                {
                    if (!isConnectionFromPool)
                    {
                        StampInitialMessage(message);
                        innerChannel.Open(timeoutHelper.RemainingTime());
                        StartBackgroundReceive(innerChannel);
                    }

                    innerChannel.Send(message, timeoutHelper.RemainingTime());
                    success = true;
                }
                finally
                {
                    if (!success)
                    {
                        CleanupChannel(innerChannel, false, key, isConnectionFromPool, ref timeoutHelper);
                    }
                }

                CleanupChannel(innerChannel, true, key, isConnectionFromPool, ref timeoutHelper);
            }
 private static NetworkDetector.ConnectivityStatus CheckTcpConnectivity(Uri baseAddress, out Exception exception)
 {
     NetworkDetector.ConnectivityStatus connectivityStatu = NetworkDetector.ConnectivityStatus.Unavailable;
     exception = null;
     if (!RelayEnvironment.GetEnvironmentVariable("RELAYFORCEHTTP", false) && !RelayEnvironment.GetEnvironmentVariable("RELAYFORCEHTTPS", false))
     {
         try
         {
             BinaryMessageEncodingBindingElement binaryMessageEncodingBindingElement = new BinaryMessageEncodingBindingElement();
             TcpTransportBindingElement          tcpTransportBindingElement          = new TcpTransportBindingElement();
             tcpTransportBindingElement.ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint = 100;
             tcpTransportBindingElement.MaxReceivedMessageSize = (long)65536;
             CustomBinding customBinding = new CustomBinding();
             customBinding.Elements.Add(binaryMessageEncodingBindingElement);
             customBinding.Elements.Add(tcpTransportBindingElement);
             customBinding.OpenTimeout    = TimeSpan.FromSeconds(10);
             customBinding.SendTimeout    = TimeSpan.FromSeconds(10);
             customBinding.ReceiveTimeout = TimeSpan.MaxValue;
             int num = 9350;
             Uri uri = ServiceBusUriHelper.CreateServiceUri("net.tcp", string.Concat(baseAddress.DnsSafeHost, ":", num.ToString(CultureInfo.InvariantCulture)), "/");
             IChannelFactory <IDuplexSessionChannel> channelFactory = null;
             IDuplexSessionChannel duplexSessionChannel             = null;
             try
             {
                 channelFactory = customBinding.BuildChannelFactory <IDuplexSessionChannel>(new object[0]);
                 channelFactory.Open();
                 duplexSessionChannel = channelFactory.CreateChannel(new EndpointAddress(uri, new AddressHeader[0]));
                 duplexSessionChannel.Open();
                 Message message = Message.CreateMessage(MessageVersion.Default, "http://schemas.microsoft.com/netservices/2009/05/servicebus/connect/OnewayPing", new OnewayPingMessage());
                 duplexSessionChannel.Send(message, customBinding.SendTimeout);
                 duplexSessionChannel.Close();
                 duplexSessionChannel = null;
                 channelFactory.Close();
                 channelFactory = null;
             }
             finally
             {
                 if (duplexSessionChannel != null)
                 {
                     duplexSessionChannel.Abort();
                 }
                 if (channelFactory != null)
                 {
                     channelFactory.Abort();
                 }
             }
             connectivityStatu = NetworkDetector.ConnectivityStatus.Available;
         }
         catch (CommunicationException communicationException)
         {
             exception = communicationException;
         }
         catch (TimeoutException timeoutException)
         {
             exception = timeoutException;
         }
     }
     NetworkDetector.LogResult(baseAddress, "Tcp", connectivityStatu);
     return(connectivityStatu);
 }
    public static void IDuplexSessionChannel_Tcp_NetTcpBinding()
    {
        StringBuilder errorBuilder = new StringBuilder();

        try
        {
            NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);

            // Create the channel factory
            IChannelFactory <IDuplexSessionChannel> factory =
                binding.BuildChannelFactory <IDuplexSessionChannel>(
                    new BindingParameterCollection());
            factory.Open();

            // Create the channel.
            IDuplexSessionChannel channel = factory.CreateChannel(
                new EndpointAddress(Endpoints.Tcp_NoSecurity_Address));
            channel.Open();

            // Create the Message object to send to the service.
            Message requestMessage = Message.CreateMessage(
                binding.MessageVersion,
                action,
                new CustomBodyWriter(clientMessage));
            requestMessage.Headers.MessageId = new UniqueId(Guid.NewGuid());

            // Send the Message and receive the Response.
            channel.Send(requestMessage);
            Message replyMessage = channel.Receive(TimeSpan.FromSeconds(5));

            // If the incoming Message did not contain the same UniqueId used for the MessageId of the outgoing Message we would have received a Fault from the Service
            if (!String.Equals(replyMessage.Headers.RelatesTo.ToString(), requestMessage.Headers.MessageId.ToString()))
            {
                errorBuilder.AppendLine(String.Format("The MessageId of the incoming Message does not match the MessageId of the outgoing Message, expected: {0} but got: {1}", requestMessage.Headers.MessageId, replyMessage.Headers.RelatesTo));
            }

            // Validate the Response
            var    replyReader      = replyMessage.GetReaderAtBodyContents();
            string actualResponse   = replyReader.ReadElementContentAsString();
            string expectedResponse = "[client] This is my request.[service] Request received, this is my Reply.";
            if (!string.Equals(actualResponse, expectedResponse))
            {
                errorBuilder.AppendLine(String.Format("Actual MessageBodyContent from service did not match the expected MessageBodyContent, expected: {0} actual: {1}", expectedResponse, actualResponse));
            }

            replyMessage.Close();
            channel.Close();
            factory.Close();
        }

        catch (Exception ex)
        {
            errorBuilder.AppendLine(String.Format("Unexpected exception was caught: {0}", ex.ToString()));
        }

        Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: CustomBindingTest FAILED with the following errors: {0}", errorBuilder));
    }
    public static void IDuplexSessionChannel_Https_NetHttpsBinding()
    {
        IChannelFactory <IDuplexSessionChannel> factory = null;
        IDuplexSessionChannel channel = null;
        Message replyMessage          = null;

        try
        {
            // *** SETUP *** \\
            NetHttpsBinding binding = new NetHttpsBinding(BasicHttpsSecurityMode.Transport);

            // Create the channel factory
            factory = binding.BuildChannelFactory <IDuplexSessionChannel>(new BindingParameterCollection());
            factory.Open();

            // Create the channel.
            channel = factory.CreateChannel(new EndpointAddress(Endpoints.HttpBaseAddress_NetHttpsWebSockets));
            channel.Open();

            // Create the Message object to send to the service.
            Message requestMessage = Message.CreateMessage(
                binding.MessageVersion,
                action,
                new CustomBodyWriter(clientMessage));
            requestMessage.Headers.MessageId = new UniqueId(Guid.NewGuid());

            // *** EXECUTE *** \\
            // Send the Message and receive the Response.
            channel.Send(requestMessage);
            replyMessage = channel.Receive(TimeSpan.FromSeconds(5));

            // *** VALIDATE *** \\
            // If the incoming Message did not contain the same UniqueId used for the MessageId of the outgoing Message we would have received a Fault from the Service
            string expectedMessageID = requestMessage.Headers.MessageId.ToString();
            string actualMessageID   = replyMessage.Headers.RelatesTo.ToString();
            Assert.True(String.Equals(expectedMessageID, actualMessageID), String.Format("Expected Message ID was {0}. Actual was {1}", expectedMessageID, actualMessageID));

            // Validate the Response
            var    replyReader      = replyMessage.GetReaderAtBodyContents();
            string actualResponse   = replyReader.ReadElementContentAsString();
            string expectedResponse = "[client] This is my request.[service] Request received, this is my Reply.";
            Assert.Equal(expectedResponse, actualResponse);

            // *** CLEANUP *** \\
            replyMessage.Close();
            channel.Session.CloseOutputSession();
            channel.Close();
            factory.Close();
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects(channel, factory);
        }
    }
Esempio n. 7
0
        void CreateProxies()
        {
            m_Proxies = new Dictionary <string, IDuplexSessionChannel>();

            foreach (ServiceEndpoint endpoint in Description.Endpoints)
            {
                IDuplexSessionChannel channel = m_Factory.CreateChannel(endpoint.Address);
                channel.Open();

                m_Proxies[endpoint.Contract.Name] = channel;
            }
        }
Esempio n. 8
0
        public static void Main()
        {
            CustomBinding binding = new CustomBinding();

            binding.Elements.Add(new TextMessageEncodingBindingElement());
            binding.Elements.Add(new TcpTransportBindingElement());

            BindingParameterCollection bpcol =
                new BindingParameterCollection();

//			using (IChannelListener<IDuplexSessionChannel> listener =
//				binding.BuildChannelListener<IDuplexSessionChannel> (
//					new Uri ("net.tcp://localhost/Server"), bpcol))
//			{
            IChannelListener <IDuplexSessionChannel> listener =
                binding.BuildChannelListener <IDuplexSessionChannel> (
                    new Uri("net.tcp://localhost/"), bpcol);

            listener.Open();

            IDuplexSessionChannel channel =
                listener.AcceptChannel();

            Console.WriteLine("Listening for messages...");

            channel.Open();

            Message message = channel.Receive();

            Console.WriteLine("Message received.");
            Console.WriteLine("Message action: {0}",
                              message.Headers.Action);
            Console.WriteLine("Message content: {0}",
                              message.GetBody <string> ());

            message = Message.CreateMessage(
                //channel.Manager.MessageVersion,
                MessageVersion.Default,
                "Action", "Hello, World, from service side");

            channel.Send(message);

            message.Close();
            channel.Close();
            listener.Close();
//			}
        }
Esempio n. 9
0
        public static void Main()
        {
            Console.WriteLine("Press ENTER when service is ready.");
            Console.ReadLine();

            CustomBinding binding = new CustomBinding();

            binding.Elements.Add(new TextMessageEncodingBindingElement());
            binding.Elements.Add(new TcpTransportBindingElement());

            BindingParameterCollection bpcol =
                new BindingParameterCollection();

//			using (IChannelFactory<IDuplexSessionChannel> factory =
//			binding.BuildChannelFactory<IDuplexSessionChannel>(bpcol))
//			{
            IChannelFactory <IDuplexSessionChannel> factory =
                binding.BuildChannelFactory <IDuplexSessionChannel> (bpcol);

            factory.Open();

            IDuplexSessionChannel channel = factory.CreateChannel(
                new EndpointAddress("net.tcp://localhost/"));

            channel.Open();

            Message message = Message.CreateMessage(
                //channel.Manager.MessageVersion,
                MessageVersion.Default,
                "Action", "Hello, World, from client side");

            channel.Send(message);

            message = channel.Receive();

            Console.WriteLine("Message received.");
            Console.WriteLine("Message action: {0}",
                              message.Headers.Action);
            Console.WriteLine("Message content: {0}",
                              message.GetBody <string> ());

            message.Close();
            channel.Close();
            factory.Close();
//			}
        }
Esempio n. 10
0
        void Dispatch(Message message)
        {
            string contract = ExtractContract(message);

            if (contract == null)
            {
                return;
            }
            try
            {
                m_Proxies[contract].Send(message);
            }
            catch
            {
                m_Proxies[contract].Abort();
                IDuplexSessionChannel channel = m_Factory.CreateChannel(m_Proxies[contract].RemoteAddress);
                channel.Open();
                m_Proxies[contract] = channel;
            }
        }
Esempio n. 11
0
        private void ReceiveInner(CancellationToken token)
        {
            NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
            IChannelListener <IDuplexSessionChannel> listener = binding.BuildChannelListener <IDuplexSessionChannel>(new Uri("net.pipe://localhost/" + this.name));

            listener.Open();
            IDuplexSessionChannel channel = null;

            token.Register(l => ((ICommunicationObject)l).Abort(), listener);
            try
            {
                channel = listener.AcceptChannel();
                token.Register(c => ((ICommunicationObject)c).Abort(), channel);
                channel.Open();

                bool sessionClosed = false;
                do
                {
                    using (Message message = channel.Receive())
                    {
                        if (message != null)
                        {
                            this.OnReceived();
                        }
                        else
                        {
                            sessionClosed = true;
                        }
                    }
                }while (!sessionClosed);
            }
            catch (CommunicationException)
            {
            }
            catch (TimeoutException)
            {
            }
        }
Esempio n. 12
0
        void RunServer()
        {
            IDuplexSessionChannel channel = listener.AcceptChannel(TimeSpan.MaxValue);

            channel.Open();
            Message message;

            while ((message = channel.Receive(TimeSpan.MaxValue)) != null)
            {
                string action = message.Headers.Action;
                string body   = message.GetBody <string>();
                Console.WriteLine("Received Action: " + action);
                Console.WriteLine("Received Body: " + body);

                if (action == "http://SayHello")
                {
                    Message helloMessage = Message.CreateMessage(message.Version, "http://Hello", "Hello " + body);
                    channel.Send(helloMessage);
                }
                message.Close();
            }
            channel.Close();
        }
 protected override void OnOpen(TimeSpan timeout)
 {
     innerChannel.Open(timeout);
 }
    public static void IDuplexSessionChannel_Http_BasicHttpBinding()
    {
#if FULLXUNIT_NOTSUPPORTED
        bool root_Certificate_Installed = Root_Certificate_Installed();
        if (!root_Certificate_Installed)
        {
            Console.WriteLine("---- Test SKIPPED --------------");
            Console.WriteLine("Attempting to run the test in ToF, a ConditionalFact evaluated as FALSE.");
            Console.WriteLine("Root_Certificate_Installed evaluated as {0}", root_Certificate_Installed);
            return;
        }
#endif
        IChannelFactory <IDuplexSessionChannel> factory = null;
        IDuplexSessionChannel channel = null;
        Message replyMessage          = null;

        try
        {
            // *** SETUP *** \\
            BasicHttpBinding binding = new BasicHttpBinding();

            // Create the channel factory
            factory = binding.BuildChannelFactory <IDuplexSessionChannel>(new BindingParameterCollection());
            factory.Open();

            // Create the channel.
            channel = factory.CreateChannel(new EndpointAddress(Endpoints.HttpBaseAddress_NetHttps));
            channel.Open();

            // Create the Message object to send to the service.
            Message requestMessage = Message.CreateMessage(
                binding.MessageVersion,
                action,
                new CustomBodyWriter(clientMessage));
            requestMessage.Headers.MessageId = new UniqueId(Guid.NewGuid());

            // *** EXECUTE *** \\
            // Send the Message and receive the Response.
            channel.Send(requestMessage);
            replyMessage = channel.Receive(TimeSpan.FromSeconds(5));

            // *** VALIDATE *** \\
            // If the incoming Message did not contain the same UniqueId used for the MessageId of the outgoing Message we would have received a Fault from the Service
            Assert.Equal(requestMessage.Headers.MessageId.ToString(), replyMessage.Headers.RelatesTo.ToString());

            // Validate the Response
            var    replyReader      = replyMessage.GetReaderAtBodyContents();
            string actualResponse   = replyReader.ReadElementContentAsString();
            string expectedResponse = "[client] This is my request.[service] Request received, this is my Reply.";
            Assert.Equal(expectedResponse, actualResponse);

            // *** CLEANUP *** \\
            replyMessage.Close();
            channel.Close();
            factory.Close();
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects(channel, factory);
        }
    }