Beispiel #1
0
        public void ExchangeMetadata()
        {
            // Service
            ServiceHost host = new ServiceHost(typeof(MetadataExchange));
            int         port = NetworkHelpers.FindFreePort();

            try {
                Binding binding = new BasicHttpBinding();
                binding.ReceiveTimeout = TimeSpan.FromSeconds(5);
                host.AddServiceEndpoint("IMetadataExchange",
                                        binding, new Uri("http://localhost:" + port));
                host.Open();
                // Client

                MetadataExchangeProxy proxy = new MetadataExchangeProxy(
                    new BasicHttpBinding(),
                    new EndpointAddress("http://localhost:" + port + "/"));
                proxy.Open();

                try {
                    Message req = Message.CreateMessage(MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
                    Message res = proxy.Get(req);
                } finally {
                    proxy.Close();
                }
            } finally {
                // Service
                if (host.State == CommunicationState.Opened)
                {
                    host.Close();
                }
            }
        }
        public void UseCase3()
        {
            // almost equivalent to samples/clientbase/samplesvc3.cs
            ServiceHost host = new ServiceHost(typeof(MetadataExchange));

            host.Description.Behaviors.Find <ServiceDebugBehavior> ()
            .IncludeExceptionDetailInFaults = true;
            Binding bs = new BasicHttpBinding();

            bs.SendTimeout    = TimeSpan.FromSeconds(5);
            bs.ReceiveTimeout = TimeSpan.FromSeconds(5);
            // magic name that does not require fully qualified name ...
            host.AddServiceEndpoint("IMetadataExchange",
                                    bs, new Uri("http://localhost:37564"));
            try
            {
                host.Open();
                // almost equivalent to samples/clientbase/samplecli3.cs
                Binding bc = new BasicHttpBinding();
                bc.SendTimeout    = TimeSpan.FromSeconds(5);
                bc.ReceiveTimeout = TimeSpan.FromSeconds(5);
                MetadataExchangeProxy proxy = new MetadataExchangeProxy(
                    bc,
                    new EndpointAddress("http://localhost:37564/"));
                proxy.Open();

                Message req = Message.CreateMessage(MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
                Message res = proxy.Get(req);
                using (XmlWriter w = XmlWriter.Create(TextWriter.Null))
                {
                    res.WriteMessage(w);
                }
            }
            finally
            {
                if (host.State == CommunicationState.Opened)
                {
                    host.Close();
                }
                EnsurePortNonBlocking(37564);
            }
        }