Returning XmlDocument from WCF service not working

前端 未结 2 670
别跟我提以往
别跟我提以往 2021-01-28 05:35

I am trying to update some WCF service methods that return strings to return XmlDocument objects. I\'ve tried returning it as-is and encapsulating it in a datacontract object. E

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-28 06:31

    There's a way to return a XmlDocument from WCF, but you need to use the XmlSerializer instead of the default serializer (DataContractSerialier) - the code below shows how it can be done. Having said that, do consider using data transfer objects as mentioned in the comments, unless your scenario really requires a XmlDocument to be transferred.

    public class StackOverflow_8951319
    {
        [ServiceContract]
        public interface ITest
        {
            [OperationContract]
            string Echo(string text);
            [OperationContract, XmlSerializerFormat]
            XmlDocument GetDocument();
        }
        public class Service : ITest
        {
            public string Echo(string text)
            {
                return text;
            }
    
            public XmlDocument GetDocument()
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(@"
      
        Bread
      
      
        Milk
      
      
        Coffee
      
    ");
                return doc;
            }
        }
        static Binding GetBinding()
        {
            var result = new WSHttpBinding(SecurityMode.None);
            //Change binding settings here
            return result;
        }
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
            host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
            host.Open();
            Console.WriteLine("Host opened");
    
            ChannelFactory factory = new ChannelFactory(GetBinding(), new EndpointAddress(baseAddress));
            ITest proxy = factory.CreateChannel();
            Console.WriteLine(proxy.Echo("Hello"));
    
            Console.WriteLine(proxy.GetDocument().OuterXml);
    
            ((IClientChannel)proxy).Close();
            factory.Close();
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    

提交回复
热议问题