예제 #1
0
        public static EndpointAddress ReadEndpointAddress(DiscoveryVersion discoveryVersion, XmlReader reader)
        {
            Fx.Assert(discoveryVersion != null, "The discoveryVersion must be non null");
            Fx.Assert(reader != null, "The reader must be non null");

            if (discoveryVersion == DiscoveryVersion.WSDiscoveryApril2005 || discoveryVersion == DiscoveryVersion.WSDiscoveryCD1)
            {
                EndpointAddressAugust2004 endpoint = discoveryVersion.Implementation.EprSerializer.ReadObject(reader) as EndpointAddressAugust2004;
                if (endpoint == null)
                {
                    throw FxTrace.Exception.AsError(new XmlException(SR2.DiscoveryXmlEndpointNull));
                }
                return(endpoint.ToEndpointAddress());
            }
            else if (discoveryVersion == DiscoveryVersion.WSDiscovery11)
            {
                EndpointAddress10 endpoint = discoveryVersion.Implementation.EprSerializer.ReadObject(reader) as EndpointAddress10;
                if (endpoint == null)
                {
                    throw FxTrace.Exception.AsError(new XmlException(SR2.DiscoveryXmlEndpointNull));
                }
                return(endpoint.ToEndpointAddress());
            }
            else
            {
                Fx.Assert("The discoveryVersion parameter cannot be null.");
                return(null);
            }
        }
예제 #2
0
        private static EndpointAddress ProcessAppliesToElement(XmlReader xr)
        {
            if (xr == null)
            {
                throw new ArgumentNullException("xr");
            }
            if (xr.IsEmptyElement)
            {
                throw new ArgumentException("wsp:AppliesTo element was empty. Unable to create EndpointAddress object");
            }
            int             depth  = xr.Depth;
            EndpointAddress result = null;

            while (xr.Read())
            {
                if ("EndpointReference" == xr.LocalName && "http://www.w3.org/2005/08/addressing" == xr.NamespaceURI && !xr.IsEmptyElement && XmlNodeType.Element == xr.NodeType)
                {
                    DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(EndpointAddress10));
                    EndpointAddress10      endpointAddress        = (EndpointAddress10)dataContractSerializer.ReadObject(xr, false);
                    result = endpointAddress.ToEndpointAddress();
                }
                else if ("EndpointReference" == xr.LocalName && "http://schemas.xmlsoap.org/ws/2004/08/addressing" == xr.NamespaceURI && !xr.IsEmptyElement && XmlNodeType.Element == xr.NodeType)
                {
                    DataContractSerializer    dataContractSerializer2 = new DataContractSerializer(typeof(EndpointAddressAugust2004));
                    EndpointAddressAugust2004 endpointAddressAugust   = (EndpointAddressAugust2004)dataContractSerializer2.ReadObject(xr, false);
                    result = endpointAddressAugust.ToEndpointAddress();
                }
                if ("AppliesTo" == xr.LocalName && "http://schemas.xmlsoap.org/ws/2004/09/policy" == xr.NamespaceURI && xr.Depth == depth && XmlNodeType.EndElement == xr.NodeType)
                {
                    break;
                }
            }
            return(result);
        }
예제 #3
0
        public void ReadFromAugust2004()
        {
            string xml = @"<a:ReplyTo xmlns:a='http://schemas.xmlsoap.org/ws/2004/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";

            XmlReader           src    = XmlReader.Create(new StringReader(xml));
            XmlDictionaryReader reader =
                XmlDictionaryReader.CreateDictionaryReader(src);

            EndpointAddressAugust2004 e2k4 = EndpointAddressAugust2004.FromEndpointAddress(new EndpointAddress("http://test"));

            ((IXmlSerializable)e2k4).ReadXml(reader);
            Console.WriteLine(e2k4.ToEndpointAddress().Uri.AbsoluteUri);

            EndpointAddress a = e2k4.ToEndpointAddress();

            Assert.AreEqual("http://www.w3.org/2005/08/addressing/anonymous", a.Uri.AbsoluteUri, "#1");
            Assert.IsFalse(a.IsAnonymous, "#2");
        }
예제 #4
0
파일: source.cs 프로젝트: ruo2012/samples-1
        public static void Main()
        {
            // <Snippet1>
            // Create an EndpointAddress with a specified address.
            EndpointAddress epa1 = new EndpointAddress("http://localhost/ServiceModelSamples");

            Console.WriteLine("The URI of the EndpointAddress is {0}:", epa1.Uri);
            Console.WriteLine();

            //Initialize an EndpointAddressAugust2004 from the endpointAddress.
            EndpointAddressAugust2004 epaA4 = EndpointAddressAugust2004.FromEndpointAddress(epa1);

            //Serialize and then deserializde the EndpointAugust2004 type.

            //Convert the EndpointAugust2004 back into an EndpointAddress.
            EndpointAddress epa2 = epaA4.ToEndpointAddress();

            Console.WriteLine("The URI of the EndpointAddress is still {0}:", epa2.Uri);
            Console.WriteLine();
            // </Snippet1>
        }
        /// <summary>
        /// Converts and EndpointAddressAugust2004 to an EndpointReferenceType.
        /// </summary>
        /// <returns>Returns an EndpointReferenceType object</returns>
        public static EndpointReferenceType FromEndpointReference(EndpointAddressAugust2004 epr)
        {
            var builder = new StringBuilder();

            using (var writer = XmlDictionaryWriter.CreateDictionaryWriter(XmlWriter.Create(builder, new XmlWriterSettings()
            {
                Indent = false, OmitXmlDeclaration = true
            })))
            {
                epr.ToEndpointAddress().WriteTo(AddressingVersion.WSAddressingAugust2004, writer);
            }

            using (var reader = XmlDictionaryReader.CreateDictionaryReader(XmlReader.Create(new StringReader(builder.ToString()), new XmlReaderSettings()
            {
                IgnoreWhitespace = true
            })))
            {
                builder.Clear();

                var serializer = new XmlSerializer(typeof(EndpointReferenceType));

                return((EndpointReferenceType)serializer.Deserialize(reader));
            }
        }
예제 #6
0
        /// <summary>
        /// Reads a wsp:AppliesTo element
        /// </summary>
        /// <param name="xr">An XmlReader positioned on the start tag of wsp:AppliesTo</param>
        /// <returns>An EndpointAddress</returns>
        private static EndpointAddress ProcessAppliesToElement(XmlReader xr)
        {
            // If provided XmlReader is null, throw an exception
            if (xr == null)
            {
                throw new ArgumentNullException("xr");
            }

            // If the wsp:AppliesTo element is empty, then throw an exception.
            if (xr.IsEmptyElement)
            {
                throw new ArgumentException("wsp:AppliesTo element was empty. Unable to create EndpointAddress object");
            }

            // Store the initial depth so we can exit this function when we reach the corresponding end-tag
            int initialDepth = xr.Depth;

            // Set our return value to null
            EndpointAddress ea = null;

            // Enter a read loop...
            while (xr.Read())
            {
                // Look for a WS-Addressing 1.0 Endpoint Reference...
                if (Constants.Addressing.Elements.EndpointReference == xr.LocalName &&
                    Constants.Addressing.NamespaceUri == xr.NamespaceURI &&
                    !xr.IsEmptyElement &&
                    XmlNodeType.Element == xr.NodeType)
                {
                    // Create a DataContractSerializer for an EndpointAddress10
                    DataContractSerializer dcs = new DataContractSerializer(typeof(EndpointAddress10));
                    // Read the EndpointAddress10 from the DataContractSerializer
                    EndpointAddress10 ea10 = (EndpointAddress10)dcs.ReadObject(xr, false);
                    // Convert the EndpointAddress10 into an EndpointAddress
                    ea = ea10.ToEndpointAddress();
                }
                // Look for a WS-Addressing 2004/08 Endpoint Reference...
                else if (Constants.Addressing.Elements.EndpointReference == xr.LocalName &&
                         Constants.Addressing.NamespaceUriAugust2004 == xr.NamespaceURI &&
                         !xr.IsEmptyElement &&
                         XmlNodeType.Element == xr.NodeType)
                {
                    // Create a DataContractSerializer for an EndpointAddressAugust2004
                    DataContractSerializer dcs = new DataContractSerializer(typeof(EndpointAddressAugust2004));
                    // Read the EndpointAddressAugust2004 from the DataContractSerializer
                    EndpointAddressAugust2004 eaAugust2004 = (EndpointAddressAugust2004)dcs.ReadObject(xr, false);
                    // Convert the EndpointAddressAugust2004 into an EndpointAddress
                    ea = eaAugust2004.ToEndpointAddress();
                }

                // Look for the end-tag that corresponds to the start-tag the reader was positioned
                // on when the method was called. When we find it, break out of the read loop.
                if (Constants.Policy.Elements.AppliesTo == xr.LocalName &&
                    Constants.Policy.NamespaceUri == xr.NamespaceURI &&
                    xr.Depth == initialDepth &&
                    XmlNodeType.EndElement == xr.NodeType)
                {
                    break;
                }
            }

            // Return the EndpointAddress
            return(ea);
        }