Example #1
0
    public static void Test()
    {
        string          baseAddress = "net.tcp://" + Environment.MachineName + ":8000/Service";
        ServiceHost     host        = new ServiceHost(typeof(StackCalculator), new Uri(baseAddress));
        ServiceEndpoint endpoint    = host.AddServiceEndpoint(typeof(IStackCalculator), GetBinding(), "");

        endpoint.Behaviors.Add(new MyInstanceContextInitializer());
        host.Open();
        Console.WriteLine("Host opened");
        ChannelFactory <IStackCalculator> factory = new ChannelFactory <IStackCalculator>(GetBinding(), new EndpointAddress(baseAddress));
        IStackCalculator proxy = factory.CreateChannel();

        proxy.Enter(40);
        proxy.Enter(30);
        proxy.Enter(20);
        proxy.Add();
        proxy.Subtract();
        ChannelFactory <IStackCalculator> factory2 = new ChannelFactory <IStackCalculator>(GetBinding(), new EndpointAddress(baseAddress));
        IStackCalculator proxy2 = factory.CreateChannel();

        proxy2.Enter(40);
        proxy2.Enter(30);
        proxy2.Enter(20);
        proxy2.Add();
        proxy2.Subtract();
        ((IClientChannel)proxy).Close();
        factory.Close();
        ((IClientChannel)proxy2).Close();
        factory2.Close();
        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
        static void Main(string[] args)
        {
            string        baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost   host        = new ServiceHost(typeof(StackCalculator), new Uri(baseAddress));
            WSHttpBinding binding     = new WSHttpBinding(SecurityMode.None);

            binding.ReliableSession.Enabled = true;
            ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IStackCalculator), binding, "");

            endpoint.Behaviors.Add(new ClientTrackerEndpointBehavior());
            host.Open();
            Console.WriteLine("Host opened");

            ChannelFactory <IStackCalculator> factory = new ChannelFactory <IStackCalculator>(binding, new EndpointAddress(baseAddress));
            IStackCalculator proxy1 = factory.CreateChannel();

            Console.WriteLine("Created first client");
            proxy1.Enter(5);
            proxy1.Enter(8);
            proxy1.Multiply();
            Console.WriteLine();

            IStackCalculator proxy2 = factory.CreateChannel();

            Console.WriteLine("Created second channel");
            proxy2.Enter(5);
            proxy2.Enter(2);
            proxy2.Divide();
            Console.WriteLine();

            Console.WriteLine("Disconnecting the first client");
            ((IClientChannel)proxy1).Close();
            Console.WriteLine();

            Console.WriteLine("Using the second proxy again");
            proxy2.Enter(10);
            proxy2.Multiply();
            Console.WriteLine();

            Console.WriteLine("Closing the second client");
            ((IClientChannel)proxy2).Close();

            factory.Close();
            host.Close();
        }
Example #3
0
        static void Main(string[] args)
        {
            string          baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost     host        = new ServiceHost(typeof(StackCalculator), new Uri(baseAddress));
            var             binding     = new BasicHttpBinding();
            ServiceEndpoint endpoint    = host.AddServiceEndpoint(typeof(IStackCalculator), binding, "");

            endpoint.Behaviors.Add(new SharedSessionEndpointBehavior());
            host.Open();
            Console.WriteLine("Host opened");

            ChannelFactory <IStackCalculator> factory = new ChannelFactory <IStackCalculator>(binding, new EndpointAddress(baseAddress));
            IStackCalculator proxy = factory.CreateChannel();

            using (new OperationContextScope((IContextChannel)proxy))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(
                    MessageHeader.CreateHeader(
                        Constants.HeaderName,
                        Constants.HeaderNamespace,
                        "abcdef"));

                proxy.Enter(2);
                proxy.Enter(3);
                proxy.Add();
                proxy.Enter(4);
                proxy.Subtract();
                Console.WriteLine();
            }

            ((IClientChannel)proxy).Close();

            Console.WriteLine("Same header name...");
            proxy = factory.CreateChannel();
            using (new OperationContextScope((IContextChannel)proxy))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(
                    MessageHeader.CreateHeader(
                        Constants.HeaderName,
                        Constants.HeaderNamespace,
                        "abcdef"));

                proxy.Enter(5);
                proxy.Enter(6);
                proxy.Add();
                proxy.Enter(7);
                proxy.Subtract();
                Console.WriteLine();
            }

            ((IClientChannel)proxy).Close();

            Console.WriteLine("Now with a different header");
            proxy = factory.CreateChannel();
            using (new OperationContextScope((IContextChannel)proxy))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(
                    MessageHeader.CreateHeader(
                        Constants.HeaderName,
                        Constants.HeaderNamespace,
                        "other"));

                proxy.Enter(8);
                proxy.Enter(9);
                proxy.Add();
                proxy.Enter(10);
                proxy.Subtract();
                Console.WriteLine();
            }

            ((IClientChannel)proxy).Close();

            Console.WriteLine("Back to the first header...");
            proxy = factory.CreateChannel();
            using (new OperationContextScope((IContextChannel)proxy))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(
                    MessageHeader.CreateHeader(
                        Constants.HeaderName,
                        Constants.HeaderNamespace,
                        "abcdef"));

                proxy.Enter(11);
                proxy.Enter(12);
                proxy.Add();
                proxy.Enter(13);
                proxy.Subtract();
                Console.WriteLine();
            }

            ((IClientChannel)proxy).Close();

            Console.WriteLine("Now waiting until the context expires");
            Thread.Sleep(SharedInstanceContextInfo.SecondsToIdle * 1000 + 2);

            Console.WriteLine("Trying again with the original header name...");
            proxy = factory.CreateChannel();
            using (new OperationContextScope((IContextChannel)proxy))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(
                    MessageHeader.CreateHeader(
                        Constants.HeaderName,
                        Constants.HeaderNamespace,
                        "abcdef"));

                proxy.Enter(14);
                proxy.Enter(15);
                proxy.Add();
                proxy.Enter(16);
                proxy.Subtract();
                Console.WriteLine();
            }

            ((IClientChannel)proxy).Close();

            Console.WriteLine("Now reusing the same instance context for a long time");
            for (int i = 0; i <= 20; i++)
            {
                Thread.Sleep(1000 * SharedInstanceContextInfo.SecondsToIdle / 10);
                proxy = factory.CreateChannel();
                using (new OperationContextScope((IContextChannel)proxy))
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(
                        MessageHeader.CreateHeader(
                            Constants.HeaderName,
                            Constants.HeaderNamespace,
                            "abcdef"));

                    if (i <= 10)
                    {
                        proxy.Enter(i);
                    }
                    else
                    {
                        proxy.Add();
                    }
                }

                ((IClientChannel)proxy).Close();
            }

            Console.WriteLine("Press ENTER to close");
            Console.ReadLine();

            factory.Close();
            host.Close();
        }