Esempio n. 1
0
        public IAsyncResult BeginSend(Message message, TimeSpan timeout,
                                      AsyncCallback callback, object state)
        {
            AddContextToOutgoingMessage(message);

            return(innerDuplexSessionChannel.BeginSend(message,
                                                       timeout, callback, state));
        }
    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);
        }
    }
Esempio n. 3
0
                bool SendMessage()
                {
                    IAsyncResult result = innerChannel.BeginSend(this.message, onInnerSend, this);

                    if (!result.CompletedSynchronously)
                    {
                        return(false);
                    }

                    innerChannel.EndSend(result);
                    return(true);
                }