Ejemplo n.º 1
0
 //入口方法
 static void Main(string[] args)
 {
     try
     {
         NetTcpBinding binding = new NetTcpBinding();
         using (Message message = Message.CreateMessage(binding.MessageVersion, "sendMessage", "Message Body"))
         {
             //创建ChannelFactory
             IChannelFactory <IDuplexChannel> factory = binding.BuildChannelFactory <IDuplexChannel>(new BindingParameterCollection());
             factory.Open();
             //这里创建IRequestChannel
             IDuplexChannel duplexChannel = factory.CreateChannel(new EndpointAddress("net.tcp://localhost:9090/DuplexService/Point2"));
             duplexChannel.Open();
             //发送消息
             duplexChannel.Send(message);
             Console.WriteLine("已经成功发送消息!");
             //var msg = duplexChannel.Receive();
             //Console.WriteLine($"收到消息:{msg.GetBody<string>()}");
             duplexChannel.Close();
             factory.Close();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
     finally
     {
         Console.Read();
     }
 }
    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_Async_Tcp_NetTcpBinding()
    {
        IChannelFactory <IDuplexSessionChannel> factory = null;
        IDuplexSessionChannel channel = null;
        Message replyMessage          = null;

        try
        {
            // *** SETUP *** \\
            NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);

            // Create the channel factory
            factory = binding.BuildChannelFactory <IDuplexSessionChannel>(new BindingParameterCollection());
            Task.Factory.FromAsync(factory.BeginOpen, factory.EndOpen, TaskCreationOptions.None).GetAwaiter().GetResult();

            // Create the channel.
            channel = factory.CreateChannel(new EndpointAddress(Endpoints.Tcp_NoSecurity_Address));
            Task.Factory.FromAsync(channel.BeginOpen, channel.EndOpen, TaskCreationOptions.None).GetAwaiter().GetResult();

            // 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.
            Task.Factory.FromAsync((asyncCallback, o) => channel.BeginSend(requestMessage, asyncCallback, o),
                                   channel.EndSend,
                                   TaskCreationOptions.None).GetAwaiter().GetResult();
            replyMessage = Task.Factory.FromAsync(channel.BeginReceive, channel.EndReceive, TaskCreationOptions.None).GetAwaiter().GetResult();

            // *** 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();
            Task.Factory.FromAsync(channel.BeginClose, channel.EndClose, TaskCreationOptions.None).GetAwaiter().GetResult();
            Task.Factory.FromAsync(factory.BeginClose, factory.EndClose, TaskCreationOptions.None).GetAwaiter().GetResult();
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects(channel, factory);
        }
    }
Ejemplo n.º 4
0
        static TcpClient()
        {
            binding = new NetTcpBinding();
            binding.Security.Mode = SecurityMode.None;

            CreateChannel =
                uri => {
                IChannelFactory <TChannel> factory = binding.BuildChannelFactory <TChannel>();
                factory.Open();
                TChannel channel = factory.CreateChannel(new EndpointAddress(uri));
                channel.Open();
                return(channel);
            };
        }
Ejemplo n.º 5
0
 public MainWindow()
 {
     InitializeComponent();
     try
     {
         binding = new NetTcpBinding(); //创建绑定
         factory = binding.BuildChannelFactory <IDuplexChannel>(new BindingParameterCollection());
         factory.Open();                //打开通道工厂
                                        //再这里创建IRequestChannel
         duplexChannel = factory.CreateChannel(new EndpointAddress("net.tcp://localhost:9090/DuplexService/Pouint2"));
         duplexChannel.Open();          //打开通道
     }
     catch (Exception ex)
     { }
 }
        public IChannelFactoryPage()
        {
            InitializeComponent();
            //匿名管道是单项的。
            //命名管道是双向的。
            //IChannelFactory<T>接口一般负责创建并管理通道栈。大家会使用ClientBase<T>来代替IChannelFactory<T>.
            //IChannelFactory需要负责关闭通道栈,IChannelListner不需要关闭通道栈。。可以独立于它的通道栈而关闭,
            binding = new NetTcpBinding();//创建绑定
            IChannelFactory <IRequestChannel> factory = binding.BuildChannelFactory <IRequestChannel>(new BindingParameterCollection());

            factory.Open();//打开ChannelFactory
            //这里创建IRequestChannel
            IRequestChannel requestChannel = factory.CreateChannel(new EndpointAddress("http://localhost:9090/RequestReplyService"));

            requestChannel.Open();//打开IRequestChannel //请求通道
            //通道工厂。用于连接Listner。
        }
Ejemplo n.º 7
0
        private static void SendMessages()
        {
            var binding = new NetTcpBinding();

            binding.Security.Mode = SecurityMode.None;
            var factory = binding.BuildChannelFactory <IDuplexSessionChannel>(binding);

            factory.Open();
            var proxy = factory.CreateChannel(new EndpointAddress(address));

            proxy.Open();
            var msg = "Hello";

            proxy.Send(Message.CreateMessage(version, "Test", msg));
            var message = proxy.Receive();

            Assert.AreEqual("Echo:" + msg, message.GetBody <string>());
        }