コード例 #1
0
        protected override void OnClose(TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            _innerOutputChannel.Close(timeoutHelper.RemainingTime());
            base.OnClose(timeoutHelper.RemainingTime());
        }
コード例 #2
0
ファイル: Output.cs プロジェクト: gdx4430090/WCF
 //入口方法
 static void Main(string[] args)
 {
     try
     {
         //建立自定义的通道栈
         BindingElement[] bindingElements = new BindingElement[3];
         bindingElements[0] = new TextMessageEncodingBindingElement();
         //OneWayBindingElement可以使得传输通道支持数据报模式
         bindingElements[1] = new OneWayBindingElement();
         bindingElements[2] = new HttpTransportBindingElement();
         CustomBinding binding = new CustomBinding(bindingElements);
         using (Message message = Message.CreateMessage(binding.MessageVersion, "sendMessage", "Message Body"))
         {
             //创建ChannelFactory
             IChannelFactory <IOutputChannel> factory = binding.BuildChannelFactory <IOutputChannel>(new BindingParameterCollection());
             factory.Open();
             //这里创建IOutputChannel
             IOutputChannel outputChannel = factory.CreateChannel(new EndpointAddress("http://localhost/InputService"));
             outputChannel.Open();
             //发送消息
             outputChannel.Send(message);
             Console.WriteLine("已经成功发送消息!");
             outputChannel.Close();
             factory.Close();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
     finally
     {
         Console.Read();
     }
 }
コード例 #3
0
 void CompleteSend(IAsyncResult result)
 {
     try
     {
         outputChannel.EndSend(result);
         outputChannel.Close();
     }
     finally
     {
         outputChannel.Abort();
     }
 }
コード例 #4
0
            public void Send(Message message, TimeSpan timeout)
            {
                TimeoutHelper  timeoutHelper = new TimeoutHelper(timeout);
                IOutputChannel outputChannel = ValidateStateAndGetOutputChannel(message, timeoutHelper);

                try
                {
                    outputChannel.Send(message, timeoutHelper.RemainingTime());
                    outputChannel.Close(timeoutHelper.RemainingTime());
                }
                finally
                {
                    outputChannel.Abort();
                }
            }
コード例 #5
0
        private void SendMessage(object objectToSend, AmqpProperties propertiesToSend)
        {
            ChannelFactory <IOutputChannel> channelFactory =
                new ChannelFactory <IOutputChannel>(Util.GetBinding(), SendToUri);
            IOutputChannel proxy = channelFactory.CreateChannel();

            proxy.Open();

            Message toSend = Message.CreateMessage(MessageVersion.Default, string.Empty, objectToSend);

            toSend.Properties["AmqpProperties"] = propertiesToSend;
            proxy.Send(toSend);

            toSend.Close();
            proxy.Close();
            channelFactory.Close();
        }
コード例 #6
0
        static void WarmUpTransactionSubsystem(Options opts)
        {
            // see if any use of transactions is expected
            if ((opts.type == ClientType.Publisher) && (opts.pubTxSize == 0))
            {
                return;
            }

            if ((opts.type == ClientType.Subscriber) && (opts.subTxSize == 0))
            {
                return;
            }

            if (opts.type == ClientType.InteropDemo)
            {
                if ((opts.subTxSize == 0) && (opts.pubTxSize == 0))
                {
                    return;
                }
            }

            Console.WriteLine("Initializing transactions");
            IRawBodyUtility bodyUtil = new RawEncoderUtility();

            // Send a transacted message to nowhere to force the initial registration with MSDTC.
            // MSDTC insists on verifying it can contact the resource in the manner expected for
            // recovery.  This requires setting up and finishing a separate connection to the
            // broker by a thread owned by the DTC.  Excluding this time allows the existing
            // reporting mechanisms to better reflect the cost per transaction without requiring
            // long test runs.
            IOutputChannel channel = QueueChannelFactory.CreateWriterChannel("amq.direct", Guid.NewGuid().ToString());
            Message        msg     = bodyUtil.CreateMessage("sacrificial transacted message from WcfPerftest");

            using (TransactionScope ts = new TransactionScope())
            {
                channel.Send(msg);
                // abort/rollback
                ts.Dispose();
            }
            channel.Close();
            Console.WriteLine("transaction resource manager ready");
        }
コード例 #7
0
        public override void Dispatch(MessageDelivery messageDelivery)
        {
            IOutputChannel channel = CreateChannelFactory().CreateChannel();
            bool           closed  = false;

            try
            {
                channel.Open();
                channel.Send(_converter.ToMessage(messageDelivery));
            }
            finally
            {
                channel.Close();
                closed = true;
            }
            if (!closed)
            {
                channel.Abort();
            }
        }
コード例 #8
0
        public IDuplexChannel CreateChannel(EndpointAddress remoteAddress, Uri via, EndpointAddress localAddress, MessageFilter filter, int priority, bool usesUniqueHeader)
        {
            ChannelDemuxerFilter             demuxFilter        = new ChannelDemuxerFilter(new AndMessageFilter(new EndpointAddressMessageFilter(localAddress, true), filter), priority);
            IDuplexChannel                   newChannel         = null;
            IOutputChannel                   innerOutputChannel = null;
            IChannelListener <IInputChannel> innerInputListener = null;
            IInputChannel innerInputChannel = null;

            try
            {
                innerOutputChannel = this.innerChannelFactory.CreateChannel(remoteAddress, via);
                innerInputListener = this.channelDemuxer.BuildChannelListener <IInputChannel>(demuxFilter);
                innerInputListener.Open();
                innerInputChannel = innerInputListener.AcceptChannel();
                newChannel        = new ClientCompositeDuplexChannel(this, innerInputChannel, innerInputListener, localAddress, innerOutputChannel, usesUniqueHeader);
            }
            finally
            {
                if (newChannel == null) // need to cleanup
                {
                    if (innerOutputChannel != null)
                    {
                        innerOutputChannel.Close();
                    }

                    if (innerInputListener != null)
                    {
                        innerInputListener.Close();
                    }

                    if (innerInputChannel != null)
                    {
                        innerInputChannel.Close();
                    }
                }
            }

            return(newChannel);
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: ay2015/NETLearningNotes
 static void Main(string[] args)
 {
     try
     {
         //建立自定义通道栈
         BindingElement[] bindingElements = new BindingElement[3];
         bindingElements[0] = new TextMessageEncodingBindingElement();//文本编码
         //OneWayBindingElement 可以使得传输通道支持数据报模式
         bindingElements[1] = new OneWayBindingElement();
         //http传输
         //  bindingElements[2] = new HttpTransportBindingElement();
         //命名管道传输
         bindingElements[2] = new NamedPipeTransportBindingElement();
         CustomBinding binding = new CustomBinding(bindingElements);
         using (Message message = Message.CreateMessage(binding.MessageVersion, "sendMessage", "Message Body"))
         //创建消息
         {
             //创建ChannelFactory
             IChannelFactory <IOutputChannel> factory = binding.BuildChannelFactory <IOutputChannel>(new BindingParameterCollection());
             factory.Open();//打开ChannelFactory
             //创建IoutputChannel
             IOutputChannel outputChannel = factory.CreateChannel(new System.ServiceModel.EndpointAddress("net.pipe://localhost/InputService"));
             outputChannel.Open();        //打开通道
             outputChannel.Send(message); //发送消息
             Console.WriteLine("已经成功发送消息!");
             outputChannel.Close();       //关闭通道
             factory.Close();             //关闭工厂
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
     finally
     {
         Console.Read();
     }
 }
コード例 #10
0
        static void Main(string[] args)
        {
            try
            {
                ParseArgs(args);
                Console.Title = "Message Sender";

                // Get credentials as Endpoint behavior
                TransportClientEndpointBehavior securityBehavior = new TransportClientEndpointBehavior();
                securityBehavior.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(serviceBusKeyName, serviceBusKey);

                // Create factory and channel using NetMessagingBinding
                NetMessagingBinding messagingBinding = new NetMessagingBinding("messagingBinding");
                EndpointAddress     address          = SampleManager.GetEndpointAddress(SampleManager.SessionlessQueueName, serviceBusNamespace);

                IChannelFactory <IOutputChannel> messagingChannelFactory = null;
                IOutputChannel messagingOutputChannel = null;
                try
                {
                    messagingChannelFactory = messagingBinding.BuildChannelFactory <IOutputChannel>(securityBehavior);
                    messagingChannelFactory.Open();
                    messagingOutputChannel = messagingChannelFactory.CreateChannel(address);
                    messagingOutputChannel.Open();

                    // Send messages to queue which does not require session
                    Console.WriteLine("Preparing to send messages to {0}...", SampleManager.SessionlessQueueName);
                    Thread.Sleep(3000);

                    SendMessages(messagingOutputChannel, messagingBinding);
                    messagingOutputChannel.Close();
                    messagingChannelFactory.Close();
                }
                catch (Exception)
                {
                    if (messagingOutputChannel != null)
                    {
                        messagingOutputChannel.Abort();
                    }
                    if (messagingChannelFactory != null)
                    {
                        messagingChannelFactory.Abort();
                    }
                    throw;
                }

                // Wait for all receivers to receive message
                Thread.Sleep(TimeSpan.FromSeconds(5.0d));
                Console.Clear();

                // Create factory and channel using custom binding
                CustomBinding customBinding = new CustomBinding("customBinding");
                address = SampleManager.GetEndpointAddress(SampleManager.SessionQueueName, serviceBusNamespace);

                IChannelFactory <IOutputChannel> customChannelFactory = null;
                IOutputChannel customOutputChannel = null;
                try
                {
                    customChannelFactory = customBinding.BuildChannelFactory <IOutputChannel>(securityBehavior);
                    customChannelFactory.Open();
                    customOutputChannel = customChannelFactory.CreateChannel(address);
                    customOutputChannel.Open();

                    // Send messages to queue which requires session
                    Console.Title = "Session MessageSender";
                    Console.WriteLine("Preparing to send messages to {0}...", SampleManager.SessionQueueName);
                    Thread.Sleep(3000);

                    SendMessages(customOutputChannel, customBinding);
                    customOutputChannel.Close();
                    customChannelFactory.Close();
                }
                catch (Exception)
                {
                    if (customOutputChannel != null)
                    {
                        customOutputChannel.Abort();
                    }
                    if (customChannelFactory != null)
                    {
                        customChannelFactory.Abort();
                    }
                    throw;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception Occurred: {0}", exception);
                SampleManager.ExceptionOccurred = true;
            }

            // All messages sent
            Console.WriteLine("\nSender complete. Press [Enter] to exit.");
            Console.ReadLine();
        }
コード例 #11
0
 public virtual void Dispose()
 {
     _outputChannel.Close();
 }
コード例 #12
0
 public void CloseAfterFault(TimeSpan timeout)
 {
     _channel.Close(timeout);
 }
コード例 #13
0
        static void Main(string[] args)
        {
            // Creating the Channel
            string channelName = "EMailHelloWorld";

            string serverAddress          = "*****@*****.**";
            string serverPWD              = "MyPassword";
            string clientAddress          = "*****@*****.**";
            string exchangeServerLocation = "http://example.com";

            ExchangeWebServiceMailBinding binding =
                new ExchangeWebServiceMailBinding(
                    new Uri(exchangeServerLocation),
                    new System.Net.NetworkCredential(serverAddress,
                                                     serverPWD));
            BindingParameterCollection parameters =
                new BindingParameterCollection();

            IChannelListener <IInputChannel> listener =
                binding.BuildChannelListener <IInputChannel>
                    (MailUriHelper.CreateUri(channelName, ""), parameters);

            listener.Open();

            // Opening the Channel
            IInputChannel inputChannel = listener.AcceptChannel();

            inputChannel.Open(TimeSpan.MaxValue);
            Console.WriteLine("Channel Open");

            // Waiting and receiving the Message
            Message reply = inputChannel.Receive(TimeSpan.MaxValue);

            Console.WriteLine("Message Recieved");
            XmlSerializerWrapper wrapper = new XmlSerializerWrapper(
                typeof(TransmittedObject));
            TransmittedObject to = reply.GetBody <TransmittedObject>(wrapper);

            // Processing the Message
            to.str = to.str + " World";
            to.i   = to.i + 1;

            Console.WriteLine("Response: " + to.str + " " + to.i.ToString());

            // Creating and returning the Message
            Message m = Message.CreateMessage(binding.MessageVersion,
                                              "urn:test", to, wrapper);

            IChannelFactory <IOutputChannel> channelFactory =
                binding.BuildChannelFactory <IOutputChannel>(parameters);

            channelFactory.Open();

            IOutputChannel outChannel = channelFactory.CreateChannel(
                new EndpointAddress(
                    MailUriHelper.CreateUri(channelName, clientAddress)));

            outChannel.Open();

            Console.WriteLine("Out Channel Open");

            // Sending the Message over the OutChannel
            outChannel.Send(m);

            Console.WriteLine("Message Sent");

            // Cleaning up
            outChannel.Close();
            channelFactory.Close();

            listener.Close();
            inputChannel.Close();
            binding.Close();
        }
コード例 #14
0
        private void SendMessages(IOutputChannel channel)
        {
            channel.Open();

            AutoResetEvent doneSending      = new AutoResetEvent(false);
            int            threadsCompleted = 0;

            if (this.Parameters.NumberOfMessages > 0)
            {
                this.SendMessage(channel, "FirstMessage", true);
            }

            if (this.Parameters.NumberOfThreads == 1)
            {
                for (int j = 0; j < this.Parameters.NumberOfMessages; ++j)
                {
                    if (this.Parameters.SenderShouldAbort)
                    {
                        this.SendMessage(channel, "Message " + (j + 1), false);
                    }

                    this.SendMessage(channel, "Message " + (j + 1), true);
                }

                doneSending.Set();
            }
            else
            {
                for (int i = 0; i < this.Parameters.NumberOfThreads; ++i)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object unused)
                    {
                        for (int j = 0; j < this.Parameters.NumberOfMessages / this.Parameters.NumberOfThreads; ++j)
                        {
                            if (this.Parameters.SenderShouldAbort)
                            {
                                this.SendMessage(channel, "Message", false);
                            }

                            this.SendMessage(channel, "Message", true);
                        }
                        if (Interlocked.Increment(ref threadsCompleted) == this.Parameters.NumberOfThreads)
                        {
                            doneSending.Set();
                        }
                    }));
                }
            }

            TimeSpan threadTimeout = TimeSpan.FromMinutes(2.0);

            if (!doneSending.WaitOne(threadTimeout, false))
            {
                lock (this.Results)
                {
                    this.Results.Add(String.Format("Threads did not complete within {0}.", threadTimeout));
                }
            }

            doneSending.Close();
            channel.Close();
        }
コード例 #15
0
        private void sendButton_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            // Creating the Message
            TransmittedObject to = new TransmittedObject();

            to.str = "Hello";
            to.i   = 5;

            XmlSerializerWrapper wrapper =
                new XmlSerializerWrapper(typeof(TransmittedObject));

            Message m = Message.CreateMessage
                            (MessageVersion.Default, "urn:test", to, wrapper);

            // Creating the Channel
            string channelName = "EMailHelloWorld";

            string serverAddress = "*****@*****.**";
            string clientAddress = "*****@*****.**";

            WindowsMobileMailBinding   binding    = new WindowsMobileMailBinding();
            BindingParameterCollection parameters =
                new BindingParameterCollection();

            IChannelFactory <IOutputChannel> channelFactory =
                binding.BuildChannelFactory <IOutputChannel>(parameters);

            channelFactory.Open();

            // Opening the Channel
            IOutputChannel outChannel = channelFactory.CreateChannel(
                new EndpointAddress(MailUriHelper.Create
                                        (channelName, serverAddress)));

            outChannel.Open();

            // Sending the Message
            outChannel.Send(m);

            // Listening for the response
            IChannelListener <IInputChannel> listner =
                binding.BuildChannelListener <IInputChannel>
                    (MailUriHelper.CreateUri(channelName, clientAddress),
                    parameters);

            listner.Open();

            IInputChannel inputChannel = listner.AcceptChannel();

            inputChannel.Open();

            Message reply = inputChannel.Receive();

            // Deserializing and using the Message
            TransmittedObject to1 =
                reply.GetBody <TransmittedObject>
                    (new XmlSerializerWrapper(typeof(TransmittedObject)));

            MessageBox.Show(to1.str + " " + to1.i.ToString());

            // Cleaning up
            outChannel.Close();
            channelFactory.Close();

            listner.Close();
            inputChannel.Close();
            binding.Close();

            Cursor.Current = Cursors.Default;
        }