Пример #1
0
        void ProcessRequest(IReplyChannel reply, RequestContext rc)
        {
            try {
                var req = rc.RequestMessage;
                var ed  = FindEndpointDispatcher(req);
                new InputOrReplyRequestProcessor(ed.DispatchRuntime, reply).ProcessReply(rc);
            } catch (Exception ex) {
                Message res;
                if (ProcessErrorWithHandlers(reply, ex, out res))
                {
                    return;
                }

                if ((!(ex is SocketException)) &&
                    (!(ex is XmlException)) &&
                    (!(ex is IOException)) &&
                    rc != null)
                {
                    rc.Reply(res);
                }

                reply.Close(owner.DefaultCloseTimeout);                          // close the channel
            } finally {
                if (rc != null)
                {
                    rc.Close();
                }

                reply.Close(owner.DefaultCloseTimeout);                          // close the channel
            }
        }
Пример #2
0
        void ProcessRequest(IReplyChannel reply, RequestContext rc)
        {
            try {
                var req = rc.RequestMessage;
                var ed  = FindEndpointDispatcher(req);
                new InputOrReplyRequestProcessor(ed.DispatchRuntime, reply).ProcessReply(rc);
            } catch (Exception ex) {
                Message res;
                if (ProcessErrorWithHandlers(reply, ex, out res))
                {
                    return;
                }

                if ((!(ex is SocketException)) &&
                    (!(ex is XmlException)) &&
                    (!(ex is IOException)))
                {
                    rc.Reply(res);
                }

                reply.Close(owner.DefaultCloseTimeout);                          // close the channel
            } finally {
                if (rc != null)
                {
                    rc.Close();
                }
                // unless it is closed by session/call manager, move it back to the loop to receive the next message.
                if (loop && reply.State != CommunicationState.Closed)
                {
                    ProcessRequestOrInput(reply);
                }
            }
        }
Пример #3
0
        public static void Snippet1()
        {
            // <Snippet1>
            CustomBinding binding = new CustomBinding();

            binding.Elements.Add(new HttpTransportBindingElement());
            BindingParameterCollection       paramCollection = new BindingParameterCollection();
            IChannelListener <IReplyChannel> listener        = binding.BuildChannelListener <IReplyChannel>
                                                                   (new Uri("http://localhost:8000/ChannelApp"), paramCollection);

            listener.Open();
            IReplyChannel channel = listener.AcceptChannel();

            Console.WriteLine("Listening for messages");
            channel.Open();
            RequestContext request = channel.ReceiveRequest();
            Message        msg     = request.RequestMessage;

            Console.WriteLine("Message Received");
            Console.WriteLine("Message Action: {0}", msg.Headers.Action);

            if (msg.Headers.Action == "hello")
            {
                Message reply = Message.CreateMessage(MessageVersion.Default, "wcf");
                request.Reply(reply);
            }

            msg.Close();
            channel.Close();
            listener.Close();
            // </Snippet1>
        }
Пример #4
0
        public static void Snippet17()
        {
            // <Snippet17>
            CustomBinding binding = new CustomBinding();
            HttpTransportBindingElement element    = new HttpTransportBindingElement();
            BindingParameterCollection  parameters = new BindingParameterCollection();
            Uri            baseAddress             = new Uri("http://localhost:8000/ChannelApp");
            String         relAddress = "http://localhost:8000/ChannelApp/service";
            BindingContext context    = new BindingContext(binding, parameters, baseAddress, relAddress, ListenUriMode.Explicit);

            IChannelListener <IReplyChannel> listener = element.BuildChannelListener <IReplyChannel>(context);

            listener.Open();
            IReplyChannel channel = listener.AcceptChannel();

            channel.Open();
            RequestContext request = channel.ReceiveRequest();
            Message        msg     = request.RequestMessage;

            Console.WriteLine("Message Received");
            Console.WriteLine("Message Action: {0}", msg.Headers.Action);

            if (msg.Headers.Action == "hello")
            {
                Message reply = Message.CreateMessage(MessageVersion.Default, "wcf");
                request.Reply(reply);
            }

            msg.Close();
            channel.Close();
            listener.Close();
            // </Snippet17>
        }
Пример #5
0
        static void RunService()
        {
            //Step1: Create a custom binding with just TCP.
            BindingElement[] bindingElements = new BindingElement[2];
            bindingElements[0] = new TextMessageEncodingBindingElement();
            bindingElements[1] = new HttpTransportBindingElement();

            CustomBinding binding = new CustomBinding(bindingElements);


            //Step2: Use the binding to build the channel listener.
            IChannelListener <IReplyChannel> listener =
                binding.BuildChannelListener <IReplyChannel>(
                    new Uri("http://localhost:8080/channelapp"),
                    new BindingParameterCollection());

            //Step3: Listening for messages.
            listener.Open();
            Console.WriteLine(
                "Listening for incoming channel connections");
            //Wait for and accept incoming connections.
            IReplyChannel channel = listener.AcceptChannel();

            Console.WriteLine("Channel accepted. Listening for messages");
            //Open the accepted channel.
            channel.Open();
            //Wait for and receive a message from the channel.
            RequestContext request = channel.ReceiveRequest();
            //Step4: Reading the request message.
            Message message = request.RequestMessage;

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

            Console.WriteLine("Message content: {0}", data);
            //Send a reply.
            Message replymessage = Message.CreateMessage(
                binding.MessageVersion,
                "http://contoso.com/someotheraction",
                data);

            request.Reply(replymessage);
            //Step5: Closing objects.
            //Do not forget to close the message.
            message.Close();
            //Do not forget to close RequestContext.
            request.Close();
            //Do not forget to close channels.
            channel.Close();
            //Do not forget to close listeners.
            listener.Close();
        }
Пример #6
0
        public void Stop()
        {
            if (0 == Interlocked.Decrement(ref m_refcount))
            {
                // Stop processing loop;
                m_requestStop = true;
                m_threadManager.ThreadEvent.Set();

                m_replyChannel.Close();

                m_thread.Join();
            }
        }
Пример #7
0
        static void Main(string[] args)
        {
            try
            {
                //建立和发送端相同的通道栈
                BindingElement[] bindingElements = new BindingElement[2];
                bindingElements[0] = new TextMessageEncodingBindingElement();
                bindingElements[1] = new HttpTransportBindingElement();
                CustomBinding binding = new CustomBinding(bindingElements);

                //建立ChannelListener
                IChannelListener <IReplyChannel> listener = binding.BuildChannelListener <IReplyChannel>(
                    new Uri("http://localhost:9090/RequestReplyService"),
                    new BindingParameterCollection());
                listener.Open();       //打开监听

                //创建IReplyChannel
                IReplyChannel replyChannel = listener.AcceptChannel();
                replyChannel.Open();   //打开通道

                Console.WriteLine("开始接收消息");

                //接收并打印消息
                RequestContext requestContext = replyChannel.ReceiveRequest();     //会堵塞在这里

                Console.WriteLine("接收到一条消息, action为{0}, body为{1}",
                                  requestContext.RequestMessage.Headers.Action,
                                  requestContext.RequestMessage.GetBody <string>());

                //创建返回消息
                Message response = Message.CreateMessage(binding.MessageVersion, "response", "response body");

                //发送返回消息
                requestContext.Reply(response);

                requestContext.Close();
                replyChannel.Close();
                listener.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
        }
Пример #8
0
        private void BasicChannelTestB(bool customBinding)
        {
            IChannelListener <IReplyChannel> listener = null;
            IReplyChannel    channel = null;
            ManualResetEvent done    = new ManualResetEvent(false);
            Thread           t       = null;

            try
            {
                channel = this.OpenChannel(customBinding, out listener);
                Assert.IsNotNull(channel);
                Assert.IsNotNull(listener);

                t = new Thread(BasicChannelTests.SubmitRequests);
                t.Start(done);

                for (var cnt = 0; cnt < TestServiceCommon.Iterations; cnt++)
                {
                    RequestContext context;
                    if (channel.TryReceiveRequest(TestServiceCommon.DefaultHostTimeout, out context))
                    {
                        this.SendResponse(context);
                    }
                    else
                    {
                        Assert.Fail("TryReceiveRequest failed.");
                    }
                }

                channel.Close(TestServiceCommon.DefaultHostTimeout);
                listener.Close(TestServiceCommon.DefaultHostTimeout);
            }
            catch (Exception e)
            {
                channel.Abort();
                listener.Abort();
                Assert.Fail("Unexpected exception: " + e);
            }
            finally
            {
                if (t != null && !done.WaitOne(TestServiceCommon.DefaultHostTimeout))
                {
                    t.Abort();
                }

                done.Dispose();
            }
        }
        public void CustomTransportDoesNotRequireMessageEncoding()
        {
            ReplyHandler replier = delegate(Message msg)
            {
                resmsg = msg;
            };

            RequestReceiver receiver = delegate()
            {
                return(reqmsg);
            };

            RequestSender sender = delegate(Message msg)
            {
                reqmsg = msg;

                CustomBinding br = new CustomBinding(
                    new HandlerTransportBindingElement(replier, receiver));
                IChannelListener <IReplyChannel> l =
                    br.BuildChannelListener <IReplyChannel> (
                        new BindingParameterCollection());
                l.Open();
                IReplyChannel rch = l.AcceptChannel();
                rch.Open();
                Message res = Message.CreateMessage(MessageVersion.Default, "urn:succeeded");
                rch.ReceiveRequest().Reply(res);
                rch.Close();
                l.Close();

                return(resmsg);
            };

            CustomBinding bs = new CustomBinding(
                new HandlerTransportBindingElement(sender));

            IChannelFactory <IRequestChannel> f =
                bs.BuildChannelFactory <IRequestChannel> (
                    new BindingParameterCollection());

            f.Open();
            IRequestChannel ch = f.CreateChannel(new EndpointAddress("urn:dummy"));

            ch.Open();
            Message result = ch.Request(Message.CreateMessage(MessageVersion.Default, "urn:request"));
        }
Пример #10
0
        static void Main(string[] args)
        {
            MyBinding binding = new MyBinding();
            IChannelListener <IReplyChannel> channelListener = binding.BuildChannelListener <IReplyChannel>(new Uri("http://127.0.0.1:8888/messagingviabinding"));

            channelListener.Open();

            while (true)
            {
                IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.MaxValue);
                channel.Open();
                RequestContext context = channel.ReceiveRequest(TimeSpan.MaxValue);

                Console.WriteLine("Receive a request message:\n{0}", context.RequestMessage);
                Message replyMessage = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "http://artech.messagingviabinding", "This is a mannualy created reply message for the purpose of testing");
                context.Reply(replyMessage);
                channel.Close();
            }
        }
Пример #11
0
        static void Main(string[] args)
        {
            // Creating the Binding
            BasicHttpBinding binding = new BasicHttpBinding();

            BindingParameterCollection parameters =
                new BindingParameterCollection();

            IChannelListener <IReplyChannel> listener =
                binding.BuildChannelListener <IReplyChannel>
                    (new Uri("http://<<ServerAddress>>:<<PortNumber>>"),
                    parameters);

            listener.Open();

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

            // Opening the Channel
            IReplyChannel channel = listener.AcceptChannel();

            channel.Open(TimeSpan.MaxValue);

            // Waiting for and receiving the Message
            RequestContext r =
                channel.ReceiveRequest(TimeSpan.MaxValue);

            TransmittedObject to =
                r.RequestMessage.GetBody <TransmittedObject>(wrapper);

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

            // Creating the return Message
            Message m = Message.CreateMessage
                            (MessageVersion.Soap11, "urn:test", to, wrapper);

            r.Reply(m, TimeSpan.MaxValue);

            // Cleaning up
            channel.Close();
        }
Пример #12
0
        bool HandleRequest(IReplyChannel channel, RequestContext context)
        {
            if (context == null)
            {
                channel.Close();
                return(false);
            }

            try
            {
                serviceManager.HandleRequest(context);
            }
            catch (CommunicationException)
            {
                context.Abort();
            }
            finally
            {
                context.Close();
            }
            return(true);
        }
Пример #13
0
 public void CloseAfterFault(TimeSpan timeout)
 {
     _channel.Close(timeout);
 }
Пример #14
0
 protected override void OnClose(TimeSpan timeout)
 {
     inner.Close(timeout);
 }
Пример #15
0
        bool HandleRequest(IReplyChannel channel, RequestContext context)
        {
            if (context == null)
            {
                channel.Close();
                return false;
            }

            try
            {
                serviceManager.HandleRequest(context);
            }
            catch (CommunicationException)
            {
                context.Abort();
            }
            finally
            {
                context.Close();
            }
            return true;
        }
 public void Close()
 {
     _innerChannel.Close();
 }