Beispiel #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();
     }
 }
Beispiel #2
0
        /// <summary>
        /// Fecha um wcf channel associado a uma instancia de servico
        /// </summary>
        /// <param name="servico">instancia de um objeto criado anteriormente com Ativador.Get()</param>
        public static void AbortChannel(object servico)
        {
            try
            {
                if (servico == null)
                {
                    logger.Error("Erro em Ativador.AbortChannel(): Parametro nao pode ser nulo");
                    return;
                }

                IDuplexChannel channel2 = servico as IDuplexChannel;
                if (channel2 != null)
                {
                    logger.Info("Abort duplex channel for: " + servico.ToString());
                    channel2.Close();
                }
                else
                {
                    IChannel channel = servico as IChannel;
                    if (channel != null)
                    {
                        logger.Info("Abort channel for: " + servico.ToString());
                        try
                        {
                            channel.Close();
                        }
                        catch (Exception ex)
                        {
                            logger.Error("Erro em Ativador.AbortChannel()" + ex.Message, ex);
                            logger.Error("Trying really abort the channel");
                            channel.Abort();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Erro em Ativador.AbortChannel()" + ex.Message, ex);
            }
        }
 private void Window_Closed(object sender, EventArgs e)
 {
     duplexChannel.Close(); //关闭通道
     factory.Close();       //关闭通道工厂
 }